toon-format-rs 0.1.0

Token-Oriented Object Notation (TOON) parser and serializer for Rust
Documentation
use std::fmt;

/// Errors that can occur during TOON parsing or serialization
#[derive(Debug, Clone, PartialEq)]
pub enum Error {
    /// Unexpected end of input
    UnexpectedEof,
    /// Invalid character at position
    InvalidCharacter { ch: char, pos: usize },
    /// Invalid number format
    InvalidNumber { text: String, pos: usize },
    /// Invalid escape sequence
    InvalidEscape { seq: String, pos: usize },
    /// Invalid header syntax (array declaration)
    InvalidHeader { text: String, pos: usize },
    /// Array length mismatch: declared N but found M elements
    ArrayLengthMismatch { declared: usize, found: usize, pos: usize },
    /// Field count mismatch in tabular row
    FieldCountMismatch { expected: usize, found: usize, pos: usize },
    /// Invalid indentation (wrong depth)
    InvalidIndentation { expected: usize, found: usize, pos: usize },
    /// Missing colon after key
    MissingColon { pos: usize },
    /// Invalid UTF-8 in input
    InvalidUtf8,
    /// Generic message
    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 {}

/// Result type alias for TOON operations
pub type Result<T> = std::result::Result<T, Error>;