1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use std::fmt::{Display, Formatter};

use thiserror::Error;

use crate::parser::CompareOp;

#[derive(Debug, Error)]
pub enum Error {
    #[error(transparent)]
    Parser(#[from] nom::error::Error<String>),

    #[error(transparent)]
    SerializationError(#[from] serde_json::Error),

    #[error(
        "the filter has a wrong format, after parsing the input \"{0}\", the part \"{1}\" remains"
    )]
    WrongFilterFormat(String, String),

    #[error("The applied filter is invalid")]
    InvalidFilter,

    #[error("The resource has an invalid format, that can't be considered in a filter")]
    InvalidResource,

    #[error("You tried applying the operator {0} on a value of type {1}, which is impossible")]
    WrongOperator(CompareOp, String),

    #[error("I tried parsing a boolean from the resource, but got {0} which seems to be wrong.")]
    MalformedBoolean(String),

    #[error("I tried parsing a number from the resource, but got {0} which seems to be wrong.")]
    MalformedNumber(String),

    #[error("I tried parsing a string from the resource, but got {0} which seems to be wrong.")]
    MalformedString(String),

    #[error("I tried parsing a datetime from the resource, but got {0} which seems to be wrong. Format should be in rfc3339 format, something like \"2011-05-13T04:42:34Z\"")]
    MalformedDatetime(String),

    #[error("The resource value extracted from the attribute name given is not a valid value. Careful! Valid values are strings, numbers, boolean and null. Arrays and Objects are not.")]
    InvalidComparisonValue(String),
}

impl Error {
    pub fn wrong_operator(compare_op: &CompareOp, resource: impl ToString) -> Self {
        Self::WrongOperator(*compare_op, resource.to_string())
    }
}

impl Display for CompareOp {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let compare_operator_string = match self {
            CompareOp::Equal => "eq (equal)",
            CompareOp::NotEqual => "ne (not equal)",
            CompareOp::Contains => "co (contains)",
            CompareOp::StartsWith => "sw (starts with)",
            CompareOp::EndsWith => "ew (ends with)",
            CompareOp::GreaterThan => "gt (greater than)",
            CompareOp::GreaterThanOrEqual => "ge (greater than or equal)",
            CompareOp::LessThan => "lt (less than)",
            CompareOp::LessThanOrEqual => "le (less than or equal)",
        };
        write!(f, "{}", compare_operator_string)
    }
}