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
#[macro_export]
macro_rules! get_or_err {
    ($this:ident, $key:expr, $err:expr, $msg:expr) => {{
        $this
            .get_model()
            .get_model()
            .get($key)
            .ok_or_else(|| {
                $crate::error::Error::from($err(format!(
                    "Missing {} definition in conf file",
                    $msg
                )))
            })?
            .get($key)
            .ok_or_else(|| {
                $crate::error::Error::from($err(format!(
                    "Missing {} section in conf file",
                    $msg
                )))
            })?
    }};
}

#[macro_export]
macro_rules! register_g_function {
    ($enforcer:ident, $fname:ident, $ast:ident) => {{
        let rm = Arc::clone(&$enforcer.rm);
        let count = $ast.value.matches('_').count();

        if count == 2 {
            $enforcer.engine.register_fn(
                $fname,
                move |arg1: ImmutableString, arg2: ImmutableString| {
                    rm.write().unwrap().has_link(&arg1, &arg2, None)
                },
            );
        } else if count == 3 {
            $enforcer.engine.register_fn(
                $fname,
                move |arg1: ImmutableString,
                      arg2: ImmutableString,
                      arg3: ImmutableString| {
                    rm.write().unwrap().has_link(&arg1, &arg2, Some(&arg3))
                },
            );
        } else {
            return Err(ModelError::P(
                r#"the number of "_" in role definition should be at least 2"#
                    .to_owned(),
            )
            .into());
        }
    }};
}

#[macro_export]
macro_rules! push_index_if_explain {
    ($this:ident) => {{
        #[cfg(feature = "explain")]
        if $this.cap > 1 {
            $this.expl.push($this.idx);
        }
    }};
}