Skip to content

Commit cecdc38

Browse files
committed
add message to decoding error
1 parent ced93a7 commit cecdc38

File tree

2 files changed

+25
-29
lines changed

2 files changed

+25
-29
lines changed

src/binary.rs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
pub mod error;
2-
use crate::{
3-
binary::error::{Error, Result},
4-
spec::BinarySubtype,
5-
Document,
6-
RawBinaryRef,
7-
};
1+
use crate::{spec::BinarySubtype, Document, RawBinaryRef};
82
use std::{
93
convert::TryFrom,
4+
error,
105
fmt::{self, Display},
116
};
127

@@ -36,7 +31,9 @@ impl Binary {
3631
/// `subtype` argument is `None`, the [`Binary`] constructed will default to
3732
/// [`BinarySubtype::Generic`].
3833
pub fn from_base64(input: &str, subtype: impl Into<Option<BinarySubtype>>) -> Result<Self> {
39-
let bytes = base64::decode(input).map_err(|_| Error::DecodingError)?;
34+
let bytes = base64::decode(input).map_err(|e| Error::DecodingError {
35+
message: e.to_string(),
36+
})?;
4037
let subtype = match subtype.into() {
4138
Some(s) => s,
4239
None => BinarySubtype::Generic,
@@ -79,3 +76,23 @@ impl Binary {
7976
}
8077
}
8178
}
79+
80+
/// Possible errors that can arise during [`Binary`] construction.
81+
#[derive(Clone, Debug)]
82+
#[non_exhaustive]
83+
pub enum Error {
84+
/// While trying to decode from base64, an error was returned.
85+
DecodingError { message: String },
86+
}
87+
88+
impl error::Error for Error {}
89+
90+
impl std::fmt::Display for Error {
91+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
92+
match self {
93+
Error::DecodingError { message: m } => fmt.write_str(m),
94+
}
95+
}
96+
}
97+
98+
pub type Result<T> = std::result::Result<T, Error>;

src/binary/error.rs

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)