1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use serde::{Deserialize, Serialize};

use crate::documents::BuildXML;
use crate::xml_builder::*;

/*
  17.3.2.26 rFonts (Run Fonts)
  This element specifies the fonts which shall be used to display the text contents of this run.
  Within a single run, there can be up to four types of font slot which shall each be allowed to use a unique font:
  - ASCII (i.e., the first 128 Unicode code points)
  - High ANSI
  - Complex Script
*/
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
#[serde(rename_all = "camelCase")]
pub struct RunFonts {
    #[serde(skip_serializing_if = "Option::is_none")]
    ascii: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    hi_ansi: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    east_asia: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    cs: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    ascii_theme: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    hi_ansi_theme: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    east_asia_theme: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    cs_theme: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    hint: Option<String>,
}

impl RunFonts {
    pub fn new() -> RunFonts {
        Default::default()
    }

    pub fn ascii(mut self, f: impl Into<String>) -> Self {
        self.ascii = Some(f.into());
        self
    }

    pub fn hi_ansi(mut self, f: impl Into<String>) -> Self {
        self.hi_ansi = Some(f.into());
        self
    }

    pub fn east_asia(mut self, f: impl Into<String>) -> Self {
        self.east_asia = Some(f.into());
        self
    }

    pub fn cs(mut self, f: impl Into<String>) -> Self {
        self.cs = Some(f.into());
        self
    }

    pub fn ascii_theme(mut self, f: impl Into<String>) -> Self {
        self.ascii_theme = Some(f.into());
        self
    }

    pub fn hi_ansi_theme(mut self, f: impl Into<String>) -> Self {
        self.hi_ansi_theme = Some(f.into());
        self
    }

    pub fn east_asia_theme(mut self, f: impl Into<String>) -> Self {
        self.east_asia_theme = Some(f.into());
        self
    }

    pub fn cs_theme(mut self, f: impl Into<String>) -> Self {
        self.cs_theme = Some(f.into());
        self
    }

    pub fn hint(mut self, f: impl Into<String>) -> Self {
        self.hint = Some(f.into());
        self
    }
}

impl BuildXML for RunFonts {
    fn build(&self) -> Vec<u8> {
        let b = XMLBuilder::new();
        b.run_fonts(
            self.ascii.as_ref(),
            self.hi_ansi.as_ref(),
            self.cs.as_ref(),
            self.east_asia.as_ref(),
            self.ascii_theme.as_ref(),
            self.hi_ansi_theme.as_ref(),
            self.cs_theme.as_ref(),
            self.east_asia_theme.as_ref(),
            self.hint.as_ref(),
        )
        .build()
    }
}

#[cfg(test)]
mod tests {

    use super::*;
    #[cfg(test)]
    use pretty_assertions::assert_eq;
    use std::str;

    #[test]
    fn test_run_fonts_east_asia_build() {
        let c = RunFonts::new().east_asia("Hiragino");
        let b = c.build();
        assert_eq!(
            str::from_utf8(&b).unwrap(),
            r#"<w:rFonts w:eastAsia="Hiragino" />"#
        );
    }

    #[test]
    fn test_run_fonts_ascii_build() {
        let c = RunFonts::new().ascii("Arial");
        let b = c.build();
        assert_eq!(
            str::from_utf8(&b).unwrap(),
            r#"<w:rFonts w:ascii="Arial" />"#
        );
    }

    #[test]
    fn test_run_fonts_hi_ansi_build() {
        let c = RunFonts::new().hi_ansi("Arial");
        let b = c.build();
        assert_eq!(
            str::from_utf8(&b).unwrap(),
            r#"<w:rFonts w:hAnsi="Arial" />"#
        );
    }

    #[test]
    fn test_run_fonts_cs_build() {
        let c = RunFonts::new().cs("Arial");
        let b = c.build();
        assert_eq!(str::from_utf8(&b).unwrap(), r#"<w:rFonts w:cs="Arial" />"#);
    }
}