Skip to main content

interscript_rs/
error.rs

1//! Error type for parsing and transliteration.
2
3use std::fmt;
4
5#[derive(Debug)]
6pub enum Error {
7    /// Grammar rejected the map source.
8    Parse {
9        map: String,
10        offset: usize,
11        expected: Vec<String>,
12    },
13    /// The map name could not be resolved on the load path.
14    MapNotFound(String),
15    /// A dependency chain closed on itself.
16    DependencyCycle(Vec<String>),
17    /// The map uses a construct this runtime does not implement
18    /// (legacy .imp DSL, rababa/secryst funcalls, reverse runs).
19    Unsupported(String),
20    /// A semantic error the Ruby runtime also rejects
21    /// (MapLogicError / SystemConversionError parity).
22    MapLogic(String),
23    /// An IMF v1 artifact / registry / inference error
24    /// (interscript-models SPEC.md violations included).
25    Model(String),
26    /// Filesystem / IO failure.
27    Io(String),
28}
29
30impl fmt::Display for Error {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        match self {
33            Error::Parse { map, offset, expected } => write!(
34                f,
35                "parse failed in {map} at offset {offset} (expected one of: {})",
36                expected.join(", ")
37            ),
38            Error::MapNotFound(name) => write!(f, "couldn't locate map {name:?}"),
39            Error::DependencyCycle(chain) => {
40                write!(f, "dependency cycle: {}", chain.join(" -> "))
41            }
42            Error::Unsupported(what) => write!(f, "unsupported: {what}"),
43            Error::MapLogic(what) => write!(f, "map logic error: {what}"),
44            Error::Model(what) => write!(f, "model error: {what}"),
45            Error::Io(what) => write!(f, "io error: {what}"),
46        }
47    }
48}
49
50impl std::error::Error for Error {}
51
52impl From<std::io::Error> for Error {
53    fn from(err: std::io::Error) -> Self {
54        Error::Io(err.to_string())
55    }
56}
57
58pub type Result<T> = std::result::Result<T, Error>;