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

288 lines
9.6 KiB
Rust
Raw Normal View History

use serde::ser::{SerializeStruct, Serializer};
use serde::Serialize;
2019-12-05 08:44:18 +02:00
use super::*;
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::*;
#[derive(Serialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Paragraph {
pub children: Vec<ParagraphChild>,
pub property: ParagraphProperty,
pub has_numbering: bool,
pub attrs: Vec<(String, String)>,
2019-11-07 09:08:59 +02:00
}
impl Default for Paragraph {
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(),
has_numbering: false,
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
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum ParagraphChild {
Run(Run),
Insert(Insert),
Delete(Delete),
BookmarkStart(BookmarkStart),
BookmarkEnd(BookmarkEnd),
CommentStart(Box<CommentRangeStart>),
CommentEnd(CommentRangeEnd),
2019-11-15 11:15:43 +02:00
}
impl BuildXML for ParagraphChild {
2019-11-15 11:15:43 +02:00
fn build(&self) -> Vec<u8> {
match self {
ParagraphChild::Run(v) => v.build(),
ParagraphChild::Insert(v) => v.build(),
ParagraphChild::Delete(v) => v.build(),
2019-12-04 09:55:03 +02:00
ParagraphChild::BookmarkStart(v) => v.build(),
ParagraphChild::BookmarkEnd(v) => v.build(),
2019-12-05 08:44:18 +02:00
ParagraphChild::CommentStart(v) => v.build(),
ParagraphChild::CommentEnd(v) => v.build(),
2019-11-15 11:15:43 +02:00
}
}
}
impl Serialize for ParagraphChild {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match *self {
ParagraphChild::Run(ref r) => {
let mut t = serializer.serialize_struct("Run", 2)?;
t.serialize_field("type", "run")?;
t.serialize_field("data", r)?;
t.end()
}
ParagraphChild::Insert(ref r) => {
let mut t = serializer.serialize_struct("Insert", 2)?;
t.serialize_field("type", "insert")?;
t.serialize_field("data", r)?;
t.end()
}
ParagraphChild::Delete(ref r) => {
let mut t = serializer.serialize_struct("Delete", 2)?;
t.serialize_field("type", "delete")?;
t.serialize_field("data", r)?;
t.end()
}
ParagraphChild::BookmarkStart(ref r) => {
let mut t = serializer.serialize_struct("BookmarkStart", 2)?;
t.serialize_field("type", "bookmarkStart")?;
t.serialize_field("data", r)?;
t.end()
}
ParagraphChild::BookmarkEnd(ref r) => {
let mut t = serializer.serialize_struct("BookmarkEnd", 2)?;
t.serialize_field("type", "bookmarkEnd")?;
t.serialize_field("data", r)?;
t.end()
}
// TODO: Add comment later
_ => {
let mut t = serializer.serialize_struct("Unsupported", 2)?;
t.serialize_field("type", "unsupported")?;
t.end()
}
}
}
}
impl Paragraph {
pub fn new() -> Paragraph {
2019-11-07 09:08:59 +02:00
Default::default()
}
2019-12-05 08:44:18 +02:00
pub fn children(&self) -> &Vec<ParagraphChild> {
&self.children
}
pub fn add_run(mut self, run: Run) -> Paragraph {
2019-11-15 11:15:43 +02:00
self.children.push(ParagraphChild::Run(run));
2019-11-07 09:08:59 +02:00
self
}
2019-11-11 06:05:07 +02:00
pub fn add_insert(mut self, insert: Insert) -> Paragraph {
2019-11-15 11:15:43 +02:00
self.children.push(ParagraphChild::Insert(insert));
2019-11-13 10:52:02 +02:00
self
}
pub fn add_delete(mut self, delete: Delete) -> Paragraph {
2019-11-15 11:15:43 +02:00
self.children.push(ParagraphChild::Delete(delete));
2019-11-11 06:05:07 +02:00
self
}
2019-11-11 08:36:26 +02:00
pub fn add_attr(mut self, key: impl Into<String>, val: impl Into<String>) -> Paragraph {
2019-11-15 11:15:43 +02:00
self.attrs.push((key.into(), val.into()));
2019-11-11 08:36:26 +02:00
self
}
pub fn add_bookmark_start(mut self, id: usize, name: impl Into<String>) -> Paragraph {
2019-12-04 09:55:03 +02:00
self.children
.push(ParagraphChild::BookmarkStart(BookmarkStart::new(id, name)));
self
}
pub fn add_bookmark_end(mut self, id: usize) -> Paragraph {
2019-12-04 09:55:03 +02:00
self.children
.push(ParagraphChild::BookmarkEnd(BookmarkEnd::new(id)));
self
}
pub fn add_comment_start(mut self, comment: Comment) -> Paragraph {
self.children.push(ParagraphChild::CommentStart(Box::new(
CommentRangeStart::new(comment),
)));
2019-12-05 08:44:18 +02:00
self
}
pub fn add_comment_end(mut self, id: usize) -> Paragraph {
2019-12-05 08:44:18 +02:00
self.children
.push(ParagraphChild::CommentEnd(CommentRangeEnd::new(id)));
self
}
pub fn align(mut self, alignment_type: AlignmentType) -> Paragraph {
2019-11-15 11:15:43 +02:00
self.property = self.property.align(alignment_type);
self
}
pub fn style(mut self, style_id: &str) -> Paragraph {
2019-11-11 08:36:26 +02:00
self.property = self.property.style(style_id);
self
}
2019-11-11 09:48:28 +02:00
pub fn indent(
mut self,
left: usize,
special_indent: Option<SpecialIndentType>,
end: Option<usize>,
) -> Paragraph {
self.property = self.property.indent(left, special_indent, end);
2019-11-11 09:48:28 +02:00
self
}
2019-12-06 12:18:48 +02:00
2019-12-06 19:15:21 +02:00
pub fn numbering(mut self, id: NumberingId, level: IndentLevel) -> Self {
2019-12-06 12:18:48 +02:00
self.property = self.property.numbering(id, level);
self.has_numbering = true;
2019-12-06 12:18:48 +02:00
self
}
2019-11-07 09:08:59 +02:00
}
impl BuildXML for Paragraph {
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-12-04 09:55:03 +02:00
#[test]
fn test_bookmark() {
let b = Paragraph::new()
.add_bookmark_start(0, "article")
2019-12-04 09:55:03 +02:00
.add_run(Run::new().add_text("Hello"))
.add_bookmark_end(0)
2019-12-04 09:55:03 +02:00
.build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<w:p><w:pPr><w:pStyle w:val="Normal" /><w:rPr /></w:pPr><w:bookmarkStart w:id="0" w:name="article" /><w:r><w:rPr /><w:t xml:space="preserve">Hello</w:t></w:r><w:bookmarkEnd w:id="0" /></w:p>"#
2019-12-04 09:55:03 +02:00
);
}
2019-12-05 08:44:18 +02:00
#[test]
fn test_comment() {
let b = Paragraph::new()
.add_comment_start(Comment::new(1))
2019-12-05 08:44:18 +02:00
.add_run(Run::new().add_text("Hello"))
.add_comment_end(1)
2019-12-05 08:44:18 +02:00
.build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<w:p><w:pPr><w:pStyle w:val="Normal" /><w:rPr /></w:pPr><w:commentRangeStart w:id="1" /><w:r><w:rPr /><w:t xml:space="preserve">Hello</w:t></w:r><w:r>
2019-12-06 12:18:48 +02:00
<w:rPr />
</w:r>
<w:commentRangeEnd w:id="1" />
2019-12-05 08:44:18 +02:00
<w:r>
<w:commentReference w:id="1" />
2019-12-05 08:44:18 +02:00
</w:r></w:p>"#
);
}
2019-12-06 12:18:48 +02:00
#[test]
fn test_numbering() {
let b = Paragraph::new()
.add_run(Run::new().add_text("Hello"))
2019-12-06 19:15:21 +02:00
.numbering(NumberingId::new(0), IndentLevel::new(1))
2019-12-06 12:18:48 +02:00
.build();
assert_eq!(
str::from_utf8(&b).unwrap(),
2019-12-06 19:15:21 +02:00
r#"<w:p><w:pPr><w:pStyle w:val="Normal" /><w:rPr /><w:numPr><w:numId w:val="0" /><w:ilvl w:val="1" /></w:numPr></w:pPr><w:r><w:rPr /><w:t xml:space="preserve">Hello</w:t></w:r></w:p>"#
2019-12-06 12:18:48 +02:00
);
}
#[test]
fn test_paragraph_run_json() {
let run = Run::new().add_text("Hello");
let p = Paragraph::new().add_run(run);
assert_eq!(
serde_json::to_string(&p).unwrap(),
r#"{"children":[{"type":"run","data":{"runProperty":{"sz":null,"szCs":null,"color":null,"highlight":null,"underline":null,"bold":null,"boldCs":null,"italic":null,"italicCs":null,"vanish":null},"children":[{"type":"text","data":{"preserveSpace":true,"text":"Hello"}}]}}],"property":{"runProperty":{"sz":null,"szCs":null,"color":null,"highlight":null,"underline":null,"bold":null,"boldCs":null,"italic":null,"italicCs":null,"vanish":null},"style":"Normal","numberingProperty":null,"alignment":null,"indent":null},"hasNumbering":false,"attrs":[]}"#
);
}
#[test]
fn test_paragraph_insert_json() {
let run = Run::new().add_text("Hello");
let ins = Insert::new(run);
let p = Paragraph::new().add_insert(ins);
assert_eq!(
serde_json::to_string(&p).unwrap(),
r#"{"children":[{"type":"insert","data":{"run":{"runProperty":{"sz":null,"szCs":null,"color":null,"highlight":null,"underline":null,"bold":null,"boldCs":null,"italic":null,"italicCs":null,"vanish":null},"children":[{"type":"text","data":{"preserveSpace":true,"text":"Hello"}}]},"author":"unnamed","date":"1970-01-01T00:00:00Z"}}],"property":{"runProperty":{"sz":null,"szCs":null,"color":null,"highlight":null,"underline":null,"bold":null,"boldCs":null,"italic":null,"italicCs":null,"vanish":null},"style":"Normal","numberingProperty":null,"alignment":null,"indent":null},"hasNumbering":false,"attrs":[]}"#
);
}
2019-11-07 09:08:59 +02:00
}