toon_rust/
error.rs

1//! Error types for TOON encoding and decoding
2
3use thiserror::Error;
4
5/// Errors that can occur during TOON encoding or decoding
6#[derive(Error, Debug, PartialEq)]
7pub enum Error {
8    /// Parse error with position information
9    #[error("Parse error at position {position}: {message}")]
10    Parse { position: usize, message: String },
11
12    /// Syntax error
13    #[error("Syntax error: {0}")]
14    Syntax(String),
15
16    /// Invalid escape sequence
17    #[error("Invalid escape sequence: {0}")]
18    InvalidEscape(String),
19
20    /// Array length mismatch
21    #[error("Array length mismatch: expected {expected}, found {found}")]
22    LengthMismatch { expected: usize, found: usize },
23
24    /// Delimiter mismatch
25    #[error("Delimiter mismatch: expected '{expected}', found '{found}'")]
26    DelimiterMismatch { expected: char, found: char },
27
28    /// Unterminated string
29    #[error("Unterminated string")]
30    UnterminatedString,
31
32    /// Invalid number format
33    #[error("Invalid number format: {0}")]
34    InvalidNumber(String),
35
36    /// Missing required field
37    #[error("Missing required field: {0}")]
38    MissingField(String),
39
40    /// Invalid header format
41    #[error("Invalid header format: {0}")]
42    InvalidHeader(String),
43
44    /// IO error
45    #[error("IO error: {0}")]
46    Io(String),
47
48    /// Serialization error
49    #[error("Serialization error: {0}")]
50    Serialization(String),
51
52    /// Deserialization error
53    #[error("Deserialization error: {0}")]
54    Deserialization(String),
55}
56
57impl Error {
58    /// Create a parse error
59    pub fn parse(position: usize, message: impl Into<String>) -> Self {
60        Self::Parse {
61            position,
62            message: message.into(),
63        }
64    }
65
66    /// Create a syntax error
67    pub fn syntax(message: impl Into<String>) -> Self {
68        Self::Syntax(message.into())
69    }
70}