casbin/
macros.rs

1#[macro_export]
2macro_rules! get_or_err {
3    ($this:ident, $key:expr, $err:expr, $msg:expr) => {{
4        $this
5            .get_model()
6            .get_model()
7            .get($key)
8            .ok_or_else(|| {
9                $crate::error::Error::from($err(format!(
10                    "Missing {} definition in conf file",
11                    $msg
12                )))
13            })?
14            .get($key)
15            .ok_or_else(|| {
16                $crate::error::Error::from($err(format!(
17                    "Missing {} section in conf file",
18                    $msg
19                )))
20            })?
21    }};
22}
23
24#[macro_export]
25macro_rules! get_or_err_with_context {
26    ($this:ident, $key:expr, $ctx:expr, $err:expr, $msg:expr) => {{
27        $this
28            .get_model()
29            .get_model()
30            .get($key)
31            .ok_or_else(|| {
32                $crate::error::Error::from($err(format!(
33                    "Missing {} definition in conf file",
34                    $msg
35                )))
36            })?
37            .get($ctx)
38            .ok_or_else(|| {
39                $crate::error::Error::from($err(format!(
40                    "Missing {} section in conf file",
41                    $msg
42                )))
43            })?
44    }};
45}
46
47#[macro_export]
48macro_rules! register_g_function {
49    ($enforcer:ident, $fname:ident, $ast:ident) => {{
50        let rm = Arc::clone(&$enforcer.rm);
51        let count = $ast.value.matches('_').count();
52
53        if count == 2 {
54            $enforcer.engine.register_fn(
55                $fname,
56                move |arg1: ImmutableString, arg2: ImmutableString| {
57                    rm.read().has_link(&arg1, &arg2, None)
58                },
59            );
60        } else if count == 3 {
61            $enforcer.engine.register_fn(
62                $fname,
63                move |arg1: ImmutableString,
64                      arg2: ImmutableString,
65                      arg3: ImmutableString| {
66                    rm.read().has_link(&arg1, &arg2, Some(&arg3))
67                },
68            );
69        } else {
70            return Err(ModelError::P(
71                r#"the number of "_" in role definition should be at least 2"#
72                    .to_owned(),
73            )
74            .into());
75        }
76    }};
77}
78
79#[macro_export]
80macro_rules! push_index_if_explain {
81    ($this:ident) => {{
82        #[cfg(feature = "explain")]
83        if $this.cap > 0 {
84            $this.expl.push($this.idx);
85        }
86    }};
87}