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
/// Bool is a wrapper for i32 in the context of an R's tristate boolean.
/// It can be TRUE, FALSE or NA_LOGICAL.
#[derive(PartialEq, Eq)]
pub struct Bool(pub i32);

impl Bool {
    /// Convert this Bool to a bool. Note NA will be true.
    pub fn to_bool(&self) -> bool {
        self.0 != 0
    }

    /// Convert this construct a Bool from a rust boolean.
    pub fn from_bool(val: bool) -> Self {
        Bool(val as i32)
    }

    /// Test if TRUE
    pub fn is_true(&self) -> bool {
        self.0 == 1
    }

    /// Test if FALSE
    pub fn is_false(&self) -> bool {
        self.0 == 0
    }
}

impl Clone for Bool {
    fn clone(&self) -> Self {
        Self(self.0)
    }
}

impl Copy for Bool {}

impl From<i32> for Bool {
    fn from(v: i32) -> Self {
        Self(v)
    }
}

impl From<bool> for Bool {
    fn from(v: bool) -> Self {
        Self(v as i32)
    }
}

impl From<Bool> for bool {
    fn from(v: Bool) -> Self {
        v.0 != 0
    }
}

impl From<&Bool> for bool {
    fn from(v: &Bool) -> Self {
        v.0 != 0
    }
}

impl std::fmt::Debug for Bool {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Bool(0) => write!(f, "FALSE"),
            Bool(1) => write!(f, "TRUE"),
            Bool(std::i32::MIN) => write!(f, "NA_LOGICAL"),
            _ => write!(f, "Bool({})", self.0),
        }
    }
}