diff --git a/docx-core/src/documents/comment_id.rs b/docx-core/src/documents/comment_id.rs new file mode 100644 index 0000000..626af01 --- /dev/null +++ b/docx-core/src/documents/comment_id.rs @@ -0,0 +1,21 @@ +#[allow(unused)] +use std::sync::atomic::{AtomicUsize, Ordering}; + +#[allow(dead_code)] +static COMMENT_ID: AtomicUsize = AtomicUsize::new(0); + +#[cfg(not(test))] +pub trait CommentId { + fn generate(&self) -> String { + let id = COMMENT_ID.load(Ordering::Relaxed); + COMMENT_ID.store(id + 1, Ordering::Relaxed); + format!("{}", id) + } +} + +#[cfg(test)] +pub trait CommentId { + fn generate(&self) -> &str { + "123" + } +} diff --git a/docx-core/src/documents/elements/comment.rs b/docx-core/src/documents/elements/comment.rs new file mode 100644 index 0000000..de912d1 --- /dev/null +++ b/docx-core/src/documents/elements/comment.rs @@ -0,0 +1,62 @@ +use crate::documents::{BuildXML, Paragraph}; +use crate::xml_builder::*; + +#[derive(Debug, Clone)] +pub struct Comment<'a> { + id: &'a str, + author: &'a str, + date: &'a str, + paragraph: Paragraph<'a>, +} + +impl<'a> Default for Comment<'a> { + fn default() -> Comment<'a> { + Comment { + id: "invalidId", + author: "unnamed", + date: "1970-01-01T00:00:00Z", + paragraph: Paragraph::new(), + } + } +} + +impl<'a> Comment<'a> { + pub fn new(id: &'a str) -> Comment<'a> { + Self { + id, + ..Default::default() + } + } + + pub fn paragraph(mut self, p: Paragraph<'a>) -> Comment<'a> { + self.paragraph = p; + self + } +} + +impl<'a> BuildXML for Comment<'a> { + fn build(&self) -> Vec { + XMLBuilder::new() + .open_comment(&self.id, self.author, self.date) + .close() + .build() + } +} + +#[cfg(test)] +mod tests { + + use super::*; + #[cfg(test)] + use pretty_assertions::assert_eq; + use std::str; + + #[test] + fn test_ins_default() { + let b = Comment::new("123").build(); + assert_eq!( + str::from_utf8(&b).unwrap(), + r#""# + ); + } +} diff --git a/docx-core/src/documents/elements/delete.rs b/docx-core/src/documents/elements/delete.rs index c840df2..4f55096 100644 --- a/docx-core/src/documents/elements/delete.rs +++ b/docx-core/src/documents/elements/delete.rs @@ -5,7 +5,7 @@ use crate::xml_builder::*; pub struct Delete<'a> { author: &'a str, date: &'a str, - run: Run, + run: Run<'a>, } impl<'a> Default for Delete<'a> { @@ -23,7 +23,7 @@ impl<'a> Delete<'a> { Default::default() } - pub fn run(mut self, run: Run) -> Delete<'a> { + pub fn run(mut self, run: Run<'a>) -> Delete<'a> { self.run = run; self } @@ -54,7 +54,7 @@ mod tests { let b = Delete::new().build(); assert_eq!( str::from_utf8(&b).unwrap(), - r#""# + r#""# ); } } diff --git a/docx-core/src/documents/elements/insert.rs b/docx-core/src/documents/elements/insert.rs index 5cba8df..1848073 100644 --- a/docx-core/src/documents/elements/insert.rs +++ b/docx-core/src/documents/elements/insert.rs @@ -5,7 +5,7 @@ use crate::xml_builder::*; pub struct Insert<'a> { author: &'a str, date: &'a str, - run: Run, + run: Run<'a>, } impl<'a> Default for Insert<'a> { @@ -23,7 +23,7 @@ impl<'a> Insert<'a> { Default::default() } - pub fn run(mut self, run: Run) -> Insert<'a> { + pub fn run(mut self, run: Run<'a>) -> Insert<'a> { self.run = run; self } @@ -54,7 +54,7 @@ mod tests { let b = Insert::new().build(); assert_eq!( str::from_utf8(&b).unwrap(), - r#""# + r#""# ); } } diff --git a/docx-core/src/documents/elements/mod.rs b/docx-core/src/documents/elements/mod.rs index f1264ba..a9e1848 100644 --- a/docx-core/src/documents/elements/mod.rs +++ b/docx-core/src/documents/elements/mod.rs @@ -5,6 +5,7 @@ mod bookmark_end; mod bookmark_start; mod br; mod color; +mod comment; mod comment_range_end; mod comment_range_start; mod default_tab_stop; @@ -57,6 +58,7 @@ pub use bookmark_end::*; pub use bookmark_start::*; pub use br::*; pub use color::*; +pub use comment::*; pub use comment_range_end::*; pub use comment_range_start::*; pub use default_tab_stop::*; diff --git a/docx-core/src/documents/elements/paragraph.rs b/docx-core/src/documents/elements/paragraph.rs index 243d056..ab83433 100644 --- a/docx-core/src/documents/elements/paragraph.rs +++ b/docx-core/src/documents/elements/paragraph.rs @@ -22,7 +22,7 @@ impl<'a> Default for Paragraph<'a> { #[derive(Debug, Clone)] pub enum ParagraphChild<'a> { - Run(Run), + Run(Run<'a>), Insert(Insert<'a>), Delete(Delete<'a>), BookmarkStart(BookmarkStart<'a>), @@ -46,7 +46,7 @@ impl<'a> Paragraph<'a> { Default::default() } - pub fn add_run(mut self, run: Run) -> Paragraph<'a> { + pub fn add_run(mut self, run: Run<'a>) -> Paragraph<'a> { self.children.push(ParagraphChild::Run(run)); self } diff --git a/docx-core/src/documents/elements/run.rs b/docx-core/src/documents/elements/run.rs index 47b2b99..58beaed 100644 --- a/docx-core/src/documents/elements/run.rs +++ b/docx-core/src/documents/elements/run.rs @@ -1,20 +1,22 @@ -use super::{Break, DeleteText, RunProperty, Tab, Text}; +use super::{Break, Comment, DeleteText, RunProperty, Tab, Text}; use crate::documents::BuildXML; use crate::types::BreakType; use crate::xml_builder::*; #[derive(Debug, Clone)] -pub struct Run { +pub struct Run<'a> { run_property: RunProperty, + comment: Option>, children: Vec, } -impl Default for Run { +impl<'a> Default for Run<'a> { fn default() -> Self { let run_property = RunProperty::new(); Self { run_property, children: vec![], + comment: None, } } } @@ -27,65 +29,70 @@ pub enum RunChild { Break(Break), } -impl Run { - pub fn new() -> Run { +impl<'a> Run<'a> { + pub fn new() -> Run<'a> { Run { ..Default::default() } } - pub fn add_text(mut self, text: &str) -> Run { + pub fn add_text(mut self, text: &'a str) -> Run<'a> { self.children.push(RunChild::Text(Text::new(text))); self } - pub fn add_delete_text(mut self, text: &str) -> Run { + pub fn add_delete_text(mut self, text: &'a str) -> Run<'a> { self.children.push(RunChild::Text(Text::new(text))); self } - pub fn add_tab(mut self) -> Run { + pub fn add_tab(mut self) -> Run<'a> { self.children.push(RunChild::Tab(Tab::new())); self } - pub fn add_break(mut self, break_type: BreakType) -> Run { + pub fn add_break(mut self, break_type: BreakType) -> Run<'a> { self.children.push(RunChild::Break(Break::new(break_type))); self } - pub fn size(mut self, size: usize) -> Run { + pub fn size(mut self, size: usize) -> Run<'a> { self.run_property = self.run_property.size(size); self } - pub fn color(mut self, color: &str) -> Run { + pub fn color(mut self, color: &'a str) -> Run<'a> { self.run_property = self.run_property.color(color); self } - pub fn highlight(mut self, color: &str) -> Run { + pub fn highlight(mut self, color: &'a str) -> Run<'a> { self.run_property = self.run_property.highlight(color); self } - pub fn bold(mut self) -> Run { + pub fn bold(mut self) -> Run<'a> { self.run_property = self.run_property.bold(); self } - pub fn italic(mut self) -> Run { + pub fn italic(mut self) -> Run<'a> { self.run_property = self.run_property.italic(); self } - pub fn underline(mut self, line_type: &str) -> Run { + pub fn underline(mut self, line_type: &'a str) -> Run<'a> { self.run_property = self.run_property.underline(line_type); self } + + pub fn comment(mut self, line_type: &'a str) -> Run<'a> { + // self.run_property = self.run_property.underline(line_type); + self + } } -impl BuildXML for Run { +impl<'a> BuildXML for Run<'a> { fn build(&self) -> Vec { let b = XMLBuilder::new(); let mut b = b.open_run().add_child(&self.run_property); diff --git a/docx-core/src/xml_builder/elements.rs b/docx-core/src/xml_builder/elements.rs index fd89f1b..788dd14 100644 --- a/docx-core/src/xml_builder/elements.rs +++ b/docx-core/src/xml_builder/elements.rs @@ -165,14 +165,15 @@ impl XMLBuilder { "w:gutter" ); - opened_el!(open_insert, "w:ins", "w:id", "w:author", "w:data"); - opened_el!(open_delete, "w:del", "w:id", "w:author", "w:data"); + opened_el!(open_insert, "w:ins", "w:id", "w:author", "w:date"); + opened_el!(open_delete, "w:del", "w:id", "w:author", "w:date"); closed_el!(bookmark_start, "w:bookmarkStart", "w:id", "w:name"); closed_el!(bookmark_end, "w:bookmarkEnd", "w:id"); closed_el!(comment_range_start, "w:commentRangeStart", "w:id"); closed_el!(comment_range_end, "w:commentRangeEnd", "w:id"); + opened_el!(open_comment, "w:comment", "w:id", "w:author", "w:date"); } #[cfg(test)] diff --git a/fixtures/comment/comment.docx b/fixtures/comment/comment.docx index 2580bcb..d1a0bd3 100644 Binary files a/fixtures/comment/comment.docx and b/fixtures/comment/comment.docx differ diff --git a/fixtures/comment/docProps/core.xml b/fixtures/comment/docProps/core.xml index 4324eb5..f329c85 100644 --- a/fixtures/comment/docProps/core.xml +++ b/fixtures/comment/docProps/core.xml @@ -1,2 +1,2 @@ -2019-12-04T16:57:40Zja-JP2019-12-04T16:58:51Z1 \ No newline at end of file +2019-12-04T16:57:40Zja-JP2019-12-04T18:22:46Z2 \ No newline at end of file diff --git a/fixtures/comment/word/comments.xml b/fixtures/comment/word/comments.xml index 96e2148..92ca460 100644 --- a/fixtures/comment/word/comments.xml +++ b/fixtures/comment/word/comments.xml @@ -10,7 +10,7 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" mc:Ignorable="w14 wp14"> - + @@ -39,6 +39,73 @@ + Comment3 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Comment Added + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Hello world!! diff --git a/fixtures/comment/word/document.xml b/fixtures/comment/word/document.xml index 666ceb0..ecc3b9a 100644 --- a/fixtures/comment/word/document.xml +++ b/fixtures/comment/word/document.xml @@ -18,12 +18,18 @@ - Comment is + Comment - here + is + + + + + + her @@ -32,6 +38,24 @@ + + + e + + + + + + + + + + + + + + + . Comment Example @@ -44,6 +68,7 @@ + \ No newline at end of file diff --git a/fixtures/comment/word/fontTable.xml b/fixtures/comment/word/fontTable.xml index 94f56db..3916a0e 100644 --- a/fixtures/comment/word/fontTable.xml +++ b/fixtures/comment/word/fontTable.xml @@ -1,2 +1,2 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/fixtures/comment/word/settings.xml b/fixtures/comment/word/settings.xml index 97dba84..c338dcc 100644 --- a/fixtures/comment/word/settings.xml +++ b/fixtures/comment/word/settings.xml @@ -1,2 +1,2 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/fixtures/comment/word/styles.xml b/fixtures/comment/word/styles.xml index 72e6f53..0fc77af 100644 --- a/fixtures/comment/word/styles.xml +++ b/fixtures/comment/word/styles.xml @@ -1,2 +1,2 @@ - \ No newline at end of file + \ No newline at end of file