Skip to main content

rumdl_lib/rules/
emphasis_style.rs

1use std::fmt;
2
3/// The style for emphasis (MD049)
4#[derive(Debug, PartialEq, Eq, Clone, Copy, Default, Hash)]
5pub enum EmphasisStyle {
6    /// Consistent with the most prevalent emphasis style found (or first found if tied)
7    #[default]
8    Consistent,
9    /// Asterisk style (*)
10    Asterisk,
11    /// Underscore style (_)
12    Underscore,
13}
14
15impl fmt::Display for EmphasisStyle {
16    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
17        match self {
18            EmphasisStyle::Asterisk => write!(f, "asterisk"),
19            EmphasisStyle::Underscore => write!(f, "underscore"),
20            EmphasisStyle::Consistent => write!(f, "consistent"),
21        }
22    }
23}
24
25impl From<&str> for EmphasisStyle {
26    fn from(s: &str) -> Self {
27        match s.trim().to_ascii_lowercase().as_str() {
28            "asterisk" => EmphasisStyle::Asterisk,
29            "underscore" => EmphasisStyle::Underscore,
30            _ => EmphasisStyle::Consistent,
31        }
32    }
33}
34
35#[cfg(test)]
36mod tests {
37    use super::*;
38
39    #[test]
40    fn test_emphasis_style_from_str() {
41        assert_eq!(EmphasisStyle::from("asterisk"), EmphasisStyle::Asterisk);
42        assert_eq!(EmphasisStyle::from("underscore"), EmphasisStyle::Underscore);
43        assert_eq!(EmphasisStyle::from(""), EmphasisStyle::Consistent);
44        assert_eq!(EmphasisStyle::from("unknown"), EmphasisStyle::Consistent);
45    }
46}