docx_rust/formatting/
underline.rs

1use hard_xml::{XmlRead, XmlWrite};
2use std::borrow::Cow;
3
4use crate::{__string_enum, __xml_test_suites};
5
6/// Underline
7///
8/// ```rust
9/// use docx_rust::formatting::*;
10///
11/// let udl = Underline::from("00ff00");
12/// let udl = Underline::from(String::from("ff0000"));
13/// let udl = Underline::from(("00ff00", UnderlineStyle::Dash));
14/// let udl = Underline::from((String::from("ff0000"), UnderlineStyle::DotDash));
15/// ```
16#[derive(Debug, Default, XmlRead, XmlWrite, Clone)]
17#[cfg_attr(test, derive(PartialEq))]
18#[xml(tag = "w:u")]
19pub struct Underline<'a> {
20    #[xml(attr = "w:color")]
21    pub color: Option<Cow<'a, str>>,
22    #[xml(attr = "w:val")]
23    pub val: Option<UnderlineStyle>,
24}
25
26impl From<String> for Underline<'_> {
27    fn from(val: String) -> Self {
28        Underline {
29            color: Some(val.into()),
30            val: None,
31        }
32    }
33}
34
35impl<'a> From<&'a str> for Underline<'a> {
36    fn from(val: &'a str) -> Self {
37        Underline {
38            color: Some(val.into()),
39            val: None,
40        }
41    }
42}
43
44impl From<UnderlineStyle> for Underline<'_> {
45    fn from(val: UnderlineStyle) -> Self {
46        Underline {
47            color: None,
48            val: Some(val),
49        }
50    }
51}
52
53impl From<(String, UnderlineStyle)> for Underline<'_> {
54    fn from(val: (String, UnderlineStyle)) -> Self {
55        Underline {
56            color: Some(val.0.into()),
57            val: Some(val.1),
58        }
59    }
60}
61
62impl<'a> From<(&'a str, UnderlineStyle)> for Underline<'a> {
63    fn from(val: (&'a str, UnderlineStyle)) -> Self {
64        Underline {
65            color: Some(val.0.into()),
66            val: Some(val.1),
67        }
68    }
69}
70
71#[derive(Debug, Clone)]
72#[cfg_attr(test, derive(PartialEq))]
73pub enum UnderlineStyle {
74    Dash,
75    DashDotDotHeavy,
76    DashDotHeavy,
77    DashedHeavy,
78    DashLong,
79    DashLongHeavy,
80    DotDash,
81    DotDotDash,
82    Dotted,
83    DottedHeavy,
84    Double,
85    None,
86    Single,
87    Thick,
88    Wave,
89    WavyDouble,
90    WavyHeavy,
91    Words,
92}
93
94__string_enum! {
95    UnderlineStyle {
96        Dash = "dash",
97        DashDotDotHeavy = "dashDotDotHeavy",
98        DashDotHeavy = "dashDotHeavy",
99        DashedHeavy = "dashedHeavy",
100        DashLong = "dashLong",
101        DashLongHeavy = "dashLongHeavy",
102        DotDash = "dotDash",
103        DotDotDash = "dotDotDash",
104        Dotted = "dotted",
105        DottedHeavy = "dottedHeavy",
106        Double = "double",
107        None = "none",
108        Single = "single",
109        Thick = "thick",
110        Wave = "wave",
111        WavyDouble = "wavyDouble",
112        WavyHeavy = "wavyHeavy",
113        Words = "words",
114    }
115}
116
117__xml_test_suites!(
118    Underline,
119    Underline::default(),
120    r#"<w:u/>"#,
121    Underline::from("00ff00"),
122    r#"<w:u w:color="00ff00"/>"#,
123    Underline::from(String::from("ff0000")),
124    r#"<w:u w:color="ff0000"/>"#,
125    Underline::from(("00ff00", UnderlineStyle::Dash)),
126    r#"<w:u w:color="00ff00" w:val="dash"/>"#,
127    Underline::from((String::from("ff0000"), UnderlineStyle::DotDash)),
128    r#"<w:u w:color="ff0000" w:val="dotDash"/>"#,
129);