devela/text/char/
core_impls.rs

1// devela::text::char::core_impls
2//
3//! implementations of core traits
4//
5
6#[allow(unused_imports)]
7use crate::{DataOverflow, _core::fmt, paste, text::char::*, unwrap, ConstDefault};
8
9/* Default, Display, Debug */
10
11///
12macro_rules! char_core_impls {
13    () => {
14        char_core_impls![
15            char7 | "_char7" + Self(unwrap![some NonExtremeU8::new(0)]),
16            char8 | "_char8" + Self(0),
17            char16 | "_char16" + Self(unwrap![some NonSurrogateU16::new(0)])
18        ];
19    };
20    ($( $name:ident | $feature:literal + $default:expr ),+ ) => {
21        $(
22            #[cfg(feature = $feature)]
23            char_core_impls![@$name + $default];
24        )+
25    };
26    (@$name:ident + $default:expr) => { paste! {
27        impl Default for super::$name {
28            /// Returns the default value of `\x00` (nul character).
29            #[must_use]
30            fn default() -> Self { $default }
31        }
32        impl ConstDefault for super::$name {
33            /// Returns the default value of `\x00` (nul character).
34            const DEFAULT: Self = $default;
35        }
36        impl fmt::Display for super::$name {
37            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38                write!(f, "{}", self.to_char())
39            }
40        }
41        impl fmt::Debug for super::$name {
42            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43                write!(f, "{:?}", self.to_char())
44            }
45        }
46        impl fmt::Binary for super::$name {
47            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48                fmt::Binary::fmt(&self.to_u32(), f)
49            }
50        }
51        impl fmt::LowerHex for super::$name {
52            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53                fmt::LowerHex::fmt(&self.to_u32(), f)
54            }
55        }
56        impl fmt::UpperHex for super::$name {
57            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58                fmt::UpperHex::fmt(&self.to_u32(), f)
59            }
60        }
61        impl fmt::Octal for super::$name {
62            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63                fmt::Octal::fmt(&self.to_u32(), f)
64            }
65        }
66    }};
67}
68#[rustfmt::skip]
69char_core_impls!();
70
71/* From char7 */
72
73#[cfg(feature = "_char7")]
74mod c7 {
75    use super::super::*;
76
77    impl From<char7> for char {
78        #[must_use]
79        fn from(c: char7) -> char {
80            c.to_char()
81        }
82    }
83    #[cfg(feature = "_char8")]
84    impl From<char7> for char8 {
85        #[must_use]
86        fn from(c: char7) -> char8 {
87            c.to_char8()
88        }
89    }
90    #[cfg(feature = "_char16")]
91    impl From<char7> for char16 {
92        #[must_use]
93        fn from(c: char7) -> char16 {
94            c.to_char16()
95        }
96    }
97}
98
99/* From char8 */
100
101#[cfg(feature = "_char8")]
102mod c8 {
103    use super::*;
104
105    impl From<char8> for char {
106        #[must_use]
107        fn from(c: char8) -> char {
108            c.to_char()
109        }
110    }
111    #[cfg(feature = "_char7")]
112    impl TryFrom<char8> for char7 {
113        type Error = DataOverflow;
114        fn try_from(c: char8) -> Result<char7, DataOverflow> {
115            c.try_to_char7()
116        }
117    }
118    #[cfg(feature = "_char16")]
119    impl From<char8> for char16 {
120        #[must_use]
121        fn from(c: char8) -> char16 {
122            c.to_char16()
123        }
124    }
125}
126
127/* From char16 */
128
129#[cfg(feature = "_char16")]
130mod c16 {
131    use super::*;
132
133    impl From<char16> for char {
134        #[must_use]
135        fn from(c: char16) -> char {
136            c.to_char()
137        }
138    }
139    #[cfg(feature = "_char7")]
140    impl TryFrom<char16> for char7 {
141        type Error = DataOverflow;
142        fn try_from(c: char16) -> Result<char7, DataOverflow> {
143            c.try_to_char7()
144        }
145    }
146    #[cfg(feature = "_char8")]
147    impl TryFrom<char16> for char8 {
148        type Error = DataOverflow;
149        fn try_from(c: char16) -> Result<char8, DataOverflow> {
150            c.try_to_char8()
151        }
152    }
153}
154
155/* From char */
156
157#[cfg(feature = "_char7")]
158impl TryFrom<char> for char7 {
159    type Error = DataOverflow;
160    fn try_from(c: char) -> Result<char7, DataOverflow> {
161        char7::try_from_char(c)
162    }
163}
164#[cfg(feature = "_char8")]
165impl TryFrom<char> for char8 {
166    type Error = DataOverflow;
167    fn try_from(c: char) -> Result<char8, DataOverflow> {
168        char8::try_from_char(c)
169    }
170}
171#[cfg(feature = "_char16")]
172impl TryFrom<char> for char16 {
173    type Error = DataOverflow;
174    fn try_from(c: char) -> Result<char16, DataOverflow> {
175        char16::try_from_char(c)
176    }
177}