yini_rs/
error.rs

1//! Error types for the YINI parser.
2
3use thiserror::Error;
4
5/// Result type alias for YINI operations.
6pub type Result<T> = std::result::Result<T, Error>;
7
8// C++ style aliases for compatibility
9pub type ParseError = Error;
10pub type FileError = Error;
11
12/// Error types that can occur during YINI parsing and operations.
13#[derive(Error, Debug)]
14pub enum Error {
15    /// Error that occurs during parsing of YINI content (equivalent to yini::ParseError).
16    #[error("YINI Parse Error: {message}")]
17    ParseError { message: String },
18
19    /// Error that occurs during file I/O operations (equivalent to yini::FileError).
20    #[error("YINI File Error: {message}")]
21    FileError { message: String },
22
23    /// Error that occurs when trying to access a value with wrong type.
24    #[error("Type conversion error: {message}")]
25    TypeError { message: String },
26
27    /// Error that occurs when trying to access a non-existent key or section.
28    #[error("Key not found: {key}")]
29    KeyNotFound { key: String },
30
31    /// Error that occurs when trying to access a non-existent section.
32    #[error("Section not found: {name}")]
33    SectionNotFound { name: String },
34}
35
36impl Error {
37    /// Create a new parse error (matches yini::ParseError).
38    pub fn parse_error(message: impl Into<String>) -> Self {
39        Self::ParseError {
40            message: message.into(),
41        }
42    }
43
44    /// Create a new file error (matches yini::FileError).
45    pub fn file_error(message: impl Into<String>) -> Self {
46        Self::FileError {
47            message: message.into(),
48        }
49    }
50
51    /// Create a new type conversion error.
52    pub fn type_error(message: impl Into<String>) -> Self {
53        Self::TypeError {
54            message: message.into(),
55        }
56    }
57
58    /// Create a new key not found error.
59    pub fn key_not_found(key: impl Into<String>) -> Self {
60        Self::KeyNotFound { key: key.into() }
61    }
62
63    /// Create a new section not found error.
64    pub fn section_not_found(name: impl Into<String>) -> Self {
65        Self::SectionNotFound { name: name.into() }
66    }
67}
68
69impl From<std::io::Error> for Error {
70    fn from(err: std::io::Error) -> Self {
71        Self::FileError {
72            message: err.to_string(),
73        }
74    }
75}