1use thiserror::Error as ThisError;
2use std::{fmt, result::Result as StdResult, num::ParseIntError};
3
4#[derive(Debug, ThisError)]
5pub enum Error {
6 #[error("Path index out of range when using wildcard. Index={idx};Length={len};")]
7 PathIndexOutOfRange { idx: usize, len: usize },
8 #[error("Match index out of range when using wildcard. Index={idx};Length={len};")]
9 MatchIndexOutOfRange { idx: usize, len: usize },
10 #[error("Unexpected end of right hand side expression.")]
11 UnexpectedEndOfRhs,
12 #[error("Unexpected right hand side expression.")]
13 UnexpectedRhsEntry,
14 #[error("Unexpected object in right hand side.")]
15 UnexpectedObjectInRhs,
16 #[error("Not implemented yet.")]
17 Todo,
18 #[error("Invalid index in expression. Length={len};\n{source}")]
21 InvalidIndex {
22 len: usize,
23 #[source]
24 source: ParseIntError,
25 },
26 #[error("Array index out of range. Index={idx};Length={len};")]
27 ArrIndexOutOfRange { idx: usize, len: usize },
28 #[error("Array index would pad the output with too many null slots. Index={idx};Length={len};Used={used};Limit={max} padded slots per shift;")]
29 ArrayPaddingLimit {
30 idx: usize,
31 len: usize,
32 used: usize,
33 max: usize,
34 },
35 #[error(
36 "Modifier run copied or wrote too much data. Used={used};Limit={max} estimated bytes per run;"
37 )]
38 ModifierBudgetLimit { used: usize, max: usize },
39 #[error(
42 "Shift run copied too much data from the input. Used={used};Limit={max} estimated bytes per run;"
43 )]
44 ShiftBudgetLimit { used: usize, max: usize },
45 #[error("Array index string is too long. Length={len};Max={max};")]
46 IndexTooLong { len: usize, max: usize },
47 #[error("Json value can't be used as an index: {kind}")]
50 InvalidIndexVal { kind: &'static str },
51 #[error("Key not found in object:{0}")]
52 KeyNotFound(MissingKey),
53 #[error("Expression didn't evaluate to a string.")]
54 EvalString,
55 #[error("Empty path while executing shift. THIS SHOULD NEVER HAPPEN.")]
56 ShiftEmptyPath,
57 #[error("Path is not empty after executing shift. THIS SHOULD NEVER HAPPEN.")]
58 ShiftPathNotEmpty,
59 #[error("Parse error: {0}")]
60 ParseError(String),
61 #[error("Invalid function: {0}")]
62 InvalidFunction(String),
63 #[error("Function argument error: {0}")]
64 FunctionArgumentError(String),
65 #[error("Path evaluation error: {0}")]
66 PathEvaluationError(String),
67}
68
69pub type Result<T> = StdResult<T, Error>;
70
71#[derive(Debug, Clone, PartialEq, Eq)]
77pub enum MissingKey {
78 Spec(String),
79 FromInput { len: usize },
80}
81
82impl fmt::Display for MissingKey {
83 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
84 match self {
85 MissingKey::Spec(key) => f.write_str(key),
86 MissingKey::FromInput { len } => write!(f, "<key from input, {len} bytes>"),
87 }
88 }
89}
90
91pub(crate) fn json_kind(v: &serde_json::Value) -> &'static str {
93 use serde_json::Value;
94
95 match v {
96 Value::Null => "null",
97 Value::Bool(_) => "boolean",
98 Value::Number(_) => "number",
99 Value::String(_) => "string",
100 Value::Array(_) => "array",
101 Value::Object(_) => "object",
102 }
103}