forked from whalelephant/rust-multibase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.rs
More file actions
57 lines (50 loc) · 1.63 KB
/
Copy patherror.rs
File metadata and controls
57 lines (50 loc) · 1.63 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
use core::fmt;
/// Type alias to use this library's [`Error`] type in a `Result`.
pub type Result<T> = core::result::Result<T, Error>;
/// Error types
#[derive(PartialEq, Eq, Clone, Debug)]
pub enum Error {
/// Unknown base code.
UnknownBase(char),
/// Invalid string.
InvalidBaseString,
/// Decode Err
DecodeError(data_encoding::DecodeError),
/// Encoding/Decode Failed
WriteFail(data_encoding::DecodePartial),
/// Mismatched sizes
MismatchedSizes(usize, usize),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::UnknownBase(code) => write!(f, "Unknown base code: {}", code),
Error::InvalidBaseString => write!(f, "Invalid base string"),
Error::DecodeError(err) => write!(f, "Error decoding something: {:?}", err),
Error::WriteFail(partial) => write!(f, "Partial Decoding: {:?}", partial),
Error::MismatchedSizes(input, output) => write!(
f,
"Input and output slices are different sizes {}:{}",
input, output
),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for Error {}
#[cfg(feature = "alloc")]
impl From<base_x::DecodeError> for Error {
fn from(_: base_x::DecodeError) -> Self {
Self::InvalidBaseString
}
}
impl From<data_encoding::DecodeError> for Error {
fn from(_: data_encoding::DecodeError) -> Self {
Self::InvalidBaseString
}
}
impl From<data_encoding::DecodePartial> for Error {
fn from(err: data_encoding::DecodePartial) -> Self {
Self::WriteFail(err)
}
}