Skip to main content

layer_tl_parser/
errors.rs

1use std::fmt;
2use std::num::ParseIntError;
3
4/// Errors produced while parsing a single parameter token.
5#[derive(Clone, Debug, PartialEq)]
6pub enum ParamParseError {
7    /// An empty string was encountered where a name/type was expected.
8    Empty,
9    /// A `{X:Type}` generic type definition (not a real error; used as a signal).
10    TypeDef {
11        /// The name of the generic type parameter (e.g. `"X"` from `{X:Type}`).
12        name: String,
13    },
14    /// A `{…}` block that isn't a valid type definition.
15    MissingDef,
16    /// A flag expression (`name.N?Type`) was malformed.
17    InvalidFlag,
18    /// A generic `<…>` argument was malformed (missing closing `>`).
19    InvalidGeneric,
20    /// A bare `name` with no `:type` — e.g. old-style `? = Int`.
21    NotImplemented,
22}
23
24impl fmt::Display for ParamParseError {
25    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26        match self {
27            Self::Empty => write!(f, "empty token"),
28            Self::TypeDef { name } => write!(f, "generic type definition: {name}"),
29            Self::MissingDef => write!(f, "unknown generic or flag definition"),
30            Self::InvalidFlag => write!(f, "invalid flag expression"),
31            Self::InvalidGeneric => write!(f, "invalid generic argument (unclosed `<`)"),
32            Self::NotImplemented => write!(f, "parameter without `:type` is not supported"),
33        }
34    }
35}
36
37impl std::error::Error for ParamParseError {}
38
39/// Errors produced while parsing a complete TL definition.
40#[derive(Debug, PartialEq)]
41pub enum ParseError {
42    /// The input was blank.
43    Empty,
44    /// No `= Type` was found.
45    MissingType,
46    /// The name (before `=`) was missing or had empty namespace components.
47    MissingName,
48    /// The `#id` hex literal was unparseable.
49    InvalidId(ParseIntError),
50    /// A parameter was invalid.
51    InvalidParam(ParamParseError),
52    /// The definition uses a syntax we don't support yet.
53    NotImplemented,
54}
55
56impl fmt::Display for ParseError {
57    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58        match self {
59            Self::Empty => write!(f, "empty definition"),
60            Self::MissingType => write!(f, "missing `= Type`"),
61            Self::MissingName => write!(f, "missing or malformed name"),
62            Self::InvalidId(e) => write!(f, "invalid constructor ID: {e}"),
63            Self::InvalidParam(e) => write!(f, "invalid parameter: {e}"),
64            Self::NotImplemented => write!(f, "unsupported TL syntax"),
65        }
66    }
67}
68
69impl std::error::Error for ParseError {
70    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
71        match self {
72            Self::InvalidId(e) => Some(e),
73            Self::InvalidParam(e) => Some(e),
74            _ => None,
75        }
76    }
77}