termimad/parse/
parse_attribute.rs1use {
2    crate::crossterm::style::Attribute,
3    lazy_regex::*,
4    std::fmt,
5};
6
7#[derive(thiserror::Error, Debug)]
8pub enum ParseAttributeError {
9    #[error("not a recognized attribute")]
10    Unrecognized,
11}
12
13pub fn write_attribute(f: &mut fmt::Formatter<'_>, a: Attribute) -> fmt::Result {
14    match a {
15        Attribute::Reset => Ok(()),
16        Attribute::Bold => write!(f, "Bold"),
17        Attribute::Dim => write!(f, "Dim"),
18        Attribute::Italic => write!(f, "Italic"),
19        Attribute::Underlined => write!(f, "Underlined"),
20        Attribute::SlowBlink => write!(f, "SlowBlink"),
21        Attribute::RapidBlink => write!(f, "RapidBlink"),
22        Attribute::Reverse => write!(f, "Reverse"),
23        Attribute::Hidden => write!(f, "Hidden"),
24        Attribute::CrossedOut => write!(f, "CrossedOut"),
25        Attribute::Fraktur => write!(f, "Fraktur"),
26        Attribute::NoBold => write!(f, "NoBold"),
27        Attribute::NormalIntensity => write!(f, "NormalIntensity"),
28        Attribute::NoItalic => write!(f, "NoItalic"),
29        Attribute::NoUnderline => write!(f, "NoUnderline"),
30        Attribute::NoBlink => write!(f, "NoBlink"),
31        Attribute::NoReverse => write!(f, "NoReverse"),
32        Attribute::NoHidden => write!(f, "NoHidden"),
33        Attribute::NotCrossedOut => write!(f, "NotCrossedOut"),
34        Attribute::Framed => write!(f, "Framed"),
35        Attribute::Encircled => write!(f, "Encircled"),
36        Attribute::OverLined => write!(f, "OverLined"),
37        Attribute::NotFramedOrEncircled => write!(f, "NotFramedOrEncircled"),
38        Attribute::NotOverLined => write!(f, "NotOverLined"),
39        _ => Ok(()),
40    }
41}
42
43pub fn parse_attribute(s: &str) -> Result<Attribute, ParseAttributeError> {
45    if regex_is_match!("bold"i, s) {
46        Ok(Attribute::Bold)
47    } else if regex_is_match!("crossed[_-]?out"i, s) {
48        Ok(Attribute::CrossedOut)
49    } else if regex_is_match!("dim"i, s) {
50        Ok(Attribute::Dim)
51    } else if regex_is_match!("italic"i, s) {
52        Ok(Attribute::Italic)
53    } else if regex_is_match!("under[_-]?lined"i, s) {
54        Ok(Attribute::Underlined)
55    } else if regex_is_match!("over[_-]?lined"i, s) {
56        Ok(Attribute::OverLined)
57    } else if regex_is_match!("reverse"i, s) {
58        Ok(Attribute::Reverse)
59    } else if regex_is_match!("encircled"i, s) {
60        Ok(Attribute::Encircled)
61    } else if regex_is_match!("slow[_-]?blink"i, s) {
62        Ok(Attribute::SlowBlink)
63    } else if regex_is_match!("rapid[_-]?blink"i, s) {
64        Ok(Attribute::RapidBlink)
65    } else {
66        Err(ParseAttributeError::Unrecognized)
67    }
68}