diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f95ce79..6f08d81 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -63,18 +63,18 @@ jobs: path: ~/.cargo/git key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }} - run: cargo build -# clippy: -# name: Clippy -# runs-on: ubuntu-latest -# steps: -# - uses: actions/checkout@v1 -# - uses: actions-rs/toolchain@v1 -# with: -# profile: minimal -# toolchain: stable -# override: true -# - run: rustup component add clippy -# - uses: actions-rs/cargo@v1 -# with: -# command: clippy -# args: -- -D warnings + lint: + name: Clippy + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v1 + - uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: stable + override: true + - run: rustup component add clippy + - uses: actions-rs/cargo@v1 + with: + command: clippy + args: -- -D warnings diff --git a/docx-core/src/documents/content_types.rs b/docx-core/src/documents/content_types.rs index f2643cf..0a1fa39 100644 --- a/docx-core/src/documents/content_types.rs +++ b/docx-core/src/documents/content_types.rs @@ -6,6 +6,12 @@ pub struct ContentTypes {} impl ContentTypes { pub fn new() -> ContentTypes { + Default::default() + } +} + +impl Default for ContentTypes { + fn default() -> Self { ContentTypes {} } } diff --git a/docx-core/src/documents/doc_props/app.rs b/docx-core/src/documents/doc_props/app.rs index 85f7931..3235bdc 100644 --- a/docx-core/src/documents/doc_props/app.rs +++ b/docx-core/src/documents/doc_props/app.rs @@ -6,7 +6,13 @@ pub struct AppProps {} impl AppProps { pub fn new() -> AppProps { - AppProps {} + Default::default() + } +} + +impl Default for AppProps { + fn default() -> Self { + Self {} } } diff --git a/docx-core/src/documents/doc_props/core.rs b/docx-core/src/documents/doc_props/core.rs index 7a4e466..eab3987 100644 --- a/docx-core/src/documents/doc_props/core.rs +++ b/docx-core/src/documents/doc_props/core.rs @@ -19,6 +19,30 @@ pub struct CorePropsConfig { title: Option, } +impl Default for CorePropsConfig { + fn default() -> Self { + Self { + created: None, + creator: None, + description: None, + language: None, + last_modified_by: None, + modified: None, + revision: None, + subject: None, + title: None, + } + } +} + +impl Default for CoreProps { + fn default() -> Self { + Self { + config: CorePropsConfig::default(), + } + } +} + impl CoreProps { pub(crate) fn new(config: CorePropsConfig) -> CoreProps { CoreProps { config } diff --git a/docx-core/src/documents/document_rels.rs b/docx-core/src/documents/document_rels.rs index 5a504a6..21855ce 100644 --- a/docx-core/src/documents/document_rels.rs +++ b/docx-core/src/documents/document_rels.rs @@ -9,7 +9,13 @@ pub struct DocumentRels { impl DocumentRels { pub fn new() -> DocumentRels { - DocumentRels { + Default::default() + } +} + +impl Default for DocumentRels { + fn default() -> Self { + Self { has_comments: false, has_numberings: false, } diff --git a/docx-core/src/documents/elements/bold.rs b/docx-core/src/documents/elements/bold.rs index 0bb3c68..2010689 100644 --- a/docx-core/src/documents/elements/bold.rs +++ b/docx-core/src/documents/elements/bold.rs @@ -6,7 +6,13 @@ pub struct Bold {} impl Bold { pub fn new() -> Bold { - Bold {} + Default::default() + } +} + +impl Default for Bold { + fn default() -> Self { + Self {} } } diff --git a/docx-core/src/documents/elements/bold_cs.rs b/docx-core/src/documents/elements/bold_cs.rs index 866a6c2..4e63d90 100644 --- a/docx-core/src/documents/elements/bold_cs.rs +++ b/docx-core/src/documents/elements/bold_cs.rs @@ -6,7 +6,13 @@ pub struct BoldCs {} impl BoldCs { pub fn new() -> BoldCs { - BoldCs {} + Default::default() + } +} + +impl Default for BoldCs { + fn default() -> Self { + Self {} } } diff --git a/docx-core/src/documents/elements/doc_defaults.rs b/docx-core/src/documents/elements/doc_defaults.rs index a68f261..1ede151 100644 --- a/docx-core/src/documents/elements/doc_defaults.rs +++ b/docx-core/src/documents/elements/doc_defaults.rs @@ -10,6 +10,12 @@ pub struct DocDefaults { impl DocDefaults { pub fn new() -> DocDefaults { + Default::default() + } +} + +impl Default for DocDefaults { + fn default() -> Self { let run_property_default = RunPropertyDefault::new(); DocDefaults { run_property_default, diff --git a/docx-core/src/documents/elements/italic.rs b/docx-core/src/documents/elements/italic.rs index 68ecaff..bfd755b 100644 --- a/docx-core/src/documents/elements/italic.rs +++ b/docx-core/src/documents/elements/italic.rs @@ -6,7 +6,13 @@ pub struct Italic {} impl Italic { pub fn new() -> Italic { - Italic {} + Default::default() + } +} + +impl Default for Italic { + fn default() -> Self { + Self {} } } diff --git a/docx-core/src/documents/elements/italic_cs.rs b/docx-core/src/documents/elements/italic_cs.rs index 1a62fff..f97cce1 100644 --- a/docx-core/src/documents/elements/italic_cs.rs +++ b/docx-core/src/documents/elements/italic_cs.rs @@ -6,7 +6,13 @@ pub struct ItalicCs {} impl ItalicCs { pub fn new() -> ItalicCs { - ItalicCs {} + Default::default() + } +} + +impl Default for ItalicCs { + fn default() -> Self { + Self {} } } diff --git a/docx-core/src/documents/elements/paragraph.rs b/docx-core/src/documents/elements/paragraph.rs index 26439ed..2bfc4ea 100644 --- a/docx-core/src/documents/elements/paragraph.rs +++ b/docx-core/src/documents/elements/paragraph.rs @@ -29,7 +29,7 @@ pub enum ParagraphChild { Delete(Delete), BookmarkStart(BookmarkStart), BookmarkEnd(BookmarkEnd), - CommentStart(CommentRangeStart), + CommentStart(Box), CommentEnd(CommentRangeEnd), } @@ -93,10 +93,9 @@ impl Paragraph { } pub fn add_comment_start(mut self, comment: Comment) -> Paragraph { - self.children - .push(ParagraphChild::CommentStart(CommentRangeStart::new( - comment, - ))); + self.children.push(ParagraphChild::CommentStart(Box::new( + CommentRangeStart::new(comment), + ))); self } diff --git a/docx-core/src/documents/elements/q_format.rs b/docx-core/src/documents/elements/q_format.rs index 3e1c8b4..7990dfc 100644 --- a/docx-core/src/documents/elements/q_format.rs +++ b/docx-core/src/documents/elements/q_format.rs @@ -11,7 +11,13 @@ pub struct QFormat {} impl QFormat { pub fn new() -> QFormat { - QFormat {} + Default::default() + } +} + +impl Default for QFormat { + fn default() -> Self { + Self {} } } diff --git a/docx-core/src/documents/elements/run_property_default.rs b/docx-core/src/documents/elements/run_property_default.rs index 0ceb60e..fb4f8c7 100644 --- a/docx-core/src/documents/elements/run_property_default.rs +++ b/docx-core/src/documents/elements/run_property_default.rs @@ -9,6 +9,12 @@ pub struct RunPropertyDefault { impl RunPropertyDefault { pub fn new() -> RunPropertyDefault { + Default::default() + } +} + +impl Default for RunPropertyDefault { + fn default() -> Self { let run_property = RunProperty::new(); RunPropertyDefault { run_property } } diff --git a/docx-core/src/documents/elements/tab.rs b/docx-core/src/documents/elements/tab.rs index bf3b33d..5002737 100644 --- a/docx-core/src/documents/elements/tab.rs +++ b/docx-core/src/documents/elements/tab.rs @@ -6,6 +6,12 @@ pub struct Tab {} impl Tab { pub fn new() -> Tab { + Default::default() + } +} + +impl Default for Tab { + fn default() -> Self { Tab {} } } diff --git a/docx-core/src/documents/elements/table_cell.rs b/docx-core/src/documents/elements/table_cell.rs index 4650c26..8bdf79c 100644 --- a/docx-core/src/documents/elements/table_cell.rs +++ b/docx-core/src/documents/elements/table_cell.rs @@ -17,13 +17,7 @@ pub enum TableCellContent { impl TableCell { pub fn new() -> TableCell { - let property = TableCellProperty::new(); - let contents = vec![]; - Self { - property, - contents, - has_numbering: false, - } + Default::default() } pub fn add_paragraph(mut self, p: Paragraph) -> TableCell { @@ -50,6 +44,18 @@ impl TableCell { } } +impl Default for TableCell { + fn default() -> Self { + let property = TableCellProperty::new(); + let contents = vec![]; + Self { + property, + contents, + has_numbering: false, + } + } +} + impl BuildXML for TableCell { fn build(&self) -> Vec { let b = XMLBuilder::new(); diff --git a/docx-core/src/documents/elements/table_cell_property.rs b/docx-core/src/documents/elements/table_cell_property.rs index cbc80ea..2034c7b 100644 --- a/docx-core/src/documents/elements/table_cell_property.rs +++ b/docx-core/src/documents/elements/table_cell_property.rs @@ -13,12 +13,7 @@ pub struct TableCellProperty { impl TableCellProperty { pub fn new() -> TableCellProperty { - TableCellProperty { - width: None, - borders: None, - grid_span: None, - vertical_merge: None, - } + Default::default() } pub fn width(mut self, v: usize, t: WidthType) -> TableCellProperty { @@ -37,6 +32,17 @@ impl TableCellProperty { } } +impl Default for TableCellProperty { + fn default() -> Self { + TableCellProperty { + width: None, + borders: None, + grid_span: None, + vertical_merge: None, + } + } +} + impl BuildXML for TableCellProperty { fn build(&self) -> Vec { XMLBuilder::new() diff --git a/docx-core/src/documents/elements/table_row_property.rs b/docx-core/src/documents/elements/table_row_property.rs index 127e512..51d618e 100644 --- a/docx-core/src/documents/elements/table_row_property.rs +++ b/docx-core/src/documents/elements/table_row_property.rs @@ -6,6 +6,12 @@ pub struct TableRowProperty {} impl TableRowProperty { pub fn new() -> TableRowProperty { + Default::default() + } +} + +impl Default for TableRowProperty { + fn default() -> Self { TableRowProperty {} } } diff --git a/docx-core/src/documents/elements/vanish.rs b/docx-core/src/documents/elements/vanish.rs index 3e6552d..c7ec549 100644 --- a/docx-core/src/documents/elements/vanish.rs +++ b/docx-core/src/documents/elements/vanish.rs @@ -10,6 +10,12 @@ impl Vanish { } } +impl Default for Vanish { + fn default() -> Self { + Vanish {} + } +} + impl BuildXML for Vanish { fn build(&self) -> Vec { let b = XMLBuilder::new(); diff --git a/docx-core/src/documents/history_id.rs b/docx-core/src/documents/history_id.rs index a025ffb..f69e297 100644 --- a/docx-core/src/documents/history_id.rs +++ b/docx-core/src/documents/history_id.rs @@ -1,12 +1,13 @@ -#[allow(unused)] -use std::sync::atomic::{AtomicUsize, Ordering}; - -#[allow(dead_code)] +#[cfg(not(test))] +use std::sync::atomic::AtomicUsize; +#[cfg(not(test))] static HISTORY_ID: AtomicUsize = AtomicUsize::new(0); #[cfg(not(test))] pub trait HistoryId { fn generate(&self) -> String { + use std::sync::atomic::Ordering; + let id = HISTORY_ID.load(Ordering::Relaxed); HISTORY_ID.store(id + 1, Ordering::Relaxed); format!("{}", id) diff --git a/docx-core/src/documents/mod.rs b/docx-core/src/documents/mod.rs index 05e19fa..cc59643 100644 --- a/docx-core/src/documents/mod.rs +++ b/docx-core/src/documents/mod.rs @@ -133,11 +133,8 @@ impl Docx { match child { DocumentChild::Paragraph(paragraph) => { for child in ¶graph.children { - match child { - ParagraphChild::CommentStart(c) => { - comments.push(c.comment()); - } - _ => {} + if let ParagraphChild::CommentStart(c) = child { + comments.push(c.comment()); } } } @@ -148,11 +145,8 @@ impl Docx { match content { TableCellContent::Paragraph(paragraph) => { for child in ¶graph.children { - match child { - ParagraphChild::CommentStart(c) => { - comments.push(c.comment()); - } - _ => {} + if let ParagraphChild::CommentStart(c) = child { + comments.push(c.comment()); } } } @@ -165,7 +159,7 @@ impl Docx { } // If this document has comments, set comments.xml to document_rels. // This is because comments.xml without comment cause an error on word online. - if comments.len() > 0 { + if !comments.is_empty() { self.document_rels.has_comments = true; } self.comments.add_comments(comments); diff --git a/docx-core/src/documents/rels.rs b/docx-core/src/documents/rels.rs index 752cae2..334e4b1 100644 --- a/docx-core/src/documents/rels.rs +++ b/docx-core/src/documents/rels.rs @@ -6,6 +6,12 @@ pub struct Rels {} impl Rels { pub fn new() -> Rels { + Default::default() + } +} + +impl Default for Rels { + fn default() -> Self { Rels {} } } @@ -24,12 +30,12 @@ impl BuildXML for Rels { "rId2", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties", "docProps/app.xml" - ) + ) .relationship( "rId3", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument", "word/document.xml" - ) + ) .close() .build() } @@ -39,9 +45,9 @@ impl BuildXML for Rels { mod tests { use super::*; - use std::str; #[cfg(test)] use pretty_assertions::assert_eq; + use std::str; #[test] fn test_build() { diff --git a/docx-core/src/documents/settings.rs b/docx-core/src/documents/settings.rs index a248eb2..778b245 100644 --- a/docx-core/src/documents/settings.rs +++ b/docx-core/src/documents/settings.rs @@ -1,4 +1,4 @@ -use super::{DefaultTabStop, Style, Zoom}; +use super::{DefaultTabStop, Zoom}; use crate::documents::BuildXML; use crate::xml_builder::*; diff --git a/docx-core/src/lib.rs b/docx-core/src/lib.rs index 0d83c35..4f5f35d 100644 --- a/docx-core/src/lib.rs +++ b/docx-core/src/lib.rs @@ -7,5 +7,4 @@ mod zipper; pub use documents::*; pub use errors::*; -pub(crate) use escape::*; pub use types::*; diff --git a/docx-core/src/xml_builder/core_properties.rs b/docx-core/src/xml_builder/core_properties.rs index 92e6fe8..906d2ff 100644 --- a/docx-core/src/xml_builder/core_properties.rs +++ b/docx-core/src/xml_builder/core_properties.rs @@ -3,7 +3,7 @@ use super::XmlEvent; impl XMLBuilder { // i.e. - opened_el!( + open!( open_core_properties, "cp:coreProperties", "xmlns:cp", @@ -12,13 +12,13 @@ impl XMLBuilder { "xmlns:dcmitype", "xmlns:xsi" ); - closed_el_with_child!(dcterms_created, "dcterms:created", "xsi:type"); - closed_el_with_child!(dc_creator, "dc:creator"); - closed_el_with_child!(dc_description, "dc:description"); - closed_el_with_child!(dc_language, "dc:language"); - closed_el_with_child!(cp_last_modified_by, "cp:lastModifiedBy"); - closed_el_with_child!(dcterms_modified, "dcterms:modified", "xsi:type"); - closed_el_with_child!(cp_revision, "cp:revision"); - closed_el_with_child!(dc_subject, "dc:subject"); - closed_el_with_child!(dc_title, "dc:title"); + closed_with_child!(dcterms_created, "dcterms:created", "xsi:type"); + closed_with_child!(dc_creator, "dc:creator"); + closed_with_child!(dc_description, "dc:description"); + closed_with_child!(dc_language, "dc:language"); + closed_with_child!(cp_last_modified_by, "cp:lastModifiedBy"); + closed_with_child!(dcterms_modified, "dcterms:modified", "xsi:type"); + closed_with_child!(cp_revision, "cp:revision"); + closed_with_child!(dc_subject, "dc:subject"); + closed_with_child!(dc_title, "dc:title"); } diff --git a/docx-core/src/xml_builder/elements.rs b/docx-core/src/xml_builder/elements.rs index 21c6f11..833a439 100644 --- a/docx-core/src/xml_builder/elements.rs +++ b/docx-core/src/xml_builder/elements.rs @@ -6,9 +6,9 @@ const EXPECT_MESSAGE: &str = "should write buf"; impl XMLBuilder { // i.e. - opened_el!(open_body, "w:body"); + open!(open_body, "w:body"); // i.e. - only_str_val_el!(based_on, "w:basedOn"); + closed_with_str!(based_on, "w:basedOn"); // i.e. pub(crate) fn text(mut self, text: &str, preserve_space: bool) -> Self { let space = if preserve_space { @@ -36,32 +36,32 @@ impl XMLBuilder { self.close() } // i.e. - opened_el!(open_run, "w:r"); - opened_el!(open_run_property, "w:rPr"); - opened_el!(open_run_property_default, "w:rPrDefault"); + open!(open_run, "w:r"); + open!(open_run_property, "w:rPr"); + open!(open_run_property_default, "w:rPrDefault"); // i.e. - closed_el!(q_format, "w:qFormat"); + closed!(q_format, "w:qFormat"); // i.e. - // opened_el!(open_paragraph, "w:p"); - opened_el_with_attrs!(open_paragraph, "w:p"); - opened_el!(open_paragraph_property, "w:pPr"); - opened_el!(open_doc_defaults, "w:docDefaults"); + // open!(open_paragraph, "w:p"); + open_with_attrs!(open_paragraph, "w:p"); + open!(open_paragraph_property, "w:pPr"); + open!(open_doc_defaults, "w:docDefaults"); // i.e. - only_str_val_el!(name, "w:name"); + closed_with_str!(name, "w:name"); // i.e. - only_str_val_el!(justification, "w:jc"); + closed_with_str!(justification, "w:jc"); // i.e. - only_str_val_el!(paragraph_style, "w:pStyle"); + closed_with_str!(paragraph_style, "w:pStyle"); // i.e. - only_usize_val_el!(sz, "w:sz"); + closed_with_usize!(sz, "w:sz"); // i.e. - only_usize_val_el!(sz_cs, "w:szCs"); + closed_with_usize!(sz_cs, "w:szCs"); - closed_el!(b, "w:b"); - closed_el!(b_cs, "w:bCs"); + closed!(b, "w:b"); + closed!(b_cs, "w:bCs"); - closed_el!(i, "w:i"); - closed_el!(i_cs, "w:iCs"); + closed!(i, "w:i"); + closed!(i_cs, "w:iCs"); // Build w:style element // i.e. pub(crate) fn open_style(mut self, style_type: StyleType, id: &str) -> Self { @@ -75,16 +75,16 @@ impl XMLBuilder { self } // i.e. - only_str_val_el!(next, "w:next"); + closed_with_str!(next, "w:next"); // i.e. - only_str_val_el!(color, "w:color"); + closed_with_str!(color, "w:color"); // i.e. - only_str_val_el!(highlight, "w:highlight"); + closed_with_str!(highlight, "w:highlight"); // i.e. - only_str_val_el!(underline, "w:u"); + closed_with_str!(underline, "w:u"); // i.e. pub(crate) fn indent(mut self, left: usize, special_indent: Option) -> Self { @@ -107,24 +107,24 @@ impl XMLBuilder { // // Table elements // - opened_el!(open_table, "w:tbl"); - opened_el!(open_table_property, "w:tblPr"); - opened_el!(open_table_grid, "w:tblGrid"); - opened_el!(open_table_row, "w:tr"); - opened_el!(open_table_row_property, "w:trPr"); - opened_el!(open_table_cell, "w:tc"); - opened_el!(open_table_cell_property, "w:tcPr"); - opened_el!(open_table_cell_borders, "w:tcBorders"); - opened_el!(open_table_borders, "w:tblBorders"); - opened_el!(open_table_cell_margins, "w:tblCellMar"); + open!(open_table, "w:tbl"); + open!(open_table_property, "w:tblPr"); + open!(open_table_grid, "w:tblGrid"); + open!(open_table_row, "w:tr"); + open!(open_table_row_property, "w:trPr"); + open!(open_table_cell, "w:tc"); + open!(open_table_cell_property, "w:tcPr"); + open!(open_table_cell_borders, "w:tcBorders"); + open!(open_table_borders, "w:tblBorders"); + open!(open_table_cell_margins, "w:tblCellMar"); closed_w_with_type_el!(table_width, "w:tblW"); closed_w_with_type_el!(table_indent, "w:tblInd"); closed_w_with_type_el!(grid_column, "w:gridCol"); closed_w_with_type_el!(table_cell_width, "w:tcW"); - only_usize_val_el!(grid_span, "w:gridSpan"); - only_str_val_el!(vertical_merge, "w:vMerge"); + closed_with_usize!(grid_span, "w:gridSpan"); + closed_with_str!(vertical_merge, "w:vMerge"); closed_w_with_type_el!(margin_top, "w:top"); closed_w_with_type_el!(margin_left, "w:left"); @@ -138,24 +138,24 @@ impl XMLBuilder { closed_border_el!(border_inside_h, "w:insideH"); closed_border_el!(border_inside_v, "w:insideV"); - closed_el!(shd, "w:shd", "w:fill", "w:val"); + closed!(shd, "w:shd", "w:fill", "w:val"); - closed_el!(tab, "w:tab"); - closed_el!(tab_with_pos, "w:tab", "w:val", "w:pos"); + closed!(tab, "w:tab"); + closed!(tab_with_pos, "w:tab", "w:val", "w:pos"); - closed_el!(br, "w:br", "w:type"); - closed_el!(zoom, "w:zoom", "w:percent"); - only_usize_val_el!(default_tab_stop, "w:defaultTabStop"); + closed!(br, "w:br", "w:type"); + closed!(zoom, "w:zoom", "w:percent"); + closed_with_usize!(default_tab_stop, "w:defaultTabStop"); - opened_el!(open_font, "w:font", "w:name"); - only_str_val_el!(pitch, "w:pitch"); - only_str_val_el!(family, "w:family"); - only_str_val_el!(charset, "w:charset"); + open!(open_font, "w:font", "w:name"); + closed_with_str!(pitch, "w:pitch"); + closed_with_str!(family, "w:family"); + closed_with_str!(charset, "w:charset"); - opened_el!(open_section_property, "w:sectPr"); - only_str_val_el!(type_tag, "w:type"); - closed_el!(page_size, "w:pgSz", "w:w", "w:h"); - closed_el!( + open!(open_section_property, "w:sectPr"); + closed_with_str!(type_tag, "w:type"); + closed!(page_size, "w:pgSz", "w:w", "w:h"); + closed!( page_margin, "w:pgMar", "w:top", @@ -166,19 +166,19 @@ impl XMLBuilder { "w:footer", "w:gutter" ); - closed_el!(columns, "w:cols", "w:space"); - closed_el!(document_grid, "w:docGrid", "w:type", "w:linePitch"); + closed!(columns, "w:cols", "w:space"); + closed!(document_grid, "w:docGrid", "w:type", "w:linePitch"); - opened_el!(open_insert, "w:ins", "w:id", "w:author", "w:date"); - opened_el!(open_delete, "w:del", "w:id", "w:author", "w:date"); + open!(open_insert, "w:ins", "w:id", "w:author", "w:date"); + open!(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!(bookmark_start, "w:bookmarkStart", "w:id", "w:name"); + closed!(bookmark_end, "w:bookmarkEnd", "w:id"); - closed_el!(comment_range_start, "w:commentRangeStart", "w:id"); - closed_el!(comment_range_end, "w:commentRangeEnd", "w:id"); - closed_el!(comment_reference, "w:commentReference", "w:id"); - opened_el!( + closed!(comment_range_start, "w:commentRangeStart", "w:id"); + closed!(comment_range_end, "w:commentRangeEnd", "w:id"); + closed!(comment_reference, "w:commentReference", "w:id"); + open!( open_comment, "w:comment", "w:id", @@ -187,19 +187,19 @@ impl XMLBuilder { "w:initials" ); - opened_el!(open_abstract_num, "w:abstractNum", "w:abstractNumId"); - opened_el!(open_level, "w:lvl", "w:ilvl"); - opened_el!(open_tabs, "w:tabs"); - opened_el!(open_num, "w:num", "w:numId"); - opened_el!(open_numbering_property, "w:numPr"); - only_usize_val_el!(indent_level, "w:ilvl"); - only_usize_val_el!(num_id, "w:numId"); - only_usize_val_el!(start, "w:start"); - only_str_val_el!(number_format, "w:numFmt"); - only_str_val_el!(level_text, "w:lvlText"); - only_str_val_el!(level_justification, "w:lvlJc"); - only_str_val_el!(abstract_num_id, "w:abstractNumId"); - closed_el!(vanish, "w:vanish"); + open!(open_abstract_num, "w:abstractNum", "w:abstractNumId"); + open!(open_level, "w:lvl", "w:ilvl"); + open!(open_tabs, "w:tabs"); + open!(open_num, "w:num", "w:numId"); + open!(open_numbering_property, "w:numPr"); + closed_with_usize!(indent_level, "w:ilvl"); + closed_with_usize!(num_id, "w:numId"); + closed_with_usize!(start, "w:start"); + closed_with_str!(number_format, "w:numFmt"); + closed_with_str!(level_text, "w:lvlText"); + closed_with_str!(level_justification, "w:lvlJc"); + closed_with_str!(abstract_num_id, "w:abstractNumId"); + closed!(vanish, "w:vanish"); } #[cfg(test)] diff --git a/docx-core/src/xml_builder/macros.rs b/docx-core/src/xml_builder/macros.rs index 937bca1..a3d4a43 100644 --- a/docx-core/src/xml_builder/macros.rs +++ b/docx-core/src/xml_builder/macros.rs @@ -1,5 +1,6 @@ -macro_rules! opened_el { +macro_rules! open { ($name: ident, $el_name: expr) => { + #[allow(dead_code)] pub(crate) fn $name(mut self) -> Self { self.writer .write(XmlEvent::start_element($el_name)) @@ -49,7 +50,7 @@ macro_rules! opened_el { }; } -macro_rules! opened_el_with_attrs { +macro_rules! open_with_attrs { ($name: ident, $el_name: expr) => { pub(crate) fn $name(mut self, attrs: &[(String, String)]) -> Self { let mut e = XmlEvent::start_element($el_name); @@ -57,7 +58,7 @@ macro_rules! opened_el_with_attrs { let mut key: &str = ""; #[allow(unused)] let mut val: &str = ""; - for attr in attrs { + for attr in attrs { key = &attr.0; val = &attr.1; e = e.attr(key, val); @@ -70,8 +71,9 @@ macro_rules! opened_el_with_attrs { }; } -macro_rules! closed_el_with_child { +macro_rules! closed_with_child { ($name: ident, $el_name: expr) => { + #[allow(dead_code)] pub(crate) fn $name(mut self, child: &str) -> Self { self.writer .write(XmlEvent::start_element($el_name)) @@ -117,7 +119,7 @@ macro_rules! closed_el_with_child { }; } -macro_rules! closed_el { +macro_rules! closed { ($name: ident, $el_name: expr) => { pub(crate) fn $name(mut self) -> Self { self.writer @@ -135,6 +137,7 @@ macro_rules! closed_el { } }; ($name: ident, $el_name: expr, $attr0: expr, $attr1: expr) => { + #[allow(dead_code)] pub(crate) fn $name(mut self, arg0: &str, arg1: &str) -> Self { self.writer .write(XmlEvent::start_element($el_name).attr($attr0, arg0).attr($attr1, arg1)) @@ -175,6 +178,7 @@ macro_rules! closed_el { } }; ($name: ident, $el_name: expr, $attr0: expr, $attr1: expr, $attr2: expr, $attr3: expr, $attr4: expr, $attr5: expr, $attr6: expr) => { + #[allow(clippy::too_many_arguments)] pub(crate) fn $name(mut self, arg0: &str, arg1: &str, arg2: &str, arg3: &str, arg4: &str, arg5: &str, arg6: &str) -> Self { self.writer .write(XmlEvent::start_element($el_name).attr($attr0, arg0).attr($attr1, arg1).attr($attr2, arg2).attr($attr3, arg3).attr($attr4, arg4).attr($attr5, arg5).attr($attr6, arg6)) @@ -183,6 +187,7 @@ macro_rules! closed_el { } }; ($name: ident, $el_name: expr, $attr0: expr, $attr1: expr, $attr2: expr, $attr3: expr, $attr4: expr, $attr5: expr, $attr6: expr, $attr7: expr) => { + #[allow(clippy::too_many_arguments)] pub(crate) fn $name(mut self, arg0: &str, arg1: &str, arg2: &str, arg3: &str, arg4: &str, arg5: &str, arg6: &str, arg7: &str) -> Self { self.writer .write(XmlEvent::start_element($el_name).attr($attr0, arg0).attr($attr1, arg1).attr($attr2, arg2).attr($attr3, arg3).attr($attr4, arg4).attr($attr5, arg5).attr($attr6, arg6).attr($attr7, arg7)) @@ -191,6 +196,7 @@ macro_rules! closed_el { } }; ($name: ident, $el_name: expr, $attr0: expr, $attr1: expr, $attr2: expr, $attr3: expr, $attr4: expr, $attr5: expr, $attr6: expr, $attr7: expr, $attr8: expr) => { + #[allow(clippy::too_many_arguments)] pub(crate) fn $name(mut self, arg0: &str, arg1: &str, arg2: &str, arg3: &str, arg4: &str, arg5: &str, arg6: &str, arg7: &str, arg8: &str) -> Self { self.writer .write(XmlEvent::start_element($el_name).attr($attr0, arg0).attr($attr1, arg1).attr($attr2, arg2).attr($attr3, arg3).attr($attr4, arg4).attr($attr5, arg5).attr($attr6, arg6).attr($attr7, arg7).attr($attr8, arg8)) @@ -200,8 +206,9 @@ macro_rules! closed_el { }; } -macro_rules! only_str_val_el { +macro_rules! closed_with_str { ($name: ident, $el_name: expr) => { + #[allow(dead_code)] pub(crate) fn $name(mut self, val: &str) -> Self { self.writer .write(XmlEvent::start_element($el_name).attr("w:val", val)) @@ -211,7 +218,7 @@ macro_rules! only_str_val_el { }; } -macro_rules! only_usize_val_el { +macro_rules! closed_with_usize { ($name: ident, $el_name: expr) => { pub(crate) fn $name(mut self, val: usize) -> Self { self.writer diff --git a/docx-core/src/xml_builder/properties.rs b/docx-core/src/xml_builder/properties.rs index c724d07..0e6a726 100644 --- a/docx-core/src/xml_builder/properties.rs +++ b/docx-core/src/xml_builder/properties.rs @@ -4,16 +4,16 @@ use super::XmlEvent; impl XMLBuilder { // Build Properties element // i.e. - opened_el!(open_properties, "Properties", "xmlns", "xmlns:vt"); + open!(open_properties, "Properties", "xmlns", "xmlns:vt"); - closed_el_with_child!(template, "Template"); - closed_el_with_child!(total_time, "TotalTime"); - closed_el_with_child!(application, "Application"); - closed_el_with_child!(pages, "Pages"); - closed_el_with_child!(words, "Words"); - closed_el_with_child!(characters, "Characters"); - closed_el_with_child!(characters_with_spaces, "CharactersWithSpaces"); - closed_el_with_child!(paragraphs, "Paragraphs"); + closed_with_child!(template, "Template"); + closed_with_child!(total_time, "TotalTime"); + closed_with_child!(application, "Application"); + closed_with_child!(pages, "Pages"); + closed_with_child!(words, "Words"); + closed_with_child!(characters, "Characters"); + closed_with_child!(characters_with_spaces, "CharactersWithSpaces"); + closed_with_child!(paragraphs, "Paragraphs"); } #[cfg(test)] diff --git a/docx-core/src/xml_builder/relationship.rs b/docx-core/src/xml_builder/relationship.rs index 1800177..3792638 100644 --- a/docx-core/src/xml_builder/relationship.rs +++ b/docx-core/src/xml_builder/relationship.rs @@ -4,10 +4,10 @@ use super::XmlEvent; impl XMLBuilder { // Build RelationShips element // i.e. - opened_el!(open_relationships, "Relationships", "xmlns"); + open!(open_relationships, "Relationships", "xmlns"); // Build Relationship - closed_el!(relationship, "Relationship", "Id", "Type", "Target"); + closed!(relationship, "Relationship", "Id", "Type", "Target"); } #[cfg(test)] diff --git a/docx-wasm/src/adaptors/special_indent.rs b/docx-wasm/src/adaptors/special_indent.rs index dbeb817..415a346 100644 --- a/docx-wasm/src/adaptors/special_indent.rs +++ b/docx-wasm/src/adaptors/special_indent.rs @@ -5,11 +5,7 @@ pub fn create_special_indent( special_indent_size: Option, ) -> Option { if let Some(kind) = special_indent_kind { - let size = if special_indent_size.is_some() { - special_indent_size.unwrap() - } else { - 0 - }; + let size = special_indent_size.unwrap_or_else(|| 0); match kind { docx_core::SpecialIndentKind::FirstLine => { Some(docx_core::SpecialIndentType::FirstLine(size)) diff --git a/docx-wasm/src/comment.rs b/docx-wasm/src/comment.rs index 47b307a..476ee59 100644 --- a/docx-wasm/src/comment.rs +++ b/docx-wasm/src/comment.rs @@ -20,12 +20,12 @@ impl Comment { #[wasm_bindgen] impl Comment { pub fn author(mut self, author: String) -> Comment { - self.0.author = author.into(); + self.0.author = author; self } pub fn date(mut self, date: String) -> Comment { - self.0.date = date.into(); + self.0.date = date; self } diff --git a/docx-wasm/src/paragraph.rs b/docx-wasm/src/paragraph.rs index eb8687e..ed1791f 100644 --- a/docx-wasm/src/paragraph.rs +++ b/docx-wasm/src/paragraph.rs @@ -51,9 +51,9 @@ impl Paragraph { pub fn add_comment_start(mut self, comment: Comment) -> Paragraph { self.0 .children - .push(docx_core::ParagraphChild::CommentStart( + .push(docx_core::ParagraphChild::CommentStart(Box::new( docx_core::CommentRangeStart::new(comment.take()), - )); + ))); self } diff --git a/makefile b/makefile index 2430124..c185a21 100644 --- a/makefile +++ b/makefile @@ -1,6 +1,9 @@ test: cargo test +lint: + cargo clippy --all-targets --all-features -- -D warnings + vrt: node vrt/index.js && reg-cli vrt/screenshot/actual vrt/screenshot/expected vrt/screenshot/diff -R vrt/report.html -I