Skip to main content

toon_format_rs/
error.rs

1use std::fmt;
2
3/// Errors that can occur during TOON parsing or serialization
4#[derive(Debug, Clone, PartialEq)]
5pub enum Error {
6    /// Unexpected end of input
7    UnexpectedEof,
8    /// Invalid character at position
9    InvalidCharacter { ch: char, pos: usize },
10    /// Invalid number format
11    InvalidNumber { text: String, pos: usize },
12    /// Invalid escape sequence
13    InvalidEscape { seq: String, pos: usize },
14    /// Invalid header syntax (array declaration)
15    InvalidHeader { text: String, pos: usize },
16    /// Array length mismatch: declared N but found M elements
17    ArrayLengthMismatch { declared: usize, found: usize, pos: usize },
18    /// Field count mismatch in tabular row
19    FieldCountMismatch { expected: usize, found: usize, pos: usize },
20    /// Invalid indentation (wrong depth)
21    InvalidIndentation { expected: usize, found: usize, pos: usize },
22    /// Missing colon after key
23    MissingColon { pos: usize },
24    /// Invalid UTF-8 in input
25    InvalidUtf8,
26    /// Generic message
27    Message(String),
28}
29
30impl fmt::Display for Error {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        match self {
33            Error::UnexpectedEof => write!(f, "unexpected end of input"),
34            Error::InvalidCharacter { ch, pos } => {
35                write!(f, "invalid character '{}' at position {}", ch, pos)
36            }
37            Error::InvalidNumber { text, pos } => {
38                write!(f, "invalid number '{}' at position {}", text, pos)
39            }
40            Error::InvalidEscape { seq, pos } => {
41                write!(f, "invalid escape sequence '{}' at position {}", seq, pos)
42            }
43            Error::InvalidHeader { text, pos } => {
44                write!(f, "invalid array header '{}' at position {}", text, pos)
45            }
46            Error::ArrayLengthMismatch { declared, found, pos } => {
47                write!(
48                    f,
49                    "array length mismatch: declared {} but found {} elements at position {}",
50                    declared, found, pos
51                )
52            }
53            Error::FieldCountMismatch { expected, found, pos } => {
54                write!(
55                    f,
56                    "field count mismatch: expected {} but found {} at position {}",
57                    expected, found, pos
58                )
59            }
60            Error::InvalidIndentation { expected, found, pos } => {
61                write!(
62                    f,
63                    "invalid indentation: expected depth {} but found {} at position {}",
64                    expected, found, pos
65                )
66            }
67            Error::MissingColon { pos } => {
68                write!(f, "missing colon after key at position {}", pos)
69            }
70            Error::InvalidUtf8 => write!(f, "invalid UTF-8 in input"),
71            Error::Message(msg) => write!(f, "{}", msg),
72        }
73    }
74}
75
76impl From<std::io::Error> for Error {
77    fn from(err: std::io::Error) -> Self {
78        Error::Message(err.to_string())
79    }
80}
81
82impl From<std::fmt::Error> for Error {
83    fn from(err: std::fmt::Error) -> Self {
84        Error::Message(err.to_string())
85    }
86}
87
88impl std::error::Error for Error {}
89
90/// Result type alias for TOON operations
91pub type Result<T> = std::result::Result<T, Error>;