From 024144ba786d94ffe8635a197a3be0fa33ec765f Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Tue, 2 Feb 2021 20:49:57 +0100 Subject: [PATCH] cargo fmt --- components/config/src/highlighting.rs | 10 ++- components/library/src/library.rs | 4 +- components/library/src/sorting.rs | 50 +++++++-------- components/rendering/src/markdown.rs | 13 +++- .../rendering/src/markdown/codeblock.rs | 9 ++- components/site/src/tpls.rs | 5 +- components/templates/src/filters.rs | 17 ++++-- .../templates/src/global_fns/load_data.rs | 61 +++++++++++-------- 8 files changed, 108 insertions(+), 61 deletions(-) diff --git a/components/config/src/highlighting.rs b/components/config/src/highlighting.rs index bfc0245b..54f6b8fa 100644 --- a/components/config/src/highlighting.rs +++ b/components/config/src/highlighting.rs @@ -24,7 +24,10 @@ pub enum HighlightSource { } /// Returns the highlighter and whether it was found in the extra or not -pub fn get_highlighter(language: Option<&str>, config: &Config) -> (HighlightLines<'static>, HighlightSource) { +pub fn get_highlighter( + language: Option<&str>, + config: &Config, +) -> (HighlightLines<'static>, HighlightSource) { let theme = &THEME_SET.themes[config.highlight_theme()]; if let Some(ref lang) = language { @@ -41,7 +44,10 @@ pub fn get_highlighter(language: Option<&str>, config: &Config) -> (HighlightLin if let Some(syntax) = SYNTAX_SET.find_syntax_by_token(hacked_lang) { (HighlightLines::new(syntax, theme), HighlightSource::Theme) } else { - (HighlightLines::new(SYNTAX_SET.find_syntax_plain_text(), theme), HighlightSource::NotFound) + ( + HighlightLines::new(SYNTAX_SET.find_syntax_plain_text(), theme), + HighlightSource::NotFound, + ) } } else { (HighlightLines::new(SYNTAX_SET.find_syntax_plain_text(), theme), HighlightSource::Plain) diff --git a/components/library/src/library.rs b/components/library/src/library.rs index 553df543..b933f8b6 100644 --- a/components/library/src/library.rs +++ b/components/library/src/library.rs @@ -6,7 +6,9 @@ use slotmap::{DefaultKey, DenseSlotMap}; use front_matter::SortBy; use crate::content::{Page, Section}; -use crate::sorting::{find_siblings, sort_pages_by_date, sort_pages_by_title, sort_pages_by_weight}; +use crate::sorting::{ + find_siblings, sort_pages_by_date, sort_pages_by_title, sort_pages_by_weight, +}; use config::Config; // Like vec! but for HashSet diff --git a/components/library/src/sorting.rs b/components/library/src/sorting.rs index 01571e1f..07dc4a61 100644 --- a/components/library/src/sorting.rs +++ b/components/library/src/sorting.rs @@ -173,33 +173,35 @@ mod tests { "meter", "track_1", ]; - let pages: Vec = titles.iter().map( - |title| create_page_with_title(title) - ).collect(); + let pages: Vec = titles.iter().map(|title| create_page_with_title(title)).collect(); let mut dense = DenseSlotMap::new(); - let keys: Vec<_> = pages.iter().map( - |p| dense.insert(p) - ).collect(); - let input: Vec<_> = pages.iter().enumerate().map( - |(i, page)| (&keys[i], page.meta.title.as_deref(), page.permalink.as_ref()) - ).collect(); + let keys: Vec<_> = pages.iter().map(|p| dense.insert(p)).collect(); + let input: Vec<_> = pages + .iter() + .enumerate() + .map(|(i, page)| (&keys[i], page.meta.title.as_deref(), page.permalink.as_ref())) + .collect(); let (sorted, _) = sort_pages_by_title(input); // Should be sorted by title - let sorted_titles: Vec<_> = sorted.iter().map( - |key| dense.get(*key).unwrap().meta.title.as_ref().unwrap() - ).collect(); - assert_eq!(sorted_titles, vec![ - "bagel", - "BART", - "μ-kernel", - "meter", - "métro", - "microkernel", - "track_1", - "track_3", - "track_13", - "Underground", - ]); + let sorted_titles: Vec<_> = sorted + .iter() + .map(|key| dense.get(*key).unwrap().meta.title.as_ref().unwrap()) + .collect(); + assert_eq!( + sorted_titles, + vec![ + "bagel", + "BART", + "μ-kernel", + "meter", + "métro", + "microkernel", + "track_1", + "track_3", + "track_13", + "Underground", + ] + ); } #[test] diff --git a/components/rendering/src/markdown.rs b/components/rendering/src/markdown.rs index 96bcba4a..1b877beb 100644 --- a/components/rendering/src/markdown.rs +++ b/components/rendering/src/markdown.rs @@ -242,7 +242,18 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result { } impl<'config> CodeBlock<'config> { - pub fn new(fence_info: &str, config: &'config Config, background: IncludeBackground, path: Option<&'config str>) -> Self { + pub fn new( + fence_info: &str, + config: &'config Config, + background: IncludeBackground, + path: Option<&'config str>, + ) -> Self { let fence_info = FenceSettings::new(fence_info); let theme = &THEME_SET.themes[config.highlight_theme()]; let (highlighter, highlight_source) = get_highlighter(fence_info.language, config); @@ -37,7 +42,7 @@ impl<'config> CodeBlock<'config> { eprintln!("Warning: Highlight language {} not found", lang); } None - }, + } _ => None, }; Self { diff --git a/components/site/src/tpls.rs b/components/site/src/tpls.rs index 2e5295ef..0377695e 100644 --- a/components/site/src/tpls.rs +++ b/components/site/src/tpls.rs @@ -50,7 +50,10 @@ pub fn load_tera(path: &Path, config: &Config) -> Result { /// Adds global fns that are to be available to shortcodes while rendering markdown pub fn register_early_global_fns(site: &mut Site) { - site.tera.register_filter("markdown", filters::MarkdownFilter::new(site.config.clone(), site.permalinks.clone())); + site.tera.register_filter( + "markdown", + filters::MarkdownFilter::new(site.config.clone(), site.permalinks.clone()), + ); site.tera.register_function( "get_url", diff --git a/components/templates/src/filters.rs b/components/templates/src/filters.rs index 731ecfd2..51ecca47 100644 --- a/components/templates/src/filters.rs +++ b/components/templates/src/filters.rs @@ -1,6 +1,6 @@ +use std::borrow::Cow; use std::collections::HashMap; use std::hash::BuildHasher; -use std::borrow::Cow; use base64::{decode, encode}; use config::Config; @@ -120,13 +120,14 @@ mod tests { config.markdown.external_links_target_blank = true; let md = "Hello :smile: ..."; - let result = - MarkdownFilter::new(config.clone(), HashMap::new()).filter(&to_value(&md).unwrap(), &HashMap::new()); + let result = MarkdownFilter::new(config.clone(), HashMap::new()) + .filter(&to_value(&md).unwrap(), &HashMap::new()); assert!(result.is_ok()); assert_eq!(result.unwrap(), to_value(&"

