mod variants;
pub use variants::*;
macro_rules! to_math_variant {
($to_math_variant:ident, $math_variant:expr) => {
fn $to_math_variant(&self) -> String {
self.chars()
.map(|ch|
if let Some(variant) = $math_variant(ch) { variant }
else { ch })
.collect()
}
}
}
pub trait MathTextTransform<T> {
fn to_math_bold(&self) -> T;
fn to_math_italic(&self) -> T;
fn to_math_bold_italic(&self) -> T;
fn to_math_sans_serif(&self) -> T;
fn to_math_sans_serif_bold(&self) -> T;
fn to_math_sans_serif_italic(&self) -> T;
fn to_math_sans_serif_bold_italic(&self) -> T;
fn to_math_script(&self) -> T;
fn to_math_bold_script(&self) -> T;
fn to_math_fraktur(&self) -> T;
fn to_math_bold_fraktur(&self) -> T;
fn to_math_monospace(&self) -> T;
fn to_math_double_struck(&self) -> T;
}
impl MathTextTransform<String> for str {
to_math_variant!(to_math_bold, math_bold);
to_math_variant!(to_math_italic, math_italic);
to_math_variant!(to_math_bold_italic, math_bold_italic);
to_math_variant!(to_math_sans_serif, math_sans_serif);
to_math_variant!(to_math_sans_serif_bold, math_sans_serif_bold);
to_math_variant!(to_math_sans_serif_italic, math_sans_serif_italic);
to_math_variant!(to_math_sans_serif_bold_italic, math_sans_serif_bold_italic);
to_math_variant!(to_math_script, math_script);
to_math_variant!(to_math_bold_script, math_bold_script);
to_math_variant!(to_math_fraktur, math_fraktur);
to_math_variant!(to_math_bold_fraktur, math_bold_fraktur);
to_math_variant!(to_math_monospace, math_monospace);
to_math_variant!(to_math_double_struck, math_double_struck);
}
#[cfg(test)]
mod tests {
use super::MathTextTransform;
#[test]
fn to_math_bold() {
assert_eq!("Bold 123".to_math_bold(), "๐๐จ๐ฅ๐ ๐๐๐");
assert_eq!("ฮฮฟฮปฮด".to_math_bold(), "๐ฉ๐๐๐
");
}
#[test]
fn to_math_italic() {
assert_eq!("Italฤฑc 123".to_math_italic(), "๐ผ๐ก๐๐๐ค๐ 123");
assert_eq!("ฮฯฮฑฮปฮนฮบ".to_math_italic(), "๐ช๐๐ผ๐๐๐
");
}
#[test]
fn to_math_bold_italic() {
assert_eq!("Bold-Italic 123".to_math_bold_italic(), "๐ฉ๐๐๐
-๐ฐ๐๐๐๐๐ 123");
assert_eq!("ฮฮฟฮปฮด-ฮฯฮฑฮปฮนฮบ".to_math_bold_italic(), "๐๐๐๐น-๐ค๐๐ถ๐๐พ๐ฟ");
}
#[test]
fn to_math_sans_serif() {
assert_eq!("Sans-Serif 123".to_math_sans_serif(), "๐ฒ๐บ๐๐-๐ฒ๐พ๐๐๐ฟ ๐๐๐");
}
#[test]
fn to_math_sans_serif_bold() {
assert_eq!("Sans-Serif-Bold 123".to_math_sans_serif_bold(), "๐ฆ๐ฎ๐ป๐-๐ฆ๐ฒ๐ฟ๐ถ๐ณ-๐๐ผ๐น๐ฑ ๐ญ๐ฎ๐ฏ");
assert_eq!("ฮฃฮฑฮฝฯ-ฮฃฮตฯฮนฯ-ฮฮฟฮปฮด".to_math_sans_serif_bold(), "๐จ๐ฐ๐ผ๐-๐จ๐ด๐๐ธ๐
-๐๐พ๐บ๐ณ");
}
#[test]
fn to_math_sans_serif_italic() {
assert_eq!("Sans-Serif-Italic 123".to_math_sans_serif_italic(), "๐๐ข๐ฏ๐ด-๐๐ฆ๐ณ๐ช๐ง-๐๐ต๐ข๐ญ๐ช๐ค 123");
}
#[test]
fn to_math_sans_serif_bold_italic() {
assert_eq!("Sans-Serif-Bold-Italic 123".to_math_sans_serif_bold_italic(), "๐๐๐ฃ๐จ-๐๐๐ง๐๐-๐ฝ๐ค๐ก๐-๐๐ฉ๐๐ก๐๐ 123");
assert_eq!("ฮฃฮฑฮฝฯ-ฮฃฮตฯฮนฯ-ฮฮฟฮปฮด-ฮฯฮฑฮปฮนฮบ".to_math_sans_serif_bold_italic(), "๐ข๐ช๐ถ๐ผ-๐ข๐ฎ๐บ๐ฒ๐ฟ-๐๐ธ๐ด๐ญ-๐๐ฝ๐ช๐ด๐ฒ๐ณ");
}
#[test]
fn to_math_script() {
assert_eq!("Script 123".to_math_script(), "๐ฎ๐ธ๐๐พ๐
๐ 123");
}
#[test]
fn to_math_bold_script() {
assert_eq!("Bold-Script 123".to_math_bold_script(), "๐๐ธ๐ต๐ญ-๐ข๐ฌ๐ป๐ฒ๐น๐ฝ 123");
}
#[test]
fn to_math_fraktur() {
assert_eq!("Fraktur 123".to_math_fraktur(), "๐๐ฏ๐๐จ๐ฑ๐ฒ๐ฏ 123");
}
#[test]
fn to_math_bold_fraktur() {
assert_eq!("Bold-Fraktur 123".to_math_bold_fraktur(), "๐ญ๐๐๐-๐ฑ๐๐๐๐๐๐ 123");
}
#[test]
fn to_math_monospace() {
assert_eq!("Monospace 123".to_math_monospace(), "๐ผ๐๐๐๐๐๐๐๐ ๐ท๐ธ๐น");
}
#[test]
fn to_math_double_struck() {
assert_eq!("Double-Struck 123".to_math_double_struck(), "๐ป๐ ๐ฆ๐๐๐-๐๐ฅ๐ฃ๐ฆ๐๐ ๐๐๐");
}
}