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

130 lines
3.1 KiB
Rust
Raw Normal View History

2019-12-05 08:44:18 +02:00
use super::{Break, DeleteText, RunProperty, Tab, Text};
2019-11-07 09:08:59 +02:00
use crate::documents::BuildXML;
2019-11-13 09:51:58 +02:00
use crate::types::BreakType;
2019-11-07 09:08:59 +02:00
use crate::xml_builder::*;
2019-11-12 11:57:16 +02:00
#[derive(Debug, Clone)]
2019-12-04 11:26:09 +02:00
pub struct Run<'a> {
2019-11-07 09:08:59 +02:00
run_property: RunProperty,
2019-12-05 08:44:18 +02:00
children: Vec<RunChild<'a>>,
2019-11-13 09:51:58 +02:00
}
2019-12-04 11:26:09 +02:00
impl<'a> Default for Run<'a> {
2019-11-13 09:51:58 +02:00
fn default() -> Self {
let run_property = RunProperty::new();
Self {
run_property,
children: vec![],
}
}
}
#[derive(Debug, Clone)]
2019-12-05 08:44:18 +02:00
pub enum RunChild<'a> {
Text(Text<'a>),
2019-11-15 11:15:43 +02:00
DeleteText(DeleteText),
2019-11-13 09:51:58 +02:00
Tab(Tab),
Break(Break),
2019-11-07 09:08:59 +02:00
}
2019-12-04 11:26:09 +02:00
impl<'a> Run<'a> {
pub fn new() -> Run<'a> {
2019-11-07 09:08:59 +02:00
Run {
..Default::default()
}
}
2019-11-11 08:36:26 +02:00
2019-12-04 11:26:09 +02:00
pub fn add_text(mut self, text: &'a str) -> Run<'a> {
2019-11-13 09:51:58 +02:00
self.children.push(RunChild::Text(Text::new(text)));
self
}
2019-12-04 11:26:09 +02:00
pub fn add_delete_text(mut self, text: &'a str) -> Run<'a> {
2019-11-15 11:15:43 +02:00
self.children.push(RunChild::Text(Text::new(text)));
self
}
2019-12-04 11:26:09 +02:00
pub fn add_tab(mut self) -> Run<'a> {
2019-11-13 09:51:58 +02:00
self.children.push(RunChild::Tab(Tab::new()));
self
}
2019-12-04 11:26:09 +02:00
pub fn add_break(mut self, break_type: BreakType) -> Run<'a> {
2019-11-13 09:51:58 +02:00
self.children.push(RunChild::Break(Break::new(break_type)));
self
}
2019-12-04 11:26:09 +02:00
pub fn size(mut self, size: usize) -> Run<'a> {
2019-11-11 08:36:26 +02:00
self.run_property = self.run_property.size(size);
self
}
2019-11-13 09:08:25 +02:00
2019-12-04 11:26:09 +02:00
pub fn color(mut self, color: &'a str) -> Run<'a> {
2019-11-13 09:08:25 +02:00
self.run_property = self.run_property.color(color);
self
}
2019-12-04 11:26:09 +02:00
pub fn highlight(mut self, color: &'a str) -> Run<'a> {
2019-11-13 09:08:25 +02:00
self.run_property = self.run_property.highlight(color);
self
}
2019-12-04 11:26:09 +02:00
pub fn bold(mut self) -> Run<'a> {
2019-11-13 09:08:25 +02:00
self.run_property = self.run_property.bold();
self
}
2019-12-04 11:26:09 +02:00
pub fn italic(mut self) -> Run<'a> {
2019-11-13 09:08:25 +02:00
self.run_property = self.run_property.italic();
self
}
2019-12-04 09:28:11 +02:00
2019-12-04 11:26:09 +02:00
pub fn underline(mut self, line_type: &'a str) -> Run<'a> {
2019-12-04 09:28:11 +02:00
self.run_property = self.run_property.underline(line_type);
self
}
2019-11-07 09:08:59 +02:00
}
2019-12-04 11:26:09 +02:00
impl<'a> BuildXML for Run<'a> {
2019-11-07 09:08:59 +02:00
fn build(&self) -> Vec<u8> {
let b = XMLBuilder::new();
2019-11-13 09:51:58 +02:00
let mut b = b.open_run().add_child(&self.run_property);
for c in &self.children {
match c {
RunChild::Text(t) => b = b.add_child(t),
2019-11-15 11:15:43 +02:00
RunChild::DeleteText(t) => b = b.add_child(t),
2019-11-13 09:51:58 +02:00
RunChild::Tab(t) => b = b.add_child(t),
RunChild::Break(t) => b = b.add_child(t),
}
}
b.close().build()
2019-11-07 09:08:59 +02:00
}
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(test)]
use pretty_assertions::assert_eq;
use std::str;
#[test]
fn test_build() {
2019-11-13 09:51:58 +02:00
let b = Run::new().add_text("Hello").build();
2019-11-07 09:08:59 +02:00
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
);
}
2019-12-04 09:28:11 +02:00
#[test]
fn test_underline() {
let b = Run::new().add_text("Hello").underline("single").build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<w:r><w:rPr><w:u w:val="single" /></w:rPr><w:t xml:space="preserve">Hello</w:t></w:r>"#
);
}
2019-11-07 09:08:59 +02:00
}