math_text_transform/
lib.rs1mod variants;
36pub use variants::*;
37
38macro_rules! to_math_variant {
39 ($to_math_variant:ident, $math_variant:expr) => {
40 fn $to_math_variant(&self) -> String {
41 self.chars()
42 .map(|ch|
43 if let Some(variant) = $math_variant(ch) { variant }
44 else { ch })
45 .collect()
46 }
47 }
48}
49
50pub trait MathTextTransform<T> {
63 fn to_math_bold(&self) -> T;
65
66 fn to_math_italic(&self) -> T;
68
69 fn to_math_bold_italic(&self) -> T;
71
72 fn to_math_sans_serif(&self) -> T;
74
75 fn to_math_sans_serif_bold(&self) -> T;
77
78 fn to_math_sans_serif_italic(&self) -> T;
80
81 fn to_math_sans_serif_bold_italic(&self) -> T;
83
84 fn to_math_script(&self) -> T;
86
87 fn to_math_bold_script(&self) -> T;
89
90 fn to_math_fraktur(&self) -> T;
92
93 fn to_math_bold_fraktur(&self) -> T;
95
96 fn to_math_monospace(&self) -> T;
98
99 fn to_math_double_struck(&self) -> T;
101}
102
103impl MathTextTransform<String> for str {
104 to_math_variant!(to_math_bold, math_bold);
105 to_math_variant!(to_math_italic, math_italic);
106 to_math_variant!(to_math_bold_italic, math_bold_italic);
107 to_math_variant!(to_math_sans_serif, math_sans_serif);
108 to_math_variant!(to_math_sans_serif_bold, math_sans_serif_bold);
109 to_math_variant!(to_math_sans_serif_italic, math_sans_serif_italic);
110 to_math_variant!(to_math_sans_serif_bold_italic, math_sans_serif_bold_italic);
111 to_math_variant!(to_math_script, math_script);
112 to_math_variant!(to_math_bold_script, math_bold_script);
113 to_math_variant!(to_math_fraktur, math_fraktur);
114 to_math_variant!(to_math_bold_fraktur, math_bold_fraktur);
115 to_math_variant!(to_math_monospace, math_monospace);
116 to_math_variant!(to_math_double_struck, math_double_struck);
117}
118
119
120#[cfg(test)]
121mod tests {
122 use super::MathTextTransform;
123
124 #[test]
125 fn to_math_bold() {
126 assert_eq!("Bold 123".to_math_bold(), "๐๐จ๐ฅ๐ ๐๐๐");
127 assert_eq!("ฮฮฟฮปฮด".to_math_bold(), "๐ฉ๐๐๐
");
128 }
129
130 #[test]
131 fn to_math_italic() {
132 assert_eq!("Italฤฑc 123".to_math_italic(), "๐ผ๐ก๐๐๐ค๐ 123");
133 assert_eq!("ฮฯฮฑฮปฮนฮบ".to_math_italic(), "๐ช๐๐ผ๐๐๐
");
134 }
135
136 #[test]
137 fn to_math_bold_italic() {
138 assert_eq!("Bold-Italic 123".to_math_bold_italic(), "๐ฉ๐๐๐
-๐ฐ๐๐๐๐๐ 123");
139 assert_eq!("ฮฮฟฮปฮด-ฮฯฮฑฮปฮนฮบ".to_math_bold_italic(), "๐๐๐๐น-๐ค๐๐ถ๐๐พ๐ฟ");
140 }
141
142 #[test]
143 fn to_math_sans_serif() {
144 assert_eq!("Sans-Serif 123".to_math_sans_serif(), "๐ฒ๐บ๐๐-๐ฒ๐พ๐๐๐ฟ ๐๐๐");
145 }
146
147 #[test]
148 fn to_math_sans_serif_bold() {
149 assert_eq!("Sans-Serif-Bold 123".to_math_sans_serif_bold(), "๐ฆ๐ฎ๐ป๐-๐ฆ๐ฒ๐ฟ๐ถ๐ณ-๐๐ผ๐น๐ฑ ๐ญ๐ฎ๐ฏ");
150 assert_eq!("ฮฃฮฑฮฝฯ-ฮฃฮตฯฮนฯ-ฮฮฟฮปฮด".to_math_sans_serif_bold(), "๐จ๐ฐ๐ผ๐-๐จ๐ด๐๐ธ๐
-๐๐พ๐บ๐ณ");
151 }
152
153 #[test]
154 fn to_math_sans_serif_italic() {
155 assert_eq!("Sans-Serif-Italic 123".to_math_sans_serif_italic(), "๐๐ข๐ฏ๐ด-๐๐ฆ๐ณ๐ช๐ง-๐๐ต๐ข๐ญ๐ช๐ค 123");
156 }
157
158 #[test]
159 fn to_math_sans_serif_bold_italic() {
160 assert_eq!("Sans-Serif-Bold-Italic 123".to_math_sans_serif_bold_italic(), "๐๐๐ฃ๐จ-๐๐๐ง๐๐-๐ฝ๐ค๐ก๐-๐๐ฉ๐๐ก๐๐ 123");
161 assert_eq!("ฮฃฮฑฮฝฯ-ฮฃฮตฯฮนฯ-ฮฮฟฮปฮด-ฮฯฮฑฮปฮนฮบ".to_math_sans_serif_bold_italic(), "๐ข๐ช๐ถ๐ผ-๐ข๐ฎ๐บ๐ฒ๐ฟ-๐๐ธ๐ด๐ญ-๐๐ฝ๐ช๐ด๐ฒ๐ณ");
162 }
163
164 #[test]
165 fn to_math_script() {
166 assert_eq!("Script 123".to_math_script(), "๐ฎ๐ธ๐๐พ๐
๐ 123");
167 }
168
169 #[test]
170 fn to_math_bold_script() {
171 assert_eq!("Bold-Script 123".to_math_bold_script(), "๐๐ธ๐ต๐ญ-๐ข๐ฌ๐ป๐ฒ๐น๐ฝ 123");
172 }
173
174 #[test]
175 fn to_math_fraktur() {
176 assert_eq!("Fraktur 123".to_math_fraktur(), "๐๐ฏ๐๐จ๐ฑ๐ฒ๐ฏ 123");
177 }
178
179 #[test]
180 fn to_math_bold_fraktur() {
181 assert_eq!("Bold-Fraktur 123".to_math_bold_fraktur(), "๐ญ๐๐๐-๐ฑ๐๐๐๐๐๐ 123");
182 }
183
184 #[test]
185 fn to_math_monospace() {
186 assert_eq!("Monospace 123".to_math_monospace(), "๐ผ๐๐๐๐๐๐๐๐ ๐ท๐ธ๐น");
187 }
188
189 #[test]
190 fn to_math_double_struck() {
191 assert_eq!("Double-Struck 123".to_math_double_struck(), "๐ป๐ ๐ฆ๐๐๐-๐๐ฅ๐ฃ๐ฆ๐๐ ๐๐๐");
192 }
193}