docx_rs/documents/elements/
color.rs1use serde::{Deserialize, Serialize, Serializer};
2use std::io::Write;
3
4use crate::documents::BuildXML;
5use crate::xml_builder::*;
6
7#[derive(Deserialize, Debug, Clone, PartialEq)]
8pub struct Color {
9 val: String,
10}
11
12impl Color {
13 pub fn new(val: impl Into<String>) -> Color {
14 Color { val: val.into() }
15 }
16}
17
18impl BuildXML for Color {
19 fn build_to<W: Write>(
20 &self,
21 stream: xml::writer::EventWriter<W>,
22 ) -> xml::writer::Result<xml::writer::EventWriter<W>> {
23 XMLBuilder::from(stream).color(&self.val)?.into_inner()
24 }
25}
26
27impl Serialize for Color {
28 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
29 where
30 S: Serializer,
31 {
32 serializer.serialize_str(&self.val)
33 }
34}
35
36#[cfg(test)]
37mod tests {
38
39 use super::*;
40 #[cfg(test)]
41 use pretty_assertions::assert_eq;
42 use std::str;
43
44 #[test]
45 fn test_build() {
46 let c = Color::new("FFFFFF");
47 let b = c.build();
48 assert_eq!(str::from_utf8(&b).unwrap(), r#"<w:color w:val="FFFFFF" />"#);
49 }
50}