python/
character.rs

1use std::fmt;
2
3
4use crate::_Object;
5
6
7#[derive(Copy)]
8#[derive(Clone)]
9#[derive(Eq)]
10#[derive(PartialEq)]
11#[derive(Hash)]
12/// Char struct that handles rust char
13pub struct Char {
14    _char: char,
15}
16
17impl Char {
18    /// constructor creates a new Char from rust char
19    /// note: its not like from, because from function
20    /// creates something from something different than what we get
21    pub fn new(_char: char) -> Self {
22        Char {
23            _char,
24        }
25    }
26}
27
28impl From<char> for Char {
29    fn from(_char: char) -> Self {
30        Char {
31            _char,
32        }
33    }
34}
35
36impl From<u8> for Char {
37    fn from(_int: u8) -> Self {
38        Char {
39            _char: _int as char,
40        }
41    }
42}
43
44impl Default for Char {
45    fn default() -> Self {
46        Char {
47            _char: '0'
48        }
49    }
50}
51
52impl _Object for Char {
53    fn __str__(&self) -> String {
54        String::from(self._char)
55    }
56
57    fn __repr__(&self) -> String {
58        format!("'{}'", self._char)
59    }
60}
61
62impl fmt::Display for Char {
63    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
64        if formatter.alternate() {
65            write!(formatter, "'{}'", self._char)
66        } else {
67            write!(formatter, "{}", self._char)
68        }
69    }
70}
71
72impl fmt::Debug for Char {
73    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
74        if formatter.alternate() {
75            write!(formatter, "Char<'{}'>", self._char)
76        } else {
77            write!(formatter, "Char<{}>", self._char)
78        }
79    }
80}
81
82impl PartialEq<char> for Char {
83    fn eq(&self, other: &char) -> bool {
84        self._char == *other
85    }
86}