docx-rs/docx-core/src/documents/elements/run_property.rs

191 lines
4.8 KiB
Rust
Raw Normal View History

use serde::{Deserialize, Serialize};
use super::*;
2019-11-06 12:17:49 +02:00
use crate::documents::BuildXML;
use crate::xml_builder::*;
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
#[serde(rename_all = "camelCase")]
2019-11-06 12:17:49 +02:00
pub struct RunProperty {
pub sz: Option<Sz>,
pub sz_cs: Option<SzCs>,
pub color: Option<Color>,
pub highlight: Option<Highlight>,
pub underline: Option<Underline>,
pub bold: Option<Bold>,
pub bold_cs: Option<BoldCs>,
pub italic: Option<Italic>,
pub italic_cs: Option<ItalicCs>,
pub vanish: Option<Vanish>,
pub spacing: Option<i32>,
pub fonts: Option<RunFonts>,
2019-11-06 12:17:49 +02:00
}
impl RunProperty {
pub fn new() -> RunProperty {
2019-11-07 06:57:58 +02:00
Default::default()
2019-11-06 12:17:49 +02:00
}
2019-11-11 08:36:26 +02:00
pub fn size(mut self, size: usize) -> RunProperty {
self.sz = Some(Sz::new(size));
self.sz_cs = Some(SzCs::new(size));
2019-11-06 12:17:49 +02:00
self
}
2019-11-07 06:57:58 +02:00
pub fn spacing(mut self, spacing: i32) -> RunProperty {
self.spacing = Some(spacing);
self
}
pub fn color(mut self, color: impl Into<String>) -> RunProperty {
2019-11-07 06:57:58 +02:00
self.color = Some(Color::new(color));
self
}
2019-11-13 09:08:25 +02:00
pub fn highlight(mut self, color: impl Into<String>) -> RunProperty {
2019-11-13 09:08:25 +02:00
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 italic(mut self) -> RunProperty {
self.italic = Some(Italic::new());
self.italic_cs = Some(ItalicCs::new());
self
}
2019-12-04 09:28:11 +02:00
pub fn underline(mut self, line_type: impl Into<String>) -> RunProperty {
2019-12-04 09:28:11 +02:00
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
}
2019-11-07 06:57:58 +02:00
}
impl Default for RunProperty {
fn default() -> Self {
Self {
color: None,
2019-11-11 08:36:26 +02:00
sz: None,
sz_cs: None,
2019-11-13 09:08:25 +02:00
highlight: None,
2019-12-04 09:28:11 +02:00
underline: None,
2019-11-13 09:08:25 +02:00
bold: None,
bold_cs: None,
italic: None,
italic_cs: None,
vanish: None,
fonts: None,
spacing: None,
2019-11-07 06:57:58 +02:00
}
}
2019-11-06 12:17:49 +02:00
}
impl BuildXML for RunProperty {
fn build(&self) -> Vec<u8> {
let b = XMLBuilder::new();
let spacing = if let Some(s) = self.spacing {
Some(Spacing::new(crate::SpacingType::Value(s)))
} else {
None
};
2019-11-07 06:57:58 +02:00
b.open_run_property()
.add_optional_child(&self.sz)
2019-11-11 08:36:26 +02:00
.add_optional_child(&self.sz_cs)
2019-11-07 06:57:58 +02:00
.add_optional_child(&self.color)
2019-11-13 09:08:25 +02:00
.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)
2019-12-04 09:28:11 +02:00
.add_optional_child(&self.underline)
.add_optional_child(&self.vanish)
.add_optional_child(&self.fonts)
.add_optional_child(&spacing)
2019-11-07 06:57:58 +02:00
.close()
.build()
2019-11-06 12:17:49 +02:00
}
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(test)]
use pretty_assertions::assert_eq;
use std::str;
#[test]
2019-11-13 09:08:25 +02:00
fn test_size() {
2019-11-11 08:36:26 +02:00
let c = RunProperty::new().size(10).color("FFFFFF");
2019-11-06 12:17:49 +02:00
let b = c.build();
assert_eq!(
str::from_utf8(&b).unwrap(),
2019-11-11 08:36:26 +02:00
r#"<w:rPr><w:sz w:val="10" /><w:szCs w:val="10" /><w:color w:val="FFFFFF" /></w:rPr>"#
2019-11-06 12:17:49 +02:00
);
}
2019-11-13 09:08:25 +02:00
#[test]
fn test_highlight() {
let c = RunProperty::new().highlight("FFFFFF");
let b = c.build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<w:rPr><w:highlight w:val="FFFFFF" /></w:rPr>"#
);
}
#[test]
fn test_bold() {
let c = RunProperty::new().bold();
let b = c.build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<w:rPr><w:b /><w:bCs /></w:rPr>"#
);
}
2019-12-04 09:28:11 +02:00
#[test]
fn test_underline() {
let c = RunProperty::new().underline("single");
let b = c.build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<w:rPr><w:u w:val="single" /></w:rPr>"#
);
}
#[test]
fn test_vanish() {
let c = RunProperty::new().vanish();
let b = c.build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<w:rPr><w:vanish /></w:rPr>"#
);
}
#[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#"<w:rPr><w:rFonts w:eastAsia="Hiragino" /></w:rPr>"#
);
}
2019-11-06 12:17:49 +02:00
}