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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//! A boolean role expression that can be evaluated.

///  A `RoleExpr` is a boolean expression tree.
///
/// It is typically parsed by the [RoleExprParser](crate::role_expr_parser::RoleExprParser).
/// However it can also be build programmatically.
///
/// Here an example for the expression `admin or power_user`:
/// ```rust
/// use toql_core::role_expr::RoleExpr;
///
/// let e = RoleExpr::Role("admin".to_string())
///                .or(RoleExpr::Role("power_user".to_string()));
/// assert_eq!("(`admin`); (`power_user`)", e.to_string());
/// ```
/// Notice that the string representation of OR always comes with parenthesis to express logical priority.
/// To validate a role expression use the [RoleValidator](crate::role_validator::RoleValidator).
///
/// `RoleExpr` are used by Toql derive generated code.
///
/// End users should restrict actions with role expressions through the Toql derive.
///
/// ### Example
/// Restricting the field's selection to the roles 'admin' or 'power_user'
/// ```rust, ignore
/// use toql_derive::Toql;
///
/// #[derive(Toql)]
/// struct FooBar {
///   #[toql(key)]
///   id: u64,
///
///   #[toql(roles(load="admin;power_user"))]
///   name: Option<String>
/// }
/// ```
///
#[derive(Debug, Clone)]
pub enum RoleExpr {
    /// Concatenate both nodes with AND
    And(Box<RoleExpr>, Box<RoleExpr>),
    /// Concatenate both nodes with OR
    Or(Box<RoleExpr>, Box<RoleExpr>),
    /// Negate node
    Not(Box<RoleExpr>),
    /// This node is a role name
    Role(String),
    /// This node is always invalid
    Invalid,
}

impl RoleExpr {
    // Create a role expression that is always invalid
    pub fn invalid() -> Self {
        RoleExpr::Invalid
    }
    // Create a role for the given name
    pub fn role(role: String) -> Self {
        RoleExpr::Role(role)
    }
    // Concatenate this role and another role with AND
    pub fn and(self, role_expr: RoleExpr) -> Self {
        RoleExpr::And(Box::new(self), Box::new(role_expr))
    }
    // Concatenate this role and another role with OR
    pub fn or(self, role_expr: RoleExpr) -> Self {
        RoleExpr::Or(Box::new(self), Box::new(role_expr))
    }
    // Negate this role expression
    #[allow(clippy::clippy::should_implement_trait)]
    pub fn not(self) -> Self {
        RoleExpr::Not(Box::new(self))
    }
}

impl ToString for RoleExpr {
    fn to_string(&self) -> String {
        match self {
            RoleExpr::And(a, b) => {
                format!("{}, {}", a.to_string(), b.to_string())
            }
            RoleExpr::Or(a, b) => {
                format!("({}); ({})", a.to_string(), b.to_string())
            }
            RoleExpr::Not(a) => format!("!{}", a.to_string()),
            RoleExpr::Role(r) => format!("{}", r.to_string()),
            RoleExpr::Invalid => "0".to_string(),
        }
    }
}

#[cfg(test)]
mod test {
    use super::RoleExpr;

    #[test]
    fn build() {
        let r1 = RoleExpr::role("role1".to_string());
        assert_eq!(r1.to_string(), "role1");

        let r2 = RoleExpr::role("role2".to_string());
        assert_eq!(r1.clone().and(r2.clone()).to_string(), "role1, role2");

        assert_eq!(r1.clone().or(r2.clone()).to_string(), "(role1); (role2)");

        assert_eq!(
            r1.clone().or(r2.clone().not()).to_string(),
            "(role1); (!role2)"
        );

        assert_eq!(
            r1.clone().or(r2.and(r1.clone())).to_string(),
            "(role1); (role2, role1)"
        );

        assert_eq!(r1.clone().and(RoleExpr::invalid()).to_string(), "role1, 0");
    }
}