use serde::{Deserialize, Serialize}; use super::*; use crate::documents::BuildXML; use crate::xml_builder::*; #[derive(Debug, Clone, Deserialize, Serialize, PartialEq)] #[serde(rename_all = "camelCase")] pub struct RunProperty { pub sz: Option, pub sz_cs: Option, pub color: Option, pub highlight: Option, pub underline: Option, pub bold: Option, pub bold_cs: Option, pub italic: Option, pub italic_cs: Option, pub vanish: Option, pub spacing: Option, pub fonts: Option, } impl RunProperty { pub fn new() -> RunProperty { Default::default() } pub fn size(mut self, size: usize) -> RunProperty { self.sz = Some(Sz::new(size)); self.sz_cs = Some(SzCs::new(size)); self } pub fn spacing(mut self, spacing: i32) -> RunProperty { self.spacing = Some(spacing); self } pub fn color(mut self, color: impl Into) -> RunProperty { self.color = Some(Color::new(color)); self } pub fn highlight(mut self, color: impl Into) -> RunProperty { self.highlight = Some(Highlight::new(color)); self } pub fn bold(mut self) -> RunProperty { self.bold = Some(Bold::new()); self.bold_cs = Some(BoldCs::new()); self } pub fn disable_bold(mut self) -> RunProperty { self.bold = Some(Bold::new().disable()); self.bold_cs = Some(BoldCs::new().disable()); self } pub fn italic(mut self) -> RunProperty { self.italic = Some(Italic::new()); self.italic_cs = Some(ItalicCs::new()); self } pub fn disable_italic(mut self) -> RunProperty { self.italic = Some(Italic::new().disable()); self.italic_cs = Some(ItalicCs::new().disable()); self } pub fn underline(mut self, line_type: impl Into) -> RunProperty { self.underline = Some(Underline::new(line_type)); self } pub fn vanish(mut self) -> RunProperty { self.vanish = Some(Vanish::new()); self } pub fn fonts(mut self, font: RunFonts) -> RunProperty { self.fonts = Some(font); self } } impl Default for RunProperty { fn default() -> Self { Self { color: None, sz: None, sz_cs: None, highlight: None, underline: None, bold: None, bold_cs: None, italic: None, italic_cs: None, vanish: None, fonts: None, spacing: None, } } } impl BuildXML for RunProperty { fn build(&self) -> Vec { let b = XMLBuilder::new(); let spacing = if let Some(s) = self.spacing { Some(Spacing::new(crate::SpacingType::Value(s))) } else { None }; b.open_run_property() .add_optional_child(&self.sz) .add_optional_child(&self.sz_cs) .add_optional_child(&self.color) .add_optional_child(&self.bold) .add_optional_child(&self.bold_cs) .add_optional_child(&self.italic) .add_optional_child(&self.italic_cs) .add_optional_child(&self.highlight) .add_optional_child(&self.underline) .add_optional_child(&self.vanish) .add_optional_child(&self.fonts) .add_optional_child(&spacing) .close() .build() } } #[cfg(test)] mod tests { use super::*; #[cfg(test)] use pretty_assertions::assert_eq; use std::str; #[test] fn test_size() { let c = RunProperty::new().size(10).color("FFFFFF"); let b = c.build(); assert_eq!( str::from_utf8(&b).unwrap(), r#""# ); } #[test] fn test_highlight() { let c = RunProperty::new().highlight("FFFFFF"); let b = c.build(); assert_eq!( str::from_utf8(&b).unwrap(), r#""# ); } #[test] fn test_bold() { let c = RunProperty::new().bold(); let b = c.build(); assert_eq!( str::from_utf8(&b).unwrap(), r#""# ); } #[test] fn test_underline() { let c = RunProperty::new().underline("single"); let b = c.build(); assert_eq!( str::from_utf8(&b).unwrap(), r#""# ); } #[test] fn test_vanish() { let c = RunProperty::new().vanish(); let b = c.build(); assert_eq!( str::from_utf8(&b).unwrap(), r#""# ); } #[test] fn test_run_fonts() { let c = RunProperty::new().fonts(RunFonts::new().east_asia("Hiragino")); let b = c.build(); assert_eq!( str::from_utf8(&b).unwrap(), r#""# ); } }