python/
boolean.rs

1use std::fmt;
2
3use crate::_Object;
4
5
6/// Bool structure for True and False
7#[derive(Copy)]
8#[derive(Clone)]
9#[derive(Default)]
10#[derive(PartialEq)]
11#[derive(Hash)]
12#[derive(Eq)]
13pub struct Bool {
14    _bool: bool,
15}
16
17impl Bool {
18    /// create a new bool
19    /// example
20    /// let boolean = Bool::new(true)
21    /// let boolean = Bool::new(false)
22    pub fn new(_bool: bool) -> Bool {
23        Bool {
24            _bool,
25        }
26    }
27}
28
29
30impl From<bool> for Bool {
31    fn from(_bool: bool) -> Self {
32        Bool {
33            _bool,
34        }
35    }
36}
37
38
39impl From<i32> for Bool {
40    fn from(_int: i32) -> Self {
41        if _int > 0 {
42            return Bool {
43                _bool: true
44            };
45        }
46        Bool {
47            _bool: false
48        }
49    }
50}
51
52
53impl _Object for Bool {
54    fn __str__(&self) -> String {
55        if self._bool {
56            format!("True")
57        } else {
58            format!("False")
59        }
60    }
61
62    fn __repr__(&self) -> String {
63        self.__str__()
64    }
65}
66
67
68impl fmt::Display for Bool {
69    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70        write!(f, "{}", self.__str__())
71    }
72}
73
74
75impl fmt::Debug for Bool {
76    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
77        write!(f, "Bool<{}>", self.__str__())
78    }
79}
80
81
82impl PartialEq<bool> for Bool {
83    fn eq(&self, other: &bool) -> bool {
84        self._bool == *other
85    }
86}