Skip to main content

simple_expressions/types/
error.rs

1use thiserror::Error;
2
3#[derive(Error, Debug)]
4#[non_exhaustive]
5pub enum Error {
6    #[error("unable to resolve variable: {0:?}")]
7    ResolveFailed(String),
8    #[error("variable is not callable")]
9    NotCallable,
10    #[error("type mismatch: {0}")]
11    TypeMismatch(String),
12    #[error("cannot use {type_name} as {target}")]
13    NotCoercible { type_name: String, target: &'static str },
14    #[error("divide by zero")]
15    DivideByZero,
16    /// Integer arithmetic that does not fit in an `i64`. Reported rather than
17    /// wrapped, so a result is never silently wrong.
18    #[error("integer overflow in '{op}'")]
19    IntegerOverflow {
20        /// The operator that overflowed, as written in the source.
21        op: &'static str,
22    },
23    #[error("evaluation failed: {0}")]
24    EvaluationFailed(String),
25    #[error("index out of bounds: {index} (len: {len})")]
26    IndexOutOfBounds { index: i64, len: usize },
27    #[error("not indexable: {0}")]
28    NotIndexable(String),
29    #[error("no such key: {0}")]
30    NoSuchKey(String),
31    #[error("unknown member '{member}' for type {type_name}")]
32    UnknownMember { type_name: String, member: String },
33    /// Positions are into the whole string handed to the entry point, so they stay
34    /// meaningful for an expression interpolated into some larger text.
35    #[error("parse error at line {line}, column {column}: {message}")]
36    ParseError {
37        /// 1-based.
38        line: usize,
39        /// 1-based, in characters.
40        column: usize,
41        /// 0-based byte offset, for callers that want to slice the input.
42        offset: usize,
43        message: String,
44        /// The offending line with a caret under `column`.
45        rendered: String,
46    },
47    #[error("internal parse error: {0}")]
48    InternalParserError(String),
49}
50
51pub type Result<T> = core::result::Result<T, Error>;