|
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}; |
8 | 2 | use std::{
|
9 | 3 | convert::TryFrom,
|
| 4 | + error, |
10 | 5 | fmt::{self, Display},
|
11 | 6 | };
|
12 | 7 |
|
@@ -36,7 +31,9 @@ impl Binary {
|
36 | 31 | /// `subtype` argument is `None`, the [`Binary`] constructed will default to
|
37 | 32 | /// [`BinarySubtype::Generic`].
|
38 | 33 | 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 | + })?; |
40 | 37 | let subtype = match subtype.into() {
|
41 | 38 | Some(s) => s,
|
42 | 39 | None => BinarySubtype::Generic,
|
@@ -79,3 +76,23 @@ impl Binary {
|
79 | 76 | }
|
80 | 77 | }
|
81 | 78 | }
|
| 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>; |
0 commit comments