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

76 lines
1.6 KiB
Rust
Raw Normal View History

use serde::Serialize;
2019-11-15 11:15:43 +02:00
use crate::documents::{BuildXML, HistoryId, Run};
use crate::xml_builder::*;
#[derive(Serialize, Debug, Clone, PartialEq)]
pub struct Delete {
pub author: String,
pub date: String,
pub runs: Vec<Run>,
2019-11-15 11:15:43 +02:00
}
impl Default for Delete {
fn default() -> Delete {
2019-11-15 11:15:43 +02:00
Delete {
author: "unnamed".to_owned(),
date: "1970-01-01T00:00:00Z".to_owned(),
runs: vec![],
2019-11-15 11:15:43 +02:00
}
}
}
impl Delete {
pub fn new(run: Run) -> Delete {
Self {
runs: vec![run],
..Default::default()
}
2019-11-15 11:15:43 +02:00
}
pub fn add_run(mut self, run: Run) -> Delete {
self.runs.push(run);
2019-11-15 11:15:43 +02:00
self
}
pub fn author(mut self, author: impl Into<String>) -> Delete {
self.author = author.into();
self
}
pub fn date(mut self, date: impl Into<String>) -> Delete {
self.date = date.into();
self
}
2019-11-15 11:15:43 +02:00
}
impl HistoryId for Delete {}
2019-11-15 11:15:43 +02:00
impl BuildXML for Delete {
2019-11-15 11:15:43 +02:00
fn build(&self) -> Vec<u8> {
XMLBuilder::new()
.open_delete(&self.generate(), &self.author, &self.date)
.add_children(&self.runs)
2019-11-15 11:15:43 +02:00
.close()
.build()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(test)]
use pretty_assertions::assert_eq;
use std::str;
#[test]
fn test_delete_default() {
let b = Delete::new(Run::new()).build();
2019-11-15 11:15:43 +02:00
assert_eq!(
str::from_utf8(&b).unwrap(),
2019-12-04 11:26:09 +02:00
r#"<w:del w:id="123" w:author="unnamed" w:date="1970-01-01T00:00:00Z"><w:r><w:rPr /></w:r></w:del>"#
2019-11-15 11:15:43 +02:00
);
}
}