From abd82c6f6b8785db926552c7119ecbe47b7aa9a3 Mon Sep 17 00:00:00 2001 From: Felix de Maneville Date: Thu, 3 Jun 2021 15:03:31 +0200 Subject: [PATCH] Clippy fixes Strong Xml 0.6 Document and body deref to the content vector renamed push methods to add_content to avoid conflict with deref --- .gitignore | 1 + Cargo.toml | 10 +++++----- examples/hello_world.rs | 6 +++--- examples/table.rs | 2 +- src/app.rs | 32 ++++++++++++++++---------------- src/core.rs | 14 +++++++------- src/document/body.rs | 31 ++++++++++++++----------------- src/document/mod.rs | 19 +++++++++++++++++-- src/docx.rs | 10 +++++----- src/lib.rs | 4 ++-- 10 files changed, 71 insertions(+), 58 deletions(-) diff --git a/.gitignore b/.gitignore index 04958ac..74ce282 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ **/*.rs.bk *.docx Cargo.lock +.idea diff --git a/Cargo.toml b/Cargo.toml index 5d9f923..6ae543a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,10 +10,10 @@ description = "A Rust library for parsing and generating docx files." keywords = ["docx", "generator", "openxml", "parser"] [dependencies] -derive_more = "0.99.5" -log = "0.4.8" -strong-xml = { version = "0.5.0", features = ["log"] } -zip = "0.5.5" +derive_more = "0.99" +log = "0.4" +strong-xml = { version = "0.6", features = ["log"] } +zip = "0.5" [dev-dependencies] -env_logger = "0.7.1" +env_logger = "0.8" diff --git a/examples/hello_world.rs b/examples/hello_world.rs index bcaed94..507f998 100644 --- a/examples/hello_world.rs +++ b/examples/hello_world.rs @@ -38,7 +38,7 @@ fn main() -> DocxResult<()> { .push_text("hello, world"), ); - docx.document.push(para); + docx.document.add_content(para); // Insert a centered paragraph with an outline let para = Paragraph::default() @@ -53,7 +53,7 @@ fn main() -> DocxResult<()> { .push_text("hello, world"), ); - docx.document.push(para); + docx.document.add_content(para); // Insert a right-aligned, italics paragraph let para = Paragraph::default() @@ -69,7 +69,7 @@ fn main() -> DocxResult<()> { .push_text("world"), ); - docx.document.push(para); + docx.document.add_content(para); docx.write_file("hello_world.docx")?; diff --git a/examples/table.rs b/examples/table.rs index 0d8bfb7..ba99926 100644 --- a/examples/table.rs +++ b/examples/table.rs @@ -24,7 +24,7 @@ fn main() -> DocxResult<()> { ); // Add the table to the document - docx.document.push(tbl); + docx.document.add_content(tbl); // Persist the document to a file docx.write_file("table.docx")?; diff --git a/src/app.rs b/src/app.rs index ef23b32..1f5de07 100644 --- a/src/app.rs +++ b/src/app.rs @@ -117,52 +117,52 @@ impl<'a> XmlWrite for App<'a> { } else { writer.write_element_end_open()?; if let Some(val) = template { - writer.write_flatten_text("Template", val)?; + writer.write_flatten_text("Template", val, false)?; } if let Some(val) = total_time { - writer.write_flatten_text("TotalTime", val)?; + writer.write_flatten_text("TotalTime", val, false)?; } if let Some(val) = pages { - writer.write_flatten_text("Pages", val)?; + writer.write_flatten_text("Pages", val, false)?; } if let Some(val) = words { - writer.write_flatten_text("Words", val)?; + writer.write_flatten_text("Words", val, false)?; } if let Some(val) = characters { - writer.write_flatten_text("Characters", val)?; + writer.write_flatten_text("Characters", val, false)?; } if let Some(val) = application { - writer.write_flatten_text("Application", val)?; + writer.write_flatten_text("Application", val, false)?; } if let Some(val) = doc_security { - writer.write_flatten_text("DocSecurity", val)?; + writer.write_flatten_text("DocSecurity", val, false)?; } if let Some(val) = lines { - writer.write_flatten_text("Lines", val)?; + writer.write_flatten_text("Lines", val, false)?; } if let Some(val) = paragraphs { - writer.write_flatten_text("Paragraphs", val)?; + writer.write_flatten_text("Paragraphs", val, false)?; } if let Some(val) = scale_crop { - writer.write_flatten_text("ScaleCrop", val)?; + writer.write_flatten_text("ScaleCrop", val, false)?; } if let Some(val) = company { - writer.write_flatten_text("Company", val)?; + writer.write_flatten_text("Company", val, false)?; } if let Some(val) = links_up_to_date { - writer.write_flatten_text("LinksUpToDate", val)?; + writer.write_flatten_text("LinksUpToDate", val, false)?; } if let Some(val) = characters_with_spaces { - writer.write_flatten_text("CharactersWithSpaces", val)?; + writer.write_flatten_text("CharactersWithSpaces", val, false)?; } if let Some(val) = shared_doc { - writer.write_flatten_text("SharedDoc", val)?; + writer.write_flatten_text("SharedDoc", val, false)?; } if let Some(val) = hyperlinks_changed { - writer.write_flatten_text("HyperlinksChanged", val)?; + writer.write_flatten_text("HyperlinksChanged", val, false)?; } if let Some(val) = app_version { - writer.write_flatten_text("AppVersion", val)?; + writer.write_flatten_text("AppVersion", val, false)?; } writer.write_element_end_close("Properties")?; } diff --git a/src/core.rs b/src/core.rs index e2b4b39..764d5ed 100644 --- a/src/core.rs +++ b/src/core.rs @@ -57,25 +57,25 @@ impl<'a> XmlWrite for Core<'a> { } else { writer.write_element_end_open()?; if let Some(val) = title { - writer.write_flatten_text("dc:title", val)?; + writer.write_flatten_text("dc:title", val, false)?; } if let Some(val) = subject { - writer.write_flatten_text("dc:subject", val)?; + writer.write_flatten_text("dc:subject", val, false)?; } if let Some(val) = creator { - writer.write_flatten_text("dc:creator", val)?; + writer.write_flatten_text("dc:creator", val, false)?; } if let Some(val) = keywords { - writer.write_flatten_text("cp:keywords", val)?; + writer.write_flatten_text("cp:keywords", val, false)?; } if let Some(val) = description { - writer.write_flatten_text("dc:description", val)?; + writer.write_flatten_text("dc:description", val, false)?; } if let Some(val) = last_modified_by { - writer.write_flatten_text("cp:lastModifiedBy", val)?; + writer.write_flatten_text("cp:lastModifiedBy", val, false)?; } if let Some(val) = revision { - writer.write_flatten_text("cp:revision", val)?; + writer.write_flatten_text("cp:revision", val, false)?; } writer.write_element_end_close("cp:coreProperties")?; } diff --git a/src/document/body.rs b/src/document/body.rs index f1595f6..b183778 100644 --- a/src/document/body.rs +++ b/src/document/body.rs @@ -3,6 +3,7 @@ use strong_xml::{XmlRead, XmlWrite}; use crate::__xml_test_suites; use crate::document::{Paragraph, Table}; +use std::ops::{Deref, DerefMut}; /// Document Body /// @@ -17,28 +18,24 @@ pub struct Body<'a> { } impl<'a> Body<'a> { - pub fn push>>(&mut self, content: T) -> &mut Self { + pub fn add_content>>(&mut self, content: T) -> &mut Self { self.content.push(content.into()); self } +} - // pub fn iter_text(&self) -> impl Iterator> { - // self.content - // .iter() - // .filter_map(|content| match content { - // BodyContent::Paragraph(para) => Some(para.iter_text()), - // }) - // .flatten() - // } +impl<'a> Deref for Body<'a> { + type Target = Vec>; - // pub fn iter_text_mut(&mut self) -> impl Iterator> { - // self.content - // .iter_mut() - // .filter_map(|content| match content { - // BodyContent::Paragraph(para) => Some(para.iter_text_mut()), - // }) - // .flatten() - // } + fn deref(&self) -> &Self::Target { + &self.content + } +} + +impl<'a> DerefMut for Body<'a> { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.content + } } /// A set of elements that can be contained in the body diff --git a/src/document/mod.rs b/src/document/mod.rs index 9fdbdb0..c4c2a11 100644 --- a/src/document/mod.rs +++ b/src/document/mod.rs @@ -26,6 +26,7 @@ use strong_xml::{XmlRead, XmlResult, XmlWrite, XmlWriter}; use crate::__xml_test_suites; use crate::schema::SCHEMA_MAIN; +use std::ops::{Deref, DerefMut}; /// The root element of the main document part. #[derive(Debug, Default, XmlRead)] @@ -38,12 +39,26 @@ pub struct Document<'a> { } impl<'a> Document<'a> { - pub fn push>>(&mut self, content: T) -> &mut Self { - self.body.push(content); + pub fn add_content>>(&mut self, content: T) -> &mut Self { + self.body.add_content(content); self } } +impl<'a> Deref for Document<'a> { + type Target = Body<'a>; + + fn deref(&self) -> &Self::Target { + &self.body + } +} + +impl<'a> DerefMut for Document<'a> { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.body + } +} + impl<'a> XmlWrite for Document<'a> { fn to_writer(&self, writer: &mut XmlWriter) -> XmlResult<()> { let Document { body } = self; diff --git a/src/docx.rs b/src/docx.rs index 4f0113d..b8f458d 100644 --- a/src/docx.rs +++ b/src/docx.rs @@ -165,8 +165,8 @@ impl DocxFile { app, content_types, core, - document_rels, document, + document_rels, font_table, rels, styles, @@ -180,7 +180,7 @@ impl DocxFile { } /// Parses content into `Docx` struct - pub fn parse<'a>(&'a self) -> DocxResult> { + pub fn parse(&self) -> DocxResult { let app = if let Some(content) = &self.app { Some(App::from_str(content)?) } else { @@ -220,13 +220,13 @@ impl DocxFile { Ok(Docx { app, - content_types, core, + content_types, document, - document_rels, font_table, - rels, styles, + rels, + document_rels, }) } } diff --git a/src/lib.rs b/src/lib.rs index 8378226..52a8514 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,7 +15,7 @@ //! //! // create a new paragraph and insert it //! let para = Paragraph::default().push_text("Lorem Ipsum"); -//! docx.document.push(para); +//! docx.document.add_content(para); //! //! docx.write_file("demo.docx").unwrap(); //! ``` @@ -40,7 +40,7 @@ //! let mut docx = docx.parse().unwrap(); //! //! let para = Paragraph::default().push_text("Lorem Ipsum"); -//! docx.document.push(para); +//! docx.document.add_content(para); //! //! docx.write_file("origin_appended.docx").unwrap(); //! ```