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
use std::convert;
use std::ops;
use std::fmt;
use std::cmp;


/// This type is for convenience. Because the Todoist API specifies some fields as integers,
/// but their value is always boolean this type will serialize and deserialize as an `isize`, 
/// while converting to and from both an isize and bool.
#[derive(Serialize, Deserialize, Default, Clone)]
pub struct IntBool(isize);


impl convert::From<bool> for IntBool {
    fn from(v : bool) -> IntBool {
        IntBool(match v {
            true => 1,
            false => 0,
        })
    }
}

impl convert::From<isize> for IntBool {
    fn from(v : isize) -> IntBool {
        IntBool(v)
    }
}

impl convert::Into<bool> for IntBool {
    fn into(self) -> bool {
        match self {
            IntBool(0) => false,
            IntBool(_) => true,
        }
    }
}


impl convert::Into<isize> for IntBool {
    fn into(self) -> isize {
        self.0
    }
}

impl fmt::Debug for IntBool {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}",
            if self.0 == 0 {
                "false"
            } else {
                "true"
            }
        )
    }
}

impl cmp::PartialEq<bool> for IntBool {
    fn eq(&self, v : &bool) -> bool {
        *v as isize == self.0
    }
}


impl cmp::PartialEq<IntBool> for IntBool {
    fn eq(&self, v : &IntBool) -> bool {
        v.0 == self.0
    }
}

impl cmp::PartialEq<isize> for IntBool {
    fn eq(&self, v : &isize) -> bool {
        *v == self.0
    }
}

impl ops::Not for IntBool {
    type Output = bool;

    fn not(self) -> bool {
        if self.0 == 0 {
            true
        } else {
            false
        }
    }
}