2019-12-13 17:47:47 +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-08 21:14:27 +02:00
|
|
|
pub struct Run {
|
2019-12-11 07:12:22 +02:00
|
|
|
pub run_property: RunProperty,
|
|
|
|
pub children: Vec<RunChild>,
|
2019-11-13 09:51:58 +02:00
|
|
|
}
|
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
impl Default for Run {
|
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-08 21:14:27 +02:00
|
|
|
pub enum RunChild {
|
|
|
|
Text(Text),
|
2019-12-13 17:47:47 +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-08 21:14:27 +02:00
|
|
|
impl Run {
|
|
|
|
pub fn new() -> Run {
|
2019-11-07 09:08:59 +02:00
|
|
|
Run {
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
}
|
2019-11-11 08:36:26 +02:00
|
|
|
|
2019-12-13 12:41:33 +02:00
|
|
|
pub fn add_text(mut self, text: &str) -> Run {
|
2019-11-13 09:51:58 +02:00
|
|
|
self.children.push(RunChild::Text(Text::new(text)));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-12-13 12:41:33 +02:00
|
|
|
pub fn add_delete_text(mut self, text: &str) -> Run {
|
2019-12-20 11:53:26 +02:00
|
|
|
self.children
|
|
|
|
.push(RunChild::DeleteText(DeleteText::new(text)));
|
2019-11-15 11:15:43 +02:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
pub fn add_tab(mut self) -> Run {
|
2019-11-13 09:51:58 +02:00
|
|
|
self.children.push(RunChild::Tab(Tab::new()));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
pub fn add_break(mut self, break_type: BreakType) -> Run {
|
2019-11-13 09:51:58 +02:00
|
|
|
self.children.push(RunChild::Break(Break::new(break_type)));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
pub fn size(mut self, size: usize) -> Run {
|
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-08 21:14:27 +02:00
|
|
|
pub fn color(mut self, color: impl Into<String>) -> Run {
|
2019-11-13 09:08:25 +02:00
|
|
|
self.run_property = self.run_property.color(color);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
pub fn highlight(mut self, color: impl Into<String>) -> Run {
|
2019-11-13 09:08:25 +02:00
|
|
|
self.run_property = self.run_property.highlight(color);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
pub fn bold(mut self) -> Run {
|
2019-11-13 09:08:25 +02:00
|
|
|
self.run_property = self.run_property.bold();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
pub fn italic(mut self) -> Run {
|
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-08 21:14:27 +02:00
|
|
|
pub fn underline(mut self, line_type: impl Into<String>) -> Run {
|
2019-12-04 09:28:11 +02:00
|
|
|
self.run_property = self.run_property.underline(line_type);
|
|
|
|
self
|
|
|
|
}
|
2019-12-13 17:47:47 +02:00
|
|
|
|
|
|
|
pub fn vanish(mut self) -> Run {
|
|
|
|
self.run_property = self.run_property.vanish();
|
|
|
|
self
|
|
|
|
}
|
2019-11-07 09:08:59 +02:00
|
|
|
}
|
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
impl BuildXML for Run {
|
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-12-13 17:47:47 +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
|
|
|
}
|