From 18bef00671a925233e24e5f9713021a308a98c30 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Mon, 16 Jul 2018 18:14:48 +0200 Subject: [PATCH] Rename taxonomies paginate and error on unknown ones --- components/config/src/lib.rs | 6 ++--- components/pagination/src/lib.rs | 4 +-- components/rebuild/src/lib.rs | 6 ++--- components/site/src/lib.rs | 10 ++++--- components/site/tests/site.rs | 2 +- components/taxonomies/src/lib.rs | 27 ++++++++++++++++--- .../documentation/content/taxonomies.md | 2 +- .../getting-started/configuration.md | 2 +- 8 files changed, 40 insertions(+), 19 deletions(-) diff --git a/components/config/src/lib.rs b/components/config/src/lib.rs index 00234e8a..3efa7abb 100644 --- a/components/config/src/lib.rs +++ b/components/config/src/lib.rs @@ -35,7 +35,7 @@ pub struct Taxonomy { pub name: String, /// If this is set, the list of individual taxonomy term page will be paginated /// by this much - pub paginate: Option, + pub paginate_by: Option, pub paginate_path: Option, /// Whether to generate a RSS feed only for each taxonomy term, defaults to false pub rss: bool, @@ -43,7 +43,7 @@ pub struct Taxonomy { impl Taxonomy { pub fn is_paginated(&self) -> bool { - if let Some(paginate_by) = self.paginate { + if let Some(paginate_by) = self.paginate_by { paginate_by > 0 } else { false @@ -55,7 +55,7 @@ impl Default for Taxonomy { fn default() -> Taxonomy { Taxonomy { name: String::new(), - paginate: None, + paginate_by: None, paginate_path: None, rss: false, } diff --git a/components/pagination/src/lib.rs b/components/pagination/src/lib.rs index dc0c1404..16aa1fc7 100644 --- a/components/pagination/src/lib.rs +++ b/components/pagination/src/lib.rs @@ -104,7 +104,7 @@ impl<'a> Paginator<'a> { /// Create a new paginator from a taxonomy /// It will always at least create one pager (the first) even if there are no pages to paginate pub fn from_taxonomy(taxonomy: &'a Taxonomy, item: &'a TaxonomyItem) -> Paginator<'a> { - let paginate_by = taxonomy.kind.paginate.unwrap(); + let paginate_by = taxonomy.kind.paginate_by.unwrap(); let mut paginator = Paginator { all_pages: &item.pages, pagers: vec![], @@ -339,7 +339,7 @@ mod tests { ]; let taxonomy_def = TaxonomyConfig { name: "tags".to_string(), - paginate: Some(2), + paginate_by: Some(2), ..TaxonomyConfig::default() }; let taxonomy_item = TaxonomyItem { diff --git a/components/rebuild/src/lib.rs b/components/rebuild/src/lib.rs index 675388a0..b438776c 100644 --- a/components/rebuild/src/lib.rs +++ b/components/rebuild/src/lib.rs @@ -112,7 +112,7 @@ fn delete_element(site: &mut Site, path: &Path, is_section: bool) -> Result<()> site.permalinks.remove(&p.file.relative); if !p.meta.taxonomies.is_empty() { - site.populate_taxonomies(); + site.populate_taxonomies()?; } // if there is a parent section, we will need to re-render it @@ -207,7 +207,7 @@ fn handle_page_editing(site: &mut Site, path: &Path) -> Result<()> { // Sort always comes first if present so the rendering will be fine match changes { PageChangesNeeded::Taxonomies => { - site.populate_taxonomies(); + site.populate_taxonomies()?; site.register_tera_global_fns(); site.render_taxonomies()?; }, @@ -240,7 +240,7 @@ fn handle_page_editing(site: &mut Site, path: &Path) -> Result<()> { // It's a new page! None => { site.populate_sections(); - site.populate_taxonomies(); + site.populate_taxonomies()?; site.register_tera_global_fns(); // No need to optimise that yet, we can revisit if it becomes an issue site.build() diff --git a/components/site/src/lib.rs b/components/site/src/lib.rs index bfe72653..200a20e0 100644 --- a/components/site/src/lib.rs +++ b/components/site/src/lib.rs @@ -246,7 +246,7 @@ impl Site { self.register_early_global_fns(); self.render_markdown()?; self.populate_sections(); - self.populate_taxonomies(); + self.populate_taxonomies()?; self.register_tera_global_fns(); Ok(()) @@ -407,9 +407,9 @@ impl Site { } /// Find all the tags and categories if it's asked in the config - pub fn populate_taxonomies(&mut self) { + pub fn populate_taxonomies(&mut self) -> Result<()> { if self.config.taxonomies.is_empty() { - return; + return Ok(()); } self.taxonomies = find_taxonomies( @@ -420,7 +420,9 @@ impl Site { .cloned() .collect::>() .as_slice() - ); + )?; + + Ok(()) } /// Inject live reload script tag if in live reload mode diff --git a/components/site/tests/site.rs b/components/site/tests/site.rs index 3d030ee0..3f44da6a 100644 --- a/components/site/tests/site.rs +++ b/components/site/tests/site.rs @@ -223,7 +223,7 @@ fn can_build_site_with_taxonomies() { taxonomies }; } - site.populate_taxonomies(); + site.populate_taxonomies().unwrap(); let tmp_dir = tempdir().expect("create temp dir"); let public = &tmp_dir.path().join("public"); site.set_output_path(&public); diff --git a/components/taxonomies/src/lib.rs b/components/taxonomies/src/lib.rs index f576c843..9a7d037b 100644 --- a/components/taxonomies/src/lib.rs +++ b/components/taxonomies/src/lib.rs @@ -3,6 +3,7 @@ extern crate serde_derive; extern crate tera; extern crate slug; +#[macro_use] extern crate errors; extern crate config; extern crate content; @@ -110,7 +111,7 @@ impl Taxonomy { } } -pub fn find_taxonomies(config: &Config, all_pages: &[Page]) -> Vec { +pub fn find_taxonomies(config: &Config, all_pages: &[Page]) -> Result> { let taxonomies_def = { let mut m = HashMap::new(); for t in &config.taxonomies { @@ -136,7 +137,7 @@ pub fn find_taxonomies(config: &Config, all_pages: &[Page]) -> Vec { .push(page.clone()); } } else { - // TODO: bail with error + bail!("Page `{}` has taxonomy `{}` which is not defined in config.toml", page.file.path.display(), name); } } } @@ -147,7 +148,7 @@ pub fn find_taxonomies(config: &Config, all_pages: &[Page]) -> Vec { taxonomies.push(Taxonomy::new(taxonomies_def[name].clone(), config, taxo)); } - taxonomies + Ok(taxonomies) } @@ -184,7 +185,7 @@ mod tests { page3.meta.taxonomies = taxo_page3; let pages = vec![page1, page2, page3]; - let taxonomies = find_taxonomies(&config, &pages); + let taxonomies = find_taxonomies(&config, &pages).unwrap(); let (tags, categories, authors) = { let mut t = None; let mut c = None; @@ -228,4 +229,22 @@ mod tests { assert_eq!(categories.items[1].permalink, "http://a-website.com/categories/programming-tutorials/"); assert_eq!(categories.items[1].pages.len(), 1); } + + #[test] + fn errors_on_unknown_taxonomy() { + let mut config = Config::default(); + config.taxonomies = vec![ + Taxonomy {name: "authors".to_string(), ..Taxonomy::default()}, + ]; + let mut page1 = Page::default(); + let mut taxo_page1 = HashMap::new(); + taxo_page1.insert("tags".to_string(), vec!["rust".to_string(), "db".to_string()]); + page1.meta.taxonomies = taxo_page1; + + let taxonomies = find_taxonomies(&config, &vec![page1]); + assert!(taxonomies.is_err()); + let err = taxonomies.unwrap_err(); + // no path as this is created by Default + assert_eq!(err.description(), "Page `` has taxonomy `tags` which is not defined in config.toml"); + } } diff --git a/docs/content/documentation/content/taxonomies.md b/docs/content/documentation/content/taxonomies.md index 77ab8026..d70878c5 100644 --- a/docs/content/documentation/content/taxonomies.md +++ b/docs/content/documentation/content/taxonomies.md @@ -10,7 +10,7 @@ The first step is to define the taxonomies in your [config.toml](./documentation A taxonomy has 4 variables: - `name`: a required string that will be used in the URLs, usually the plural version (i.e. tags, categories etc) -- `paginate`: if this is set to a number, each term page will be paginated by this much. +- `paginate_by`: if this is set to a number, each term page will be paginated by this much. - `paginate_path`: if set, will be the path used by paginated page and the page number will be appended after it. For example the default would be page/1 - `rss`: if set to `true`, a RSS feed will be generated for each individual term. diff --git a/docs/content/documentation/getting-started/configuration.md b/docs/content/documentation/getting-started/configuration.md index 87cae5e2..d552d876 100644 --- a/docs/content/documentation/getting-started/configuration.md +++ b/docs/content/documentation/getting-started/configuration.md @@ -44,7 +44,7 @@ rss_limit = 20 # Example: # taxonomies = [ # {name: "tags", rss: true}, # each tag will have its own RSS feed -# {name: "categories", paginate: 5}, # 5 terms per page +# {name: "categories", paginate_by: 5}, # 5 terms per page # ] # taxonomies = []