Skip to main content

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 { position: usize, message: String },
15
16    #[error("Invalid entity reference: #{0}")]
17    InvalidEntityRef(u32),
18
19    #[error("Invalid IFC type: {0}")]
20    InvalidIfcType(String),
21
22    #[error("Unexpected token at position {position}: expected {expected}, got {got}")]
23    UnexpectedToken {
24        position: usize,
25        expected: String,
26        got: String,
27    },
28
29    #[error("IO error: {0}")]
30    Io(#[from] std::io::Error),
31
32    #[error("UTF-8 error: {0}")]
33    Utf8(#[from] std::str::Utf8Error),
34}
35
36impl Error {
37    pub fn parse(position: usize, message: impl Into<String>) -> Self {
38        Self::ParseError {
39            position,
40            message: message.into(),
41        }
42    }
43
44    pub fn unexpected(
45        position: usize,
46        expected: impl Into<String>,
47        got: impl Into<String>,
48    ) -> Self {
49        Self::UnexpectedToken {
50            position,
51            expected: expected.into(),
52            got: got.into(),
53        }
54    }
55}