docx_rs/documents/elements/
highlight.rs1use serde::{Deserialize, Serialize, Serializer};
2use std::io::Write;
3
4use crate::documents::BuildXML;
5use crate::xml_builder::*;
6
7#[derive(Debug, Clone, Deserialize, PartialEq)]
8pub struct Highlight {
9 val: String,
10}
11
12impl Highlight {
13 pub fn new(val: impl Into<String>) -> Highlight {
14 Highlight { val: val.into() }
15 }
16}
17
18impl BuildXML for Highlight {
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).highlight(&self.val)?.into_inner()
24 }
25}
26
27impl Serialize for Highlight {
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_highlight() {
46 let c = Highlight::new("FFFFFF");
47 let b = c.build();
48 assert_eq!(
49 str::from_utf8(&b).unwrap(),
50 r#"<w:highlight w:val="FFFFFF" />"#
51 );
52 }
53
54 #[test]
55 fn test_highlight_json() {
56 let c = Highlight::new("FFFFFF");
57
58 assert_eq!(serde_json::to_string(&c).unwrap(), r#""FFFFFF""#);
59 }
60}