ifc_lite_core/
error.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5use thiserror::Error;
6
7/// Result type for IFC-Lite core operations
8pub type Result<T> = std::result::Result<T, Error>;
9
10/// Errors that can occur during IFC parsing
11#[derive(Error, Debug)]
12pub enum Error {
13    #[error("Parse error at position {position}: {message}")]
14    ParseError {
15        position: usize,
16        message: String,
17    },
18
19    #[error("Invalid entity reference: #{0}")]
20    InvalidEntityRef(u32),
21
22    #[error("Invalid IFC type: {0}")]
23    InvalidIfcType(String),
24
25    #[error("Unexpected token at position {position}: expected {expected}, got {got}")]
26    UnexpectedToken {
27        position: usize,
28        expected: String,
29        got: String,
30    },
31
32    #[error("IO error: {0}")]
33    Io(#[from] std::io::Error),
34
35    #[error("UTF-8 error: {0}")]
36    Utf8(#[from] std::str::Utf8Error),
37}
38
39impl Error {
40    pub fn parse(position: usize, message: impl Into<String>) -> Self {
41        Self::ParseError {
42            position,
43            message: message.into(),
44        }
45    }
46
47    pub fn unexpected(position: usize, expected: impl Into<String>, got: impl Into<String>) -> Self {
48        Self::UnexpectedToken {
49            position,
50            expected: expected.into(),
51            got: got.into(),
52        }
53    }
54}