2019-11-15 11:15:43 +02:00
|
|
|
use super::{Delete, Insert, ParagraphProperty, Run};
|
2019-11-07 09:08:59 +02:00
|
|
|
use crate::documents::BuildXML;
|
2019-11-11 06:05:07 +02:00
|
|
|
use crate::types::*;
|
2019-11-07 09:08:59 +02:00
|
|
|
use crate::xml_builder::*;
|
|
|
|
|
2019-11-12 11:57:16 +02:00
|
|
|
#[derive(Debug, Clone)]
|
2019-11-15 11:15:43 +02:00
|
|
|
pub struct Paragraph<'a> {
|
|
|
|
children: Vec<ParagraphChild<'a>>,
|
2019-11-11 06:05:07 +02:00
|
|
|
property: ParagraphProperty,
|
2019-11-13 10:52:02 +02:00
|
|
|
attrs: Vec<(String, String)>,
|
2019-11-07 09:08:59 +02:00
|
|
|
}
|
|
|
|
|
2019-11-15 11:15:43 +02:00
|
|
|
impl<'a> Default for Paragraph<'a> {
|
2019-11-07 09:08:59 +02:00
|
|
|
fn default() -> Self {
|
2019-11-11 06:05:07 +02:00
|
|
|
Self {
|
2019-11-15 11:15:43 +02:00
|
|
|
children: Vec::new(),
|
2019-11-11 06:05:07 +02:00
|
|
|
property: ParagraphProperty::new(),
|
2019-11-13 10:52:02 +02:00
|
|
|
attrs: Vec::new(),
|
2019-11-11 06:05:07 +02:00
|
|
|
}
|
2019-11-07 09:08:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-15 11:15:43 +02:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum ParagraphChild<'a> {
|
|
|
|
Run(Run),
|
|
|
|
Insert(Insert<'a>),
|
|
|
|
Delete(Delete<'a>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> BuildXML for ParagraphChild<'a> {
|
|
|
|
fn build(&self) -> Vec<u8> {
|
|
|
|
match self {
|
|
|
|
ParagraphChild::Run(v) => v.build(),
|
|
|
|
ParagraphChild::Insert(v) => v.build(),
|
|
|
|
ParagraphChild::Delete(v) => v.build(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Paragraph<'a> {
|
|
|
|
pub fn new() -> Paragraph<'a> {
|
2019-11-07 09:08:59 +02:00
|
|
|
Default::default()
|
|
|
|
}
|
|
|
|
|
2019-11-15 11:15:43 +02:00
|
|
|
pub fn add_run(mut self, run: Run) -> Paragraph<'a> {
|
|
|
|
self.children.push(ParagraphChild::Run(run));
|
2019-11-07 09:08:59 +02:00
|
|
|
self
|
|
|
|
}
|
2019-11-11 06:05:07 +02:00
|
|
|
|
2019-11-15 11:15:43 +02:00
|
|
|
pub fn add_insert(mut self, insert: Insert<'a>) -> Paragraph<'a> {
|
|
|
|
self.children.push(ParagraphChild::Insert(insert));
|
2019-11-13 10:52:02 +02:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-11-15 11:15:43 +02:00
|
|
|
pub fn add_delete(mut self, delete: Delete<'a>) -> Paragraph<'a> {
|
|
|
|
self.children.push(ParagraphChild::Delete(delete));
|
2019-11-11 06:05:07 +02:00
|
|
|
self
|
|
|
|
}
|
2019-11-11 08:36:26 +02:00
|
|
|
|
2019-11-15 11:15:43 +02:00
|
|
|
pub fn add_attr(mut self, key: impl Into<String>, val: impl Into<String>) -> Paragraph<'a> {
|
|
|
|
self.attrs.push((key.into(), val.into()));
|
2019-11-11 08:36:26 +02:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-11-15 11:15:43 +02:00
|
|
|
pub fn align(mut self, alignment_type: AlignmentType) -> Paragraph<'a> {
|
|
|
|
self.property = self.property.align(alignment_type);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
// pub fn size(mut self, size: usize) -> Paragraph<'a> {
|
|
|
|
// self.children = self.children.into_iter().map(|r| r.size(size)).collect();
|
|
|
|
// self
|
|
|
|
// }
|
|
|
|
|
|
|
|
pub fn style(mut self, style_id: &str) -> Paragraph<'a> {
|
2019-11-11 08:36:26 +02:00
|
|
|
self.property = self.property.style(style_id);
|
|
|
|
self
|
|
|
|
}
|
2019-11-11 09:48:28 +02:00
|
|
|
|
2019-11-15 11:15:43 +02:00
|
|
|
pub fn indent(
|
|
|
|
mut self,
|
|
|
|
left: usize,
|
|
|
|
special_indent: Option<SpecialIndentType>,
|
|
|
|
) -> Paragraph<'a> {
|
2019-11-11 09:48:28 +02:00
|
|
|
self.property = self.property.indent(left, special_indent);
|
|
|
|
self
|
|
|
|
}
|
2019-11-07 09:08:59 +02:00
|
|
|
}
|
|
|
|
|
2019-11-15 11:15:43 +02:00
|
|
|
impl<'a> BuildXML for Paragraph<'a> {
|
2019-11-07 09:08:59 +02:00
|
|
|
fn build(&self) -> Vec<u8> {
|
2019-11-07 10:31:04 +02:00
|
|
|
XMLBuilder::new()
|
2019-11-13 10:52:02 +02:00
|
|
|
.open_paragraph(&self.attrs)
|
2019-11-11 06:05:07 +02:00
|
|
|
.add_child(&self.property)
|
2019-11-15 11:15:43 +02:00
|
|
|
.add_children(&self.children)
|
2019-11-07 10:31:04 +02:00
|
|
|
.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_paragraph() {
|
2019-11-13 09:51:58 +02:00
|
|
|
let b = Paragraph::new()
|
|
|
|
.add_run(Run::new().add_text("Hello"))
|
|
|
|
.build();
|
2019-11-07 09:08:59 +02:00
|
|
|
assert_eq!(
|
|
|
|
str::from_utf8(&b).unwrap(),
|
2019-11-12 06:33:45 +02:00
|
|
|
r#"<w:p><w:pPr><w:pStyle w:val="Normal" /><w:rPr /></w:pPr><w:r><w:rPr /><w:t xml:space="preserve">Hello</w:t></w:r></w:p>"#
|
2019-11-07 09:08:59 +02:00
|
|
|
);
|
|
|
|
}
|
2019-11-11 08:36:26 +02:00
|
|
|
|
2019-11-13 10:52:02 +02:00
|
|
|
#[test]
|
|
|
|
fn test_custom_attr() {
|
|
|
|
let b = Paragraph::new()
|
|
|
|
.add_run(Run::new().add_text("Hello"))
|
|
|
|
.add_attr("customId", "abcd-1234-567890")
|
|
|
|
.build();
|
|
|
|
assert_eq!(
|
|
|
|
str::from_utf8(&b).unwrap(),
|
|
|
|
r#"<w:p customId="abcd-1234-567890"><w:pPr><w:pStyle w:val="Normal" /><w:rPr /></w:pPr><w:r><w:rPr /><w:t xml:space="preserve">Hello</w:t></w:r></w:p>"#
|
|
|
|
);
|
|
|
|
}
|
2019-11-07 09:08:59 +02:00
|
|
|
}
|