use alloc::{format, string::ToString};
use core::{
cmp::PartialEq,
convert::From,
error::Error as StdError,
fmt::{Debug, Display, Formatter, Result as FmtResult},
num::ParseIntError,
option::Option::{self, None, Some},
result::Result as StdResult,
};
#[derive(PartialEq)]
pub enum Error {
InvalidRepresentation,
MissingRadixPrefix,
InvalidRadixPrefix,
InvalidStringQuotes,
InvalidArrayBrackets,
InvalidByteRepresentation { source: ParseIntError },
}
pub type Result<T> = StdResult<Error, T>;
#[inline]
pub fn parse_error(source: ParseIntError) -> Error {
Error::InvalidByteRepresentation { source }
}
impl Debug for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
match self {
Self::InvalidRepresentation => write!(f, "InvalidRepresentation"),
Self::MissingRadixPrefix => write!(f, "MissingRadixPrefix"),
Self::InvalidRadixPrefix => write!(f, "InvalidRadixPrefix"),
Self::InvalidStringQuotes => write!(f, "InvalidStringQuotes"),
Self::InvalidArrayBrackets => write!(f, "InvalidArrayBrackets"),
Self::InvalidByteRepresentation { source } => f
.debug_struct("InvalidByteRepresentation")
.field("source", source)
.finish(),
}
}
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
write!(
f,
"{}",
match self {
Self::InvalidRepresentation =>
"The binary string representation is invalid.".to_string(),
Self::MissingRadixPrefix =>
"The binary string representation is missing a radix prefix.".to_string(),
Self::InvalidRadixPrefix =>
"The binary string representation has an invalid radix prefix.".to_string(),
Self::InvalidStringQuotes =>
"The binary string representation must be correctly enclosed in double quotes: '\"'."
.to_string(),
Self::InvalidArrayBrackets =>
"The binary array representation must be correctly enclosed in brackets: '[' and ']'.".to_string(),
Self::InvalidByteRepresentation { source } => {
format!("Failed to parse individual byte representation; source error: {source}")
}
}
)
}
}
impl StdError for Error {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
match self {
Self::InvalidByteRepresentation { source } => Some(source),
_ => None,
}
}
}
impl From<ParseIntError> for Error {
fn from(source: ParseIntError) -> Self {
Self::InvalidByteRepresentation { source }
}
}