forked from P3KI/bendy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.rs
More file actions
147 lines (128 loc) · 4.35 KB
/
error.rs
File metadata and controls
147 lines (128 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#[cfg(not(feature = "std"))]
use alloc::{
format,
string::{FromUtf8Error, String, ToString},
};
#[cfg(not(feature = "std"))]
use core::{
fmt::{self, Display, Formatter},
num::ParseIntError,
};
#[cfg(feature = "std")]
use std::{
error::Error as StdError,
fmt::{self, Display, Formatter},
sync::Arc,
};
use failure::Fail;
use crate::state_tracker::StructureError;
#[derive(Debug, Clone, Fail)]
pub struct Error {
#[fail(context)]
context: Option<String>,
#[fail(cause)]
error: ErrorKind,
}
/// An enumeration of potential errors that appear during bencode deserialization.
#[derive(Debug, Clone, Fail)]
pub enum ErrorKind {
/// Error that occurs if the serialized structure contains invalid semantics.
#[cfg(feature = "std")]
#[fail(display = "malformed content discovered: {}", _0)]
MalformedContent(Arc<failure::Error>),
/// Error that occurs if the serialized structure contains invalid semantics.
#[cfg(not(feature = "std"))]
#[fail(display = "malformed content discovered")]
MalformedContent,
/// Error that occurs if the serialized structure is incomplete.
#[fail(display = "missing field: {}", _0)]
MissingField(String),
/// Error in the bencode structure (e.g. a missing field end separator).
#[fail(display = "bencode encoding corrupted ({})", _0)]
StructureError(#[fail(cause)] StructureError),
/// Error that occurs if the serialized structure contains an unexpected field.
#[fail(display = "unexpected field: {}", _0)]
UnexpectedField(String),
/// Error through an unexpected bencode token during deserialization.
#[fail(display = "discovered {} but expected {}", _0, _1)]
UnexpectedToken(String, String),
}
pub trait ResultExt {
fn context(self, context: impl Display) -> Self;
}
impl Error {
pub fn context(mut self, context: impl Display) -> Self {
if let Some(current) = self.context.as_mut() {
*current = format!("{}.{}", context, current);
} else {
self.context = Some(context.to_string());
}
self
}
/// Raised when there is a general error while deserializing a type.
/// The message should not be capitalized and should not end with a period.
#[cfg(feature = "std")]
pub fn malformed_content(cause: impl Into<failure::Error>) -> Error {
let error = Arc::new(cause.into());
Self::from(ErrorKind::MalformedContent(error))
}
/// Returns a `Error::MissingField` which contains the name of the field.
pub fn missing_field(field_name: impl Display) -> Error {
Self::from(ErrorKind::MissingField(field_name.to_string()))
}
/// Returns a `Error::UnexpectedField` which contains the name of the field.
pub fn unexpected_field(field_name: impl Display) -> Error {
Self::from(ErrorKind::UnexpectedField(field_name.to_string()))
}
/// Returns a `Error::UnexpectedElement` which contains a custom error message.
pub fn unexpected_token(expected: impl Display, discovered: impl Display) -> Error {
Self::from(ErrorKind::UnexpectedToken(
expected.to_string(),
discovered.to_string(),
))
}
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match &self.context {
Some(context) => write!(f, "Error: {} in {}", self.error, context),
None => write!(f, "Error: {}", self.error),
}
}
}
impl From<StructureError> for Error {
fn from(error: StructureError) -> Self {
Self::from(ErrorKind::StructureError(error))
}
}
impl From<ErrorKind> for Error {
fn from(kind: ErrorKind) -> Self {
Self {
context: None,
error: kind,
}
}
}
#[cfg(not(feature = "std"))]
impl From<FromUtf8Error> for Error {
fn from(_: FromUtf8Error) -> Self {
Self::from(ErrorKind::MalformedContent)
}
}
#[cfg(not(feature = "std"))]
impl From<ParseIntError> for Error {
fn from(_: ParseIntError) -> Self {
Self::from(ErrorKind::MalformedContent)
}
}
#[cfg(feature = "std")]
impl<T: StdError + Send + Sync + 'static> From<T> for Error {
fn from(error: T) -> Self {
Self::malformed_content(error)
}
}
impl<T> ResultExt for Result<T, Error> {
fn context(self, context: impl Display) -> Result<T, Error> {
self.map_err(|err| err.context(context))
}
}