Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docx-core/src/documents/elements/delete_instr_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ mod tests {
let b = DeleteInstrText::TOC(InstrToC::new().heading_styles_range(1, 3)).build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<w:delInstrText>TOC \o &quot;1-3&quot;</w:delInstrText>"#
r"<w:delInstrText>TOC \o &quot;1-3&quot;</w:delInstrText>"
);
}
}
2 changes: 1 addition & 1 deletion docx-core/src/documents/elements/instr_pageref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,6 @@ mod tests {
#[test]
fn test_page_ref() {
let b = InstrPAGEREF::new("_Toc00000000").hyperlink().build();
assert_eq!(str::from_utf8(&b).unwrap(), r#"PAGEREF _Toc00000000 \h"#);
assert_eq!(str::from_utf8(&b).unwrap(), r"PAGEREF _Toc00000000 \h");
}
}
4 changes: 2 additions & 2 deletions docx-core/src/documents/elements/instr_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ mod tests {
let b = Box::new(InstrText::TOC(InstrToC::new().heading_styles_range(1, 3))).build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<w:instrText>TOC \o &quot;1-3&quot;</w:instrText>"#
r"<w:instrText>TOC \o &quot;1-3&quot;</w:instrText>"
);
}

Expand All @@ -117,7 +117,7 @@ mod tests {
.build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<w:instrText>PAGEREF _Toc90425847 \h</w:instrText>"#
r"<w:instrText>PAGEREF _Toc90425847 \h</w:instrText>"
);
}
}
10 changes: 5 additions & 5 deletions docx-core/src/documents/elements/instr_toc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ mod tests {
#[test]
fn test_toc() {
let b = InstrToC::new().heading_styles_range(1, 3).build();
assert_eq!(str::from_utf8(&b).unwrap(), r#"TOC \o &quot;1-3&quot;"#);
assert_eq!(str::from_utf8(&b).unwrap(), r"TOC \o &quot;1-3&quot;");
}

#[test]
Expand All @@ -404,20 +404,20 @@ mod tests {
.build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"TOC \o &quot;1-3&quot; \t &quot;style1,2,style2,3&quot;"#
r"TOC \o &quot;1-3&quot; \t &quot;style1,2,style2,3&quot;"
);
}

#[test]
fn read_toc_with_o_and_h() {
let i = r#"TOC \o &quot;1-3&quot; \h"#;
let i = r"TOC \o &quot;1-3&quot; \h";
let i = InstrToC::from_str(i).unwrap();
assert_eq!(i, InstrToC::new().heading_styles_range(1, 3).hyperlink());
}

#[test]
fn read_toc_with_l_and_n() {
let i = r#"TOC \o &quot;1-3&quot; \l &quot;4-5&quot; \n &quot;1-4&quot; \h"#;
let i = r"TOC \o &quot;1-3&quot; \l &quot;4-5&quot; \n &quot;1-4&quot; \h";
let i = InstrToC::from_str(i).unwrap();
assert_eq!(
i,
Expand All @@ -431,7 +431,7 @@ mod tests {

#[test]
fn read_toc_with_a_and_b_and_t() {
let i = r#"TOC \a &quot;hoge&quot; \b &quot;test&quot; \o &quot;1-3&quot; \t &quot;MySpectacularStyle,1,MySpectacularStyle2,4&quot;"#;
let i = r"TOC \a &quot;hoge&quot; \b &quot;test&quot; \o &quot;1-3&quot; \t &quot;MySpectacularStyle,1,MySpectacularStyle2,4&quot;";
let i = InstrToC::from_str(i).unwrap();
assert_eq!(
i,
Expand Down
4 changes: 4 additions & 0 deletions docx-core/src/documents/elements/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ mod level_text;
mod line_spacing;
mod link;
mod mc_fallback;
mod move_from;
mod move_to;
mod name;
mod next;
mod num_pages;
Expand Down Expand Up @@ -199,6 +201,8 @@ pub use level_text::*;
pub use line_spacing::*;
pub use link::*;
pub use mc_fallback::*;
pub use move_from::*;
pub use move_to::*;
pub use name::*;
pub use next::*;
pub use num_pages::*;
Expand Down
133 changes: 133 additions & 0 deletions docx-core/src/documents/elements/move_from.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
use serde::ser::{SerializeStruct, Serializer};
use serde::Serialize;
use std::io::Write;

use crate::xml_builder::*;
use crate::{documents::*, escape};

#[derive(Serialize, Debug, Clone, PartialEq)]
pub struct MoveFrom {
pub author: String,
pub date: String,
pub children: Vec<MoveFromChild>,
}

#[derive(Debug, Clone, PartialEq)]
pub enum MoveFromChild {
Run(Run),
CommentStart(Box<CommentRangeStart>),
CommentEnd(CommentRangeEnd),
}

impl Serialize for MoveFromChild {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match *self {
MoveFromChild::Run(ref r) => {
let mut t = serializer.serialize_struct("Run", 2)?;
t.serialize_field("type", "run")?;
t.serialize_field("data", r)?;
t.end()
}
MoveFromChild::CommentStart(ref r) => {
let mut t = serializer.serialize_struct("CommentRangeStart", 2)?;
t.serialize_field("type", "commentRangeStart")?;
t.serialize_field("data", r)?;
t.end()
}
MoveFromChild::CommentEnd(ref r) => {
let mut t = serializer.serialize_struct("CommentRangeEnd", 2)?;
t.serialize_field("type", "commentRangeEnd")?;
t.serialize_field("data", r)?;
t.end()
}
}
}
}

impl Default for MoveFrom {
fn default() -> Self {
Self {
author: "unnamed".to_owned(),
date: "1970-01-01T00:00:00Z".to_owned(),
children: vec![],
}
}
}

impl MoveFrom {
pub fn new() -> Self {
Self {
children: vec![],
..Default::default()
}
}

pub fn add_run(mut self, run: Run) -> Self {
self.children.push(MoveFromChild::Run(run));
self
}

pub fn add_comment_start(mut self, comment: Comment) -> Self {
self.children.push(MoveFromChild::CommentStart(Box::new(
CommentRangeStart::new(comment),
)));
self
}

pub fn add_comment_end(mut self, id: usize) -> Self {
self.children
.push(MoveFromChild::CommentEnd(CommentRangeEnd::new(id)));
self
}

pub fn author(mut self, author: impl Into<String>) -> Self {
self.author = escape::escape(&author.into());
self
}

pub fn date(mut self, date: impl Into<String>) -> Self {
self.date = date.into();
self
}
}

impl HistoryId for MoveFrom {}

impl BuildXML for MoveFrom {
fn build_to<W: Write>(
&self,
stream: xml::writer::EventWriter<W>,
) -> xml::writer::Result<xml::writer::EventWriter<W>> {
let id = self.generate();
XMLBuilder::from(stream)
.open_move_from(&id, &self.author, &self.date)?
.apply_each(&self.children, |ch, b| match ch {
MoveFromChild::Run(t) => b.add_child(t),
MoveFromChild::CommentStart(c) => b.add_child(&c),
MoveFromChild::CommentEnd(c) => b.add_child(c),
})?
.close()?
.into_inner()
}
}

#[cfg(test)]
mod tests {

use super::*;
#[cfg(test)]
use pretty_assertions::assert_eq;
use std::str;

#[test]
fn test_move_from_default() {
let b = MoveFrom::new().add_run(Run::new()).build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<w:moveFrom w:id="123" w:author="unnamed" w:date="1970-01-01T00:00:00Z"><w:r><w:rPr /></w:r></w:moveFrom>"#
);
}
}
134 changes: 134 additions & 0 deletions docx-core/src/documents/elements/move_to.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
use serde::ser::{SerializeStruct, Serializer};
use serde::Serialize;
use std::io::Write;

use crate::xml_builder::*;
use crate::{documents::*, escape};

#[derive(Serialize, Debug, Clone, PartialEq)]
pub struct MoveTo {
pub author: String,
pub date: String,
pub children: Vec<MoveToChild>,
}

#[derive(Debug, Clone, PartialEq)]
pub enum MoveToChild {
Run(Run),
CommentStart(Box<CommentRangeStart>),
CommentEnd(CommentRangeEnd),
}

impl Serialize for MoveToChild {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match *self {
MoveToChild::Run(ref r) => {
let mut t = serializer.serialize_struct("Run", 2)?;
t.serialize_field("type", "run")?;
t.serialize_field("data", r)?;
t.end()
}
MoveToChild::CommentStart(ref r) => {
let mut t = serializer.serialize_struct("CommentRangeStart", 2)?;
t.serialize_field("type", "commentRangeStart")?;
t.serialize_field("data", r)?;
t.end()
}
MoveToChild::CommentEnd(ref r) => {
let mut t = serializer.serialize_struct("CommentRangeEnd", 2)?;
t.serialize_field("type", "commentRangeEnd")?;
t.serialize_field("data", r)?;
t.end()
}
}
}
}

impl Default for MoveTo {
fn default() -> Self {
Self {
author: "unnamed".to_owned(),
date: "1970-01-01T00:00:00Z".to_owned(),
children: vec![],
}
}
}

impl MoveTo {
pub fn new() -> Self {
Self {
children: vec![],
..Default::default()
}
}

pub fn add_run(mut self, run: Run) -> Self {
self.children.push(MoveToChild::Run(run));
self
}

pub fn add_comment_start(mut self, comment: Comment) -> Self {
self.children
.push(MoveToChild::CommentStart(Box::new(CommentRangeStart::new(
comment,
))));
self
}

pub fn add_comment_end(mut self, id: usize) -> Self {
self.children
.push(MoveToChild::CommentEnd(CommentRangeEnd::new(id)));
self
}

pub fn author(mut self, author: impl Into<String>) -> Self {
self.author = escape::escape(&author.into());
self
}

pub fn date(mut self, date: impl Into<String>) -> Self {
self.date = date.into();
self
}
}

impl HistoryId for MoveTo {}

impl BuildXML for MoveTo {
fn build_to<W: Write>(
&self,
stream: xml::writer::EventWriter<W>,
) -> xml::writer::Result<xml::writer::EventWriter<W>> {
let id = self.generate();
XMLBuilder::from(stream)
.open_move_to(&id, &self.author, &self.date)?
.apply_each(&self.children, |ch, b| match ch {
MoveToChild::Run(t) => b.add_child(t),
MoveToChild::CommentStart(c) => b.add_child(&c),
MoveToChild::CommentEnd(c) => b.add_child(c),
})?
.close()?
.into_inner()
}
}

#[cfg(test)]
mod tests {

use super::*;
#[cfg(test)]
use pretty_assertions::assert_eq;
use std::str;

#[test]
fn test_move_to_default() {
let b = MoveTo::new().add_run(Run::new()).build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<w:moveTo w:id="123" w:author="unnamed" w:date="1970-01-01T00:00:00Z"><w:r><w:rPr /></w:r></w:moveTo>"#
);
}
}
Loading