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

65 lines
1.3 KiB
Rust
Raw Normal View History

2019-11-11 06:05:07 +02:00
use super::{ParagraphProperty, Run, RunProperty, Text};
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-07 11:45:03 +02:00
#[derive(Debug)]
2019-11-07 09:08:59 +02:00
pub struct Paragraph {
runs: Vec<Run>,
2019-11-11 06:05:07 +02:00
property: ParagraphProperty,
2019-11-07 09:08:59 +02:00
}
impl Default for Paragraph {
fn default() -> Self {
2019-11-11 06:05:07 +02:00
Self {
runs: Vec::new(),
property: ParagraphProperty::new(),
}
2019-11-07 09:08:59 +02:00
}
}
impl Paragraph {
pub fn new() -> Paragraph {
Default::default()
}
pub fn add_run(mut self, run: Run) -> Paragraph {
self.runs.push(run);
self
}
2019-11-11 06:05:07 +02:00
pub fn align(mut self, alignment_type: AlignmentType) -> Paragraph {
self.property = self.property.align(alignment_type);
self
}
2019-11-07 09:08:59 +02:00
}
impl BuildXML for Paragraph {
fn build(&self) -> Vec<u8> {
2019-11-07 10:31:04 +02:00
XMLBuilder::new()
.open_paragraph()
2019-11-11 06:05:07 +02:00
.add_child(&self.property)
2019-11-07 10:31:04 +02:00
.add_children(&self.runs)
.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() {
let b = Paragraph::new().add_run(Run::new("Hello")).build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<w:p><w:r><w:rPr /><w:t>Hello</w:t></w:r></w:p>"#
);
}
}