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
use bitflags::bitflags;

bitflags! {
    #[derive(Debug, Clone, Copy)]
    pub struct Context: u8 {
        /// [In]
        const In          = 1 << 0;
        const FORBID_CALL = 1 << 1;
    }
}

impl Default for Context {
    fn default() -> Self {
        Self::In
    }
}

impl Context {
    #[inline]
    pub fn has_in(self) -> bool {
        self.contains(Self::In)
    }

    #[inline]
    pub fn has_forbid_call(self) -> bool {
        self.contains(Self::FORBID_CALL)
    }

    #[inline]
    #[must_use]
    pub fn and_in(self, include: bool) -> Self {
        self.and(Self::In, include)
    }

    #[inline]
    #[must_use]
    pub fn and_forbid_call(self, include: bool) -> Self {
        self.and(Self::FORBID_CALL, include)
    }

    #[inline]
    fn and(self, flag: Self, set: bool) -> Self {
        if set {
            self | flag
        } else {
            self - flag
        }
    }

    #[inline]
    pub(crate) fn union_in_if(self, include: bool) -> Self {
        self.union_if(Self::In, include)
    }

    #[inline]
    fn union_if(self, other: Self, include: bool) -> Self {
        if include {
            self.union(other)
        } else {
            self
        }
    }
}