use std::default::Default;
use std::fmt::Display;
use std::io;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Position {
pub offset: usize,
pub col: usize,
pub line: usize,
}
impl Default for Position {
fn default() -> Self {
Self {
offset: 0,
line: 1,
col: 0,
}
}
}
impl Display for Position {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"offset:{} line:{} col:{}",
self.offset, self.line, self.col
)
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum Reason {
ExpectedNumber,
ExpectedString,
ExpectedBool,
ExpectedNull,
UnexpectedCtrlChar,
InvalidUtf8,
InvalidEscapeCode,
UnexpectedEof,
UnexpectedChar,
NonBlockingRead,
CouldNotRewindInput(io::ErrorKind),
UnrecognizedIoError(io::ErrorKind),
}
#[derive(Debug, Copy, Clone, thiserror::Error, PartialEq, Eq)]
#[error("malformed json at position {position}")]
pub struct JsonError {
pub reason: Reason,
pub position: Position,
}
impl From<io::ErrorKind> for Reason {
fn from(value: io::ErrorKind) -> Self {
match value {
io::ErrorKind::UnexpectedEof => Reason::UnexpectedEof,
io::ErrorKind::WouldBlock => Reason::NonBlockingRead,
_ => Reason::UnrecognizedIoError(value),
}
}
}