Skip to main content

nxs/
error.rs

1use std::fmt;
2
3#[derive(Debug, Clone, PartialEq)]
4pub enum NxsError {
5    BadMagic,
6    UnknownSigil(char),
7    BadEscape(char),
8    OutOfBounds,
9    DictMismatch,
10    CircularLink,
11    RecursionLimit,
12    MacroUnresolved(String),
13    ListTypeMismatch,
14    Overflow,
15    ParseError(String),
16    IoError(String),
17    /// Exit 4 — two records disagree on a key's sigil and policy is `error`.
18    ConvertSchemaConflict(String),
19    /// Exit 3 — malformed JSON/CSV/XML; byte offset is the position in the stream.
20    ConvertParseError {
21        offset: u64,
22        msg: String,
23    },
24    /// Exit 3 — XML entity-expansion attack detected (billion-laughs etc.).
25    ConvertEntityExpansion,
26    /// Exit 3 — nesting depth exceeded `--max-depth` / `--xml-max-depth`.
27    ConvertDepthExceeded,
28}
29
30impl fmt::Display for NxsError {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        match self {
33            NxsError::BadMagic => write!(f, "ERR_BAD_MAGIC"),
34            NxsError::UnknownSigil(c) => write!(f, "ERR_UNKNOWN_SIGIL: '{c}'"),
35            NxsError::BadEscape(c) => write!(f, "ERR_BAD_ESCAPE: '\\{c}'"),
36            NxsError::OutOfBounds => write!(f, "ERR_OUT_OF_BOUNDS"),
37            NxsError::DictMismatch => write!(f, "ERR_DICT_MISMATCH"),
38            NxsError::CircularLink => write!(f, "ERR_CIRCULAR_LINK"),
39            NxsError::RecursionLimit => write!(f, "ERR_RECURSION_LIMIT"),
40            NxsError::MacroUnresolved(s) => write!(f, "ERR_MACRO_UNRESOLVED: {s}"),
41            NxsError::ListTypeMismatch => write!(f, "ERR_LIST_TYPE_MISMATCH"),
42            NxsError::Overflow => write!(f, "ERR_OVERFLOW"),
43            NxsError::ParseError(s) => write!(f, "ParseError: {s}"),
44            NxsError::IoError(s) => write!(f, "IoError: {s}"),
45            NxsError::ConvertSchemaConflict(s) => write!(f, "ERR_SCHEMA_CONFLICT: {s}"),
46            NxsError::ConvertParseError { offset, msg } => {
47                write!(f, "ERR_PARSE_ERROR at byte {offset}: {msg}")
48            }
49            NxsError::ConvertEntityExpansion => write!(f, "ERR_ENTITY_EXPANSION"),
50            NxsError::ConvertDepthExceeded => write!(f, "ERR_DEPTH_EXCEEDED"),
51        }
52    }
53}
54
55impl std::error::Error for NxsError {}
56
57pub type Result<T> = std::result::Result<T, NxsError>;