use alloc::string::String;
use core::fmt;
use crate::error;
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum InvalidFormatDescription {
#[non_exhaustive]
UnclosedOpeningBracket {
index: usize,
},
#[non_exhaustive]
InvalidComponentName {
name: String,
index: usize,
},
#[non_exhaustive]
InvalidModifier {
value: String,
index: usize,
},
#[non_exhaustive]
MissingComponentName {
index: usize,
},
}
impl From<InvalidFormatDescription> for crate::Error {
fn from(original: InvalidFormatDescription) -> Self {
Self::InvalidFormatDescription(original)
}
}
impl TryFrom<crate::Error> for InvalidFormatDescription {
type Error = error::DifferentVariant;
fn try_from(err: crate::Error) -> Result<Self, Self::Error> {
match err {
crate::Error::InvalidFormatDescription(err) => Ok(err),
_ => Err(error::DifferentVariant),
}
}
}
impl fmt::Display for InvalidFormatDescription {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use InvalidFormatDescription::*;
match self {
UnclosedOpeningBracket { index } => {
write!(f, "unclosed opening bracket at byte index {index}")
}
InvalidComponentName { name, index } => {
write!(f, "invalid component name `{name}` at byte index {index}")
}
InvalidModifier { value, index } => {
write!(f, "invalid modifier `{value}` at byte index {index}")
}
MissingComponentName { index } => {
write!(f, "missing component name at byte index {index}")
}
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for InvalidFormatDescription {}