use std::fmt;
#[derive(Debug, Clone, PartialEq)]
pub enum Error {
UnexpectedEof,
InvalidCharacter { ch: char, pos: usize },
InvalidNumber { text: String, pos: usize },
InvalidEscape { seq: String, pos: usize },
InvalidHeader { text: String, pos: usize },
ArrayLengthMismatch { declared: usize, found: usize, pos: usize },
FieldCountMismatch { expected: usize, found: usize, pos: usize },
InvalidIndentation { expected: usize, found: usize, pos: usize },
MissingColon { pos: usize },
InvalidUtf8,
Message(String),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::UnexpectedEof => write!(f, "unexpected end of input"),
Error::InvalidCharacter { ch, pos } => {
write!(f, "invalid character '{}' at position {}", ch, pos)
}
Error::InvalidNumber { text, pos } => {
write!(f, "invalid number '{}' at position {}", text, pos)
}
Error::InvalidEscape { seq, pos } => {
write!(f, "invalid escape sequence '{}' at position {}", seq, pos)
}
Error::InvalidHeader { text, pos } => {
write!(f, "invalid array header '{}' at position {}", text, pos)
}
Error::ArrayLengthMismatch { declared, found, pos } => {
write!(
f,
"array length mismatch: declared {} but found {} elements at position {}",
declared, found, pos
)
}
Error::FieldCountMismatch { expected, found, pos } => {
write!(
f,
"field count mismatch: expected {} but found {} at position {}",
expected, found, pos
)
}
Error::InvalidIndentation { expected, found, pos } => {
write!(
f,
"invalid indentation: expected depth {} but found {} at position {}",
expected, found, pos
)
}
Error::MissingColon { pos } => {
write!(f, "missing colon after key at position {}", pos)
}
Error::InvalidUtf8 => write!(f, "invalid UTF-8 in input"),
Error::Message(msg) => write!(f, "{}", msg),
}
}
}
impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Self {
Error::Message(err.to_string())
}
}
impl From<std::fmt::Error> for Error {
fn from(err: std::fmt::Error) -> Self {
Error::Message(err.to_string())
}
}
impl std::error::Error for Error {}
pub type Result<T> = std::result::Result<T, Error>;