Hello https://google.com 😄 …

\n").unwrap()); let md = "```py\ni=0\n```"; - let result = MarkdownFilter::new(config, HashMap::new()).filter(&to_value(&md).unwrap(), &HashMap::new()); + let result = MarkdownFilter::new(config, HashMap::new()) + .filter(&to_value(&md).unwrap(), &HashMap::new()); assert!(result.is_ok()); assert!(result.unwrap().as_str().unwrap().contains("
Hello. Check out my blog!

\n").unwrap()); + assert_eq!( + result.unwrap(), + to_value(&"

Hello. Check out my blog!

\n").unwrap() + ); } #[test] diff --git a/components/templates/src/global_fns/load_data.rs b/components/templates/src/global_fns/load_data.rs index 0f74c648..a5b929fa 100644 --- a/components/templates/src/global_fns/load_data.rs +++ b/components/templates/src/global_fns/load_data.rs @@ -190,7 +190,15 @@ impl LoadData { impl TeraFn for LoadData { fn call(&self, args: &HashMap) -> Result { - let required = if let Some(req) = optional_arg!(bool, args.get("required"), "`load_data`: `required` must be a boolean (true or false)") { req } else { true }; + let required = if let Some(req) = optional_arg!( + bool, + args.get("required"), + "`load_data`: `required` must be a boolean (true or false)" + ) { + req + } else { + true + }; let path_arg = optional_arg!(String, args.get("path"), GET_DATA_ARGUMENT_ERROR_MESSAGE); let url_arg = optional_arg!(String, args.get("url"), GET_DATA_ARGUMENT_ERROR_MESSAGE); let data_source = DataSource::from_args(path_arg.clone(), url_arg, &self.base_path)?; @@ -198,13 +206,19 @@ impl TeraFn for LoadData { // If the file doesn't exist, source is None match (&data_source, required) { // If the file was not required, return a Null value to the template - (None, false) => { return Ok(Value::Null); }, + (None, false) => { + return Ok(Value::Null); + } // If the file was required, error (None, true) => { // source is None only with path_arg (not URL), so path_arg is safely unwrap - return Err(format!("{} doesn't exist", &self.base_path.join(path_arg.unwrap()).display()).into()); - }, - _ => {}, + return Err(format!( + "{} doesn't exist", + &self.base_path.join(path_arg.unwrap()).display() + ) + .into()); + } + _ => {} } let data_source = data_source.unwrap(); let file_format = get_output_format_from_args(&args, &data_source)?; @@ -223,25 +237,26 @@ impl TeraFn for LoadData { .get(url.as_str()) .header(header::ACCEPT, file_format.as_accept_header()) .send() - .and_then(|res| res.error_for_status()) { - Ok(r) => { - r.text() - .map_err(|e| format!("Failed to parse response from {}: {:?}", url, e).into()) - }, - Err(e) => { - if !required { - // HTTP error is discarded (because required=false) and - // Null value is returned to the template - return Ok(Value::Null); - } - Err(match e.status() { - Some(status) => format!("Failed to request {}: {}", url, status), - None => format!("Could not get response status for url: {}", url), - }.into()) + .and_then(|res| res.error_for_status()) + { + Ok(r) => r.text().map_err(|e| { + format!("Failed to parse response from {}: {:?}", url, e).into() + }), + Err(e) => { + if !required { + // HTTP error is discarded (because required=false) and + // Null value is returned to the template + return Ok(Value::Null); } + Err(match e.status() { + Some(status) => format!("Failed to request {}: {}", url, status), + None => format!("Could not get response status for url: {}", url), + } + .into()) + } } - }, - // Now that we have discarded recoverable errors, we can unwrap the result + } + // Now that we have discarded recoverable errors, we can unwrap the result }?; let result_value: Result = match file_format { @@ -542,7 +557,6 @@ mod tests { assert_eq!(result.unwrap(), tera::Value::Null); } - #[test] fn set_default_user_agent() { let user_agent = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION")); @@ -693,7 +707,6 @@ mod tests { } } - #[test] fn can_load_json() { let static_fn = LoadData::new(PathBuf::from("../utils/test-files"));