gnostr_xq/vm/
error.rs

1use std::rc::Rc;
2
3use thiserror::Error;
4
5use crate::{value::RcString, vm::machine::LabelId, Number, Value};
6
7pub type Result<T, E = QueryExecutionError> = std::result::Result<T, E>;
8
9#[derive(Debug, Error)]
10#[error(transparent)]
11pub struct InputError(#[from] Box<dyn std::error::Error + Send + Sync>);
12impl InputError {
13    pub fn new<E: 'static + std::error::Error + Send + Sync>(e: E) -> Self {
14        Self(Box::new(e))
15    }
16}
17
18#[derive(Debug, Error)]
19#[non_exhaustive]
20pub enum QueryExecutionError {
21    #[error("Object was indexed by non-string value `{0:?}`")]
22    ObjectIndexByNonString(Value),
23    #[error("Object was indexed by non-integer value `{0:?}`")]
24    ArrayIndexByNonInt(Value),
25    #[error("Slice on non-array `{0:?}`")]
26    SliceByNonInt(Value),
27    #[error("Cannot iterate over non-iterable value `{0:?}`")]
28    IterateOnNonIterable(Value),
29    #[error("Cannot index on non-indexable value `{0:?}`")]
30    IndexOnNonIndexable(Value),
31    #[error("Slice on not an array nor a string `{0:?}`")]
32    SliceOnNonArrayNorString(Value),
33    #[error("Expected an integer but got a non-integral value `{0:?}`")]
34    NonIntegralNumber(Number),
35    #[error("Unary {0:?} negation was applied to non-numeric value `{1:?}`")]
36    UnaryOnNonNumeric(&'static str, Value),
37    #[error("Cannot {0:?} `{1:?}` and `{2:?}`")]
38    IncompatibleBinaryOperator(&'static str, Value, Value),
39    #[error("Cannot repeat string `{0:?}` times")]
40    StringRepeatByNonUSize(Number),
41    #[error("Cannot divide/modulo by zero")]
42    DivModByZero,
43    #[error("Tried to construct an object with non-string key `{0:?}`")]
44    ObjectNonStringKey(Value),
45    #[error("Invalid path for `{0:?}`")]
46    InvalidPathError(Value),
47    #[error("Breaking on label `{0:?}`")]
48    Breaking(LabelId),
49    #[error("Path should be an array but was `{0:?}`")]
50    PathNotArray(Value),
51    #[error("Invalid index `{0:?}`")]
52    InvalidIndex(Value),
53    #[error("Invalid indexing for value `{0:?}` and index `{0:?}`")]
54    InvalidIndexing(Value, Value),
55    #[error("Expected an array but was `{0:?}`")]
56    ExpectedAnArray(Value),
57    #[error("Expected slicing but got an invalid one `{0:?}`")]
58    InvalidSlicing(Value),
59    #[error("At least one of range start or end has to be specified")]
60    UnboundedRange,
61    #[error("Invalid as base64")]
62    InvalidAsBase64(#[from] base64::DecodeError),
63    #[error("Invalid as a UTF-8-encoded byte array")]
64    InvalidUTF8Bytes(#[from] std::string::FromUtf8Error),
65    #[error("Invalid as a unicode scalar value `{0:}`")]
66    InvalidNumberAsChar(Number),
67    #[error("utf8bytelength is applied to non-string value`{0:?}`")]
68    InvalidUTF8ByteLength(Value),
69    #[error("{0:?} was called invalidly with arg `{1:?}`")]
70    InvalidArgType(&'static str, Value),
71    #[error("`{0:?}` can't be parsed as a number")]
72    InvalidStringToNumber(RcString),
73    #[error("Given string was invalid as a json `{0:}`")]
74    InvalidJson(RcString),
75    #[error("Length mismatch for `{0:?}`")]
76    LengthMismatch(&'static str),
77    #[error("Unknown string formatter `{0:?}`")]
78    UnknownStringFormatter(RcString),
79    #[error("Unable to parse date time")]
80    DateTimeParseError(RcString),
81    #[error("Unable to format date time")]
82    DateTimeFormatError(RcString),
83    #[error("Invalid date time")]
84    InvalidDateTime(#[from] time::error::ComponentRange),
85    #[error("Unable to determine local date time offset")]
86    IndeterminateOffset(#[from] time::error::IndeterminateOffset),
87    #[error("Unable to determine local time zone")]
88    TimeZoneLookupFailure(RcString),
89    #[error("Failed to compile regex: {0:?}")]
90    OnigurumaCompileError(#[from] onig::Error),
91    #[error("Invalid regex flag: {0}")]
92    InvalidRegexFlag(char),
93    #[error("Input source gave an error")]
94    InputError(#[from] InputError),
95    #[error("There's no more input")]
96    NoMoreInputError,
97    #[error("{0:?}")]
98    UserDefinedError(Value),
99}
100
101impl From<time_fmt::parse::ParseError> for QueryExecutionError {
102    fn from(e: time_fmt::parse::ParseError) -> Self {
103        Self::DateTimeParseError(Rc::new(format!("{e}")))
104    }
105}
106
107impl From<time_fmt::format::FormatError> for QueryExecutionError {
108    fn from(e: time_fmt::format::FormatError) -> Self {
109        Self::DateTimeParseError(Rc::new(format!("{e}")))
110    }
111}
112
113impl From<time::error::Parse> for QueryExecutionError {
114    fn from(e: time::error::Parse) -> Self {
115        Self::DateTimeParseError(Rc::new(format!("{e}")))
116    }
117}
118
119impl From<time_tz::system::Error> for QueryExecutionError {
120    fn from(e: time_tz::system::Error) -> Self {
121        Self::TimeZoneLookupFailure(Rc::new(format!("{e}")))
122    }
123}