#[derive(Debug)]
pub enum RegistryError {
UnknownStyle(String),
}
impl std::fmt::Display for RegistryError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::UnknownStyle(s) => write!(f, "unknown style: '{s}' has not been registered"),
}
}
}
pub struct LexErrorDisplay<'a> {
pub error: &'a LexError,
pub input: &'a str,
}
impl std::fmt::Display for LexErrorDisplay<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, " | {}", self.input)?;
match self.error {
LexError::UnclosedTag(position) => {
write!(f, " | {}^ unclosed tag", " ".repeat(*position))
}
LexError::InvalidTag {
tag_content,
position,
} => {
write!(
f,
" | {}{} invalid tag: '{tag_content}'",
" ".repeat(*position + 1),
"^".repeat(tag_content.len())
)
}
LexError::UnclosedValue(position) => {
write!(
f,
" | {}^ unclosed parentheses for color value",
" ".repeat(*position + 1)
)
}
LexError::InvalidArgumentCount {
expected,
got,
position,
} => {
write!(
f,
" | {}^ expected {expected} arguments, got {got}",
" ".repeat(*position + 1)
)
}
LexError::InvalidValue { value, position } => {
write!(
f,
" | {}{} invalid value: '{value}'",
" ".repeat(*position + 1),
"^".repeat(value.len())
)
}
LexError::InvalidResetTarget(position) => {
write!(
f,
" | {}^ reset target must be a color or emphasis tag",
" ".repeat(*position + 1)
)
}
}
}
}
#[derive(Debug)]
pub enum LexError {
UnclosedTag(usize),
InvalidTag {
tag_content: String,
position: usize,
},
UnclosedValue(usize),
InvalidArgumentCount {
expected: usize,
got: usize,
position: usize,
},
InvalidValue {
value: String,
position: usize,
},
InvalidResetTarget(usize),
}
impl LexError {
pub fn display<'a>(&'a self, input: &'a str) -> LexErrorDisplay<'a> {
LexErrorDisplay { error: self, input }
}
}
impl std::fmt::Display for LexError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
LexError::UnclosedTag(pos) => write!(f, "unclosed tag at position {pos}"),
LexError::InvalidTag {
tag_content,
position,
} => write!(f, "invalid tag '{tag_content}' at position {position}"),
LexError::UnclosedValue(pos) => {
write!(f, "unclosed parentheses for color value at position {pos}")
}
LexError::InvalidArgumentCount {
expected,
got,
position,
} => {
write!(
f,
"expected {expected} arguments, got {got} at position {position}"
)
}
LexError::InvalidValue { value, position } => {
write!(f, "invalid value '{value}' at position {position}")
}
LexError::InvalidResetTarget(pos) => write!(
f,
"reset target must be a color or emphasis tag at position {pos}"
),
}
}
}
impl std::error::Error for LexError {}