use crate::ast::FormatType;
use alloc::string::String;
use core::fmt;
#[derive(Debug)]
pub enum Error {
Parse { offset: usize, message: String },
MissingArgument { name: String, offset: usize },
UnsupportedTrait {
format_type: FormatType,
offset: usize,
},
Format(fmt::Error),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Parse {
message, offset, ..
} => {
write!(f, "parse error at byte {}: {}", offset, message)
}
Self::MissingArgument { name, .. } => {
write!(f, "missing argument: `{name}`")
}
Self::UnsupportedTrait { format_type, .. } => {
let trait_name = match format_type {
FormatType::Octal => "Octal",
FormatType::LowerHex => "LowerHex",
FormatType::UpperHex => "UpperHex",
FormatType::Binary => "Binary",
FormatType::LowerExp => "LowerExp",
FormatType::UpperExp => "UpperExp",
FormatType::Pointer => "Pointer",
_ => "Unknown",
};
write!(f, "unsupported format trait: `{trait_name}`")
}
Self::Format(e) => write!(f, "formatting error: {e}"),
}
}
}
impl core::error::Error for Error {
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
if let Self::Format(e) = self {
Some(e)
} else {
None
}
}
}
impl From<fmt::Error> for Error {
fn from(e: fmt::Error) -> Self {
Self::Format(e)
}
}