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

81 lines
1.7 KiB
Rust
Raw Normal View History

2019-11-07 09:08:59 +02:00
use super::{RunProperty, Text};
use crate::documents::BuildXML;
use crate::xml_builder::*;
2019-11-12 11:57:16 +02:00
#[derive(Debug, Clone)]
2019-11-07 09:08:59 +02:00
pub struct Run {
run_property: RunProperty,
text: Text,
}
impl Run {
pub fn new(text: impl Into<String>) -> Run {
Run {
text: Text::new(text),
..Default::default()
}
}
2019-11-11 08:36:26 +02:00
pub fn size(mut self, size: usize) -> Run {
self.run_property = self.run_property.size(size);
self
}
2019-11-13 09:08:25 +02:00
pub fn color(mut self, color: &str) -> Run {
self.run_property = self.run_property.color(color);
self
}
pub fn highlight(mut self, color: &str) -> Run {
self.run_property = self.run_property.highlight(color);
self
}
pub fn bold(mut self) -> Run {
self.run_property = self.run_property.bold();
self
}
pub fn italic(mut self) -> Run {
self.run_property = self.run_property.italic();
self
}
2019-11-07 09:08:59 +02:00
}
impl Default for Run {
fn default() -> Self {
let run_property = RunProperty::new();
let text = Text::new("");
Self { run_property, text }
}
}
impl BuildXML for Run {
fn build(&self) -> Vec<u8> {
let b = XMLBuilder::new();
b.open_run()
.add_child(&self.run_property)
.add_child(&self.text)
.close()
.build()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(test)]
use pretty_assertions::assert_eq;
use std::str;
#[test]
fn test_build() {
let b = Run::new("Hello").build();
assert_eq!(
str::from_utf8(&b).unwrap(),
2019-11-11 06:05:07 +02:00
r#"<w:r><w:rPr /><w:t xml:space="preserve">Hello</w:t></w:r>"#
2019-11-07 09:08:59 +02:00
);
}
}