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

69 lines
1.4 KiB
Rust
Raw Normal View History

2019-11-11 08:36:26 +02:00
use super::{Color, Sz, SzCs};
2019-11-06 12:17:49 +02:00
use crate::documents::BuildXML;
use crate::xml_builder::*;
2019-11-12 11:57:16 +02:00
#[derive(Debug, Clone)]
2019-11-06 12:17:49 +02:00
pub struct RunProperty {
sz: Option<Sz>,
2019-11-11 08:36:26 +02:00
sz_cs: Option<SzCs>,
2019-11-07 06:57:58 +02:00
color: Option<Color>,
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
2019-11-11 08:36:26 +02:00
pub fn color(mut self, color: &str) -> RunProperty {
2019-11-07 06:57:58 +02:00
self.color = Some(Color::new(color));
self
}
}
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-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();
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)
.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]
fn test_build() {
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
);
}
}