fast_yaml_core/
error.rs

1use thiserror::Error;
2
3/// Errors that can occur during YAML parsing.
4#[derive(Error, Debug)]
5pub enum ParseError {
6    /// YAML syntax error with location information.
7    #[error("YAML parse error at line {line}, column {column}: {message}")]
8    Syntax {
9        /// Line number where the error occurred (1-indexed).
10        line: usize,
11        /// Column number where the error occurred (1-indexed).
12        column: usize,
13        /// Description of the syntax error.
14        message: String,
15    },
16
17    /// Invalid float value encountered.
18    #[error("invalid float value '{value}': {source}")]
19    InvalidFloat {
20        /// The invalid float value string.
21        value: String,
22        /// The underlying parse error.
23        #[source]
24        source: std::num::ParseFloatError,
25    },
26
27    /// YAML scanner error from saphyr.
28    #[error("YAML scanner error: {0}")]
29    Scanner(#[from] saphyr::ScanError),
30}
31
32/// Errors that can occur during YAML emission.
33#[derive(Error, Debug)]
34pub enum EmitError {
35    /// General emission error.
36    #[error("failed to emit YAML: {0}")]
37    Emit(String),
38
39    /// Attempted to serialize an unsupported type.
40    #[error("unsupported type for serialization: {0}")]
41    UnsupportedType(String),
42}
43
44/// Result type for parsing operations.
45pub type ParseResult<T> = std::result::Result<T, ParseError>;
46
47/// Result type for emission operations.
48pub type EmitResult<T> = std::result::Result<T, EmitError>;
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53
54    #[test]
55    fn test_parse_error_display() {
56        let err = ParseError::Syntax {
57            line: 10,
58            column: 5,
59            message: "unexpected token".to_string(),
60        };
61        assert!(err.to_string().contains("line 10"));
62        assert!(err.to_string().contains("column 5"));
63    }
64
65    #[test]
66    fn test_emit_error_display() {
67        let err = EmitError::UnsupportedType("CustomType".to_string());
68        assert!(err.to_string().contains("CustomType"));
69    }
70}