vrl/compiler/value/
error.rs

1use super::Kind;
2use crate::compiler::ExpressionError;
3use crate::diagnostic::DiagnosticMessage;
4use crate::prelude::ValueError::OutOfRange;
5
6#[allow(clippy::module_name_repetitions)]
7#[derive(thiserror::Error, Debug, PartialEq, Eq)]
8pub enum ValueError {
9    #[error(
10        "expected {}, got {got}",
11        .expected
12    )]
13    Expected { got: Kind, expected: Kind },
14
15    #[error("can't coerce {0} into {1}")]
16    Coerce(Kind, Kind),
17
18    #[error("can't calculate remainder of type {0} and {1}")]
19    Rem(Kind, Kind),
20
21    #[error("can't multiply type {0} by {1}")]
22    Mul(Kind, Kind),
23
24    #[error("can't divide type {0} by {1}")]
25    Div(Kind, Kind),
26
27    #[error("can't divide by zero")]
28    DivideByZero,
29
30    #[error("floats can't be NaN")]
31    NanFloat,
32
33    #[error("can't add type {1} to {0}")]
34    Add(Kind, Kind),
35
36    #[error("can't subtract type {1} from {0}")]
37    Sub(Kind, Kind),
38
39    #[error("can't apply an OR to these types - {0}")]
40    Or(#[from] ExpressionError),
41
42    #[error("can't apply an AND to types {0} and {1}")]
43    And(Kind, Kind),
44
45    #[error("can't compare {0} > {1}")]
46    Gt(Kind, Kind),
47
48    #[error("can't compare {0} >= {1}")]
49    Ge(Kind, Kind),
50
51    #[error("can't compare {0} < {1}")]
52    Lt(Kind, Kind),
53
54    #[error("can't compare {0} <= {1}")]
55    Le(Kind, Kind),
56
57    #[error("can't merge type {1} into {0}")]
58    Merge(Kind, Kind),
59
60    #[error("can't convert out of range {0}")]
61    OutOfRange(Kind),
62}
63
64impl DiagnosticMessage for ValueError {
65    fn code(&self) -> usize {
66        use ValueError::{
67            Add, And, Coerce, Div, DivideByZero, Expected, Ge, Gt, Le, Lt, Merge, Mul, NanFloat,
68            Or, Rem, Sub,
69        };
70
71        match self {
72            Expected { .. } => 300,
73            Coerce(..) => 301,
74            Rem(..) => 302,
75            Mul(..) => 303,
76            Div(..) => 304,
77            DivideByZero => 305,
78            NanFloat => 306,
79            Add(..) => 307,
80            Sub(..) => 308,
81            Or(..) => 309,
82            And(..) => 310,
83            Gt(..) => 311,
84            Ge(..) => 312,
85            Lt(..) => 313,
86            Le(..) => 314,
87            Merge(..) => 315,
88            OutOfRange(..) => 316,
89        }
90    }
91}
92
93impl From<ValueError> for ExpressionError {
94    fn from(err: ValueError) -> Self {
95        Self::Error {
96            message: err.message(),
97            labels: vec![],
98            notes: vec![],
99        }
100    }
101}