From 51a2213fcf3621944dc6f7fc4261048b69f1e3fa Mon Sep 17 00:00:00 2001 From: Nathan West Date: Wed, 19 Aug 2020 06:25:54 -0400 Subject: [PATCH] Replaced all `impl Default` with `derive(Default)`, where possible (#1141) --- components/config/src/config/languages.rs | 8 +-- components/config/src/config/slugify.rs | 12 +--- components/config/src/config/taxonomies.rs | 14 +---- components/library/src/content/file_info.rs | 18 +----- components/library/src/content/page.rs | 58 +------------------ components/library/src/content/section.rs | 54 +---------------- components/link_checker/src/lib.rs | 2 +- components/rendering/src/table_of_contents.rs | 16 +---- components/utils/src/slugs.rs | 6 ++ 9 files changed, 18 insertions(+), 170 deletions(-) diff --git a/components/config/src/config/languages.rs b/components/config/src/config/languages.rs index 4a9195a2..dee61ffb 100644 --- a/components/config/src/config/languages.rs +++ b/components/config/src/config/languages.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use serde_derive::{Deserialize, Serialize}; -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] #[serde(default)] pub struct Language { /// The language code @@ -13,10 +13,4 @@ pub struct Language { pub search: bool, } -impl Default for Language { - fn default() -> Self { - Language { code: String::new(), feed: false, search: false } - } -} - pub type TranslateTerm = HashMap; diff --git a/components/config/src/config/slugify.rs b/components/config/src/config/slugify.rs index e3dd1615..dae4be61 100644 --- a/components/config/src/config/slugify.rs +++ b/components/config/src/config/slugify.rs @@ -2,20 +2,10 @@ use serde_derive::{Deserialize, Serialize}; use utils::slugs::SlugifyStrategy; -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] #[serde(default)] pub struct Slugify { pub paths: SlugifyStrategy, pub taxonomies: SlugifyStrategy, pub anchors: SlugifyStrategy, } - -impl Default for Slugify { - fn default() -> Self { - Slugify { - paths: SlugifyStrategy::On, - taxonomies: SlugifyStrategy::On, - anchors: SlugifyStrategy::On, - } - } -} diff --git a/components/config/src/config/taxonomies.rs b/components/config/src/config/taxonomies.rs index 2823eeb6..e18f54c6 100644 --- a/components/config/src/config/taxonomies.rs +++ b/components/config/src/config/taxonomies.rs @@ -1,6 +1,6 @@ use serde_derive::{Deserialize, Serialize}; -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] #[serde(default)] pub struct Taxonomy { /// The name used in the URL, usually the plural @@ -33,15 +33,3 @@ impl Taxonomy { } } } - -impl Default for Taxonomy { - fn default() -> Self { - Taxonomy { - name: String::new(), - paginate_by: None, - paginate_path: None, - feed: false, - lang: String::new(), - } - } -} diff --git a/components/library/src/content/file_info.rs b/components/library/src/content/file_info.rs index 3dc0a537..7cae3c11 100644 --- a/components/library/src/content/file_info.rs +++ b/components/library/src/content/file_info.rs @@ -27,7 +27,7 @@ pub fn find_content_components>(path: P) -> Vec { } /// Struct that contains all the information about the actual file -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Default, Clone, PartialEq)] pub struct FileInfo { /// The full path to the .md file pub path: PathBuf, @@ -143,22 +143,6 @@ impl FileInfo { } } -#[doc(hidden)] -impl Default for FileInfo { - fn default() -> FileInfo { - FileInfo { - path: PathBuf::new(), - parent: PathBuf::new(), - grand_parent: None, - filename: String::new(), - name: String::new(), - components: vec![], - relative: String::new(), - canonical: PathBuf::new(), - } - } -} - #[cfg(test)] mod tests { use std::path::{Path, PathBuf}; diff --git a/components/library/src/content/page.rs b/components/library/src/content/page.rs index a7233643..5e7578b1 100644 --- a/components/library/src/content/page.rs +++ b/components/library/src/content/page.rs @@ -29,7 +29,7 @@ lazy_static! { ).unwrap(); } -#[derive(Clone, Debug, PartialEq)] +#[derive(Clone, Debug, Default, PartialEq)] pub struct Page { /// All info about the actual file pub file: FileInfo, @@ -91,31 +91,7 @@ impl Page { pub fn new>(file_path: P, meta: PageFrontMatter, base_path: &PathBuf) -> Page { let file_path = file_path.as_ref(); - Page { - file: FileInfo::new_page(file_path, base_path), - meta, - ancestors: vec![], - raw_content: "".to_string(), - assets: vec![], - serialized_assets: vec![], - content: "".to_string(), - slug: "".to_string(), - path: "".to_string(), - components: vec![], - permalink: "".to_string(), - summary: None, - earlier: None, - later: None, - lighter: None, - heavier: None, - toc: vec![], - word_count: None, - reading_time: None, - lang: String::new(), - translations: Vec::new(), - internal_links_with_anchors: Vec::new(), - external_links: Vec::new(), - } + Page { file: FileInfo::new_page(file_path, base_path), meta, ..Self::default() } } pub fn is_draft(&self) -> bool { @@ -341,36 +317,6 @@ impl Page { } } -impl Default for Page { - fn default() -> Page { - Page { - file: FileInfo::default(), - meta: PageFrontMatter::default(), - ancestors: vec![], - raw_content: "".to_string(), - assets: vec![], - serialized_assets: vec![], - content: "".to_string(), - slug: "".to_string(), - path: "".to_string(), - components: vec![], - permalink: "".to_string(), - summary: None, - earlier: None, - later: None, - lighter: None, - heavier: None, - toc: vec![], - word_count: None, - reading_time: None, - lang: String::new(), - translations: Vec::new(), - internal_links_with_anchors: Vec::new(), - external_links: Vec::new(), - } - } -} - #[cfg(test)] mod tests { use std::collections::HashMap; diff --git a/components/library/src/content/section.rs b/components/library/src/content/section.rs index 16f4ee19..c3c5270f 100644 --- a/components/library/src/content/section.rs +++ b/components/library/src/content/section.rs @@ -17,7 +17,8 @@ use crate::content::has_anchor; use crate::content::ser::SerializingSection; use crate::library::Library; -#[derive(Clone, Debug, PartialEq)] +// Default is used to create a default index section if there is no _index.md in the root content directory +#[derive(Clone, Debug, Default, PartialEq)] pub struct Section { /// All info about the actual file pub file: FileInfo, @@ -74,28 +75,7 @@ impl Section { ) -> Section { let file_path = file_path.as_ref(); - Section { - file: FileInfo::new_section(file_path, base_path), - meta, - ancestors: vec![], - path: "".to_string(), - components: vec![], - permalink: "".to_string(), - raw_content: "".to_string(), - assets: vec![], - serialized_assets: vec![], - content: "".to_string(), - pages: vec![], - ignored_pages: vec![], - subsections: vec![], - toc: vec![], - word_count: None, - reading_time: None, - lang: String::new(), - translations: Vec::new(), - internal_links_with_anchors: Vec::new(), - external_links: Vec::new(), - } + Section { file: FileInfo::new_section(file_path, base_path), meta, ..Self::default() } } pub fn parse( @@ -266,34 +246,6 @@ impl Section { } } -/// Used to create a default index section if there is no _index.md in the root content directory -impl Default for Section { - fn default() -> Section { - Section { - file: FileInfo::default(), - meta: SectionFrontMatter::default(), - ancestors: vec![], - path: "".to_string(), - components: vec![], - permalink: "".to_string(), - raw_content: "".to_string(), - assets: vec![], - serialized_assets: vec![], - content: "".to_string(), - pages: vec![], - ignored_pages: vec![], - subsections: vec![], - toc: vec![], - reading_time: None, - word_count: None, - lang: String::new(), - translations: Vec::new(), - internal_links_with_anchors: Vec::new(), - external_links: Vec::new(), - } - } -} - #[cfg(test)] mod tests { use std::fs::{create_dir, File}; diff --git a/components/link_checker/src/lib.rs b/components/link_checker/src/lib.rs index d4336bca..1d56db2c 100644 --- a/components/link_checker/src/lib.rs +++ b/components/link_checker/src/lib.rs @@ -56,7 +56,7 @@ pub fn check_url(url: &str, config: &LinkChecker) -> Result { response.copy_to(&mut buf).unwrap(); match String::from_utf8(buf) { Ok(s) => s, - Err(_) => return Err("The page didn't return valid UTF-8".to_string()) + Err(_) => return Err("The page didn't return valid UTF-8".to_string()), } }; diff --git a/components/rendering/src/table_of_contents.rs b/components/rendering/src/table_of_contents.rs index 5f1d54ef..8e7773d0 100644 --- a/components/rendering/src/table_of_contents.rs +++ b/components/rendering/src/table_of_contents.rs @@ -1,7 +1,7 @@ use serde_derive::Serialize; /// Populated while receiving events from the markdown parser -#[derive(Debug, PartialEq, Clone, Serialize)] +#[derive(Debug, Default, PartialEq, Clone, Serialize)] pub struct Heading { pub level: u32, pub id: String, @@ -12,19 +12,7 @@ pub struct Heading { impl Heading { pub fn new(level: u32) -> Heading { - Heading { - level, - id: String::new(), - permalink: String::new(), - title: String::new(), - children: Vec::new(), - } - } -} - -impl Default for Heading { - fn default() -> Self { - Heading::new(0) + Heading { level, ..Self::default() } } } diff --git a/components/utils/src/slugs.rs b/components/utils/src/slugs.rs index 476eec54..f4ae57e7 100644 --- a/components/utils/src/slugs.rs +++ b/components/utils/src/slugs.rs @@ -11,6 +11,12 @@ pub enum SlugifyStrategy { Off, } +impl Default for SlugifyStrategy { + fn default() -> Self { + SlugifyStrategy::On + } +} + fn strip_chars(s: &str, chars: &str) -> String { let mut sanitized_string = s.to_string(); sanitized_string.retain(|c| !chars.contains(c));