scim_filter/
error.rs

1use std::fmt::{Display, Formatter};
2
3use thiserror::Error;
4
5use crate::parser::CompareOp;
6
7#[derive(Debug, Error)]
8pub enum Error {
9    #[error(transparent)]
10    Parser(#[from] nom::error::Error<String>),
11
12    #[error(transparent)]
13    SerializationError(#[from] serde_json::Error),
14
15    #[error(
16        "the filter has a wrong format, after parsing the input \"{0}\", the part \"{1}\" remains"
17    )]
18    WrongFilterFormat(String, String),
19
20    #[error("The applied filter is invalid")]
21    InvalidFilter,
22
23    #[error("The resource has an invalid format, that can't be considered in a filter")]
24    InvalidResource,
25
26    #[error("You tried applying the operator {0} on a value of type {1}, which is impossible")]
27    WrongOperator(CompareOp, String),
28
29    #[error("I tried parsing a boolean from the resource, but got {0} which seems to be wrong.")]
30    MalformedBoolean(String),
31
32    #[error("I tried parsing a number from the resource, but got {0} which seems to be wrong.")]
33    MalformedNumber(String),
34
35    #[error("I tried parsing a string from the resource, but got {0} which seems to be wrong.")]
36    MalformedString(String),
37
38    #[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\"")]
39    MalformedDatetime(String),
40
41    #[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.")]
42    InvalidComparisonValue(String),
43}
44
45impl Error {
46    pub fn wrong_operator(compare_op: &CompareOp, resource: impl ToString) -> Self {
47        Self::WrongOperator(*compare_op, resource.to_string())
48    }
49}
50
51impl Display for CompareOp {
52    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
53        let compare_operator_string = match self {
54            CompareOp::Equal => "eq (equal)",
55            CompareOp::NotEqual => "ne (not equal)",
56            CompareOp::Contains => "co (contains)",
57            CompareOp::StartsWith => "sw (starts with)",
58            CompareOp::EndsWith => "ew (ends with)",
59            CompareOp::GreaterThan => "gt (greater than)",
60            CompareOp::GreaterThanOrEqual => "ge (greater than or equal)",
61            CompareOp::LessThan => "lt (less than)",
62            CompareOp::LessThanOrEqual => "le (less than or equal)",
63        };
64        write!(f, "{}", compare_operator_string)
65    }
66}