From 35fb6245684751ff448e75224d51f4c64bf4a838 Mon Sep 17 00:00:00 2001 From: Huck Boles Date: Sat, 20 May 2023 13:31:23 -0500 Subject: [PATCH] refactor: clippy suggestions --- src/builder.rs | 12 ++++----- src/builder/array.rs | 44 +++++++++++++++++++++++++----- src/builder/source.rs | 60 ++++++++++++++++++++--------------------- src/builder/tests.rs | 49 ++++++++++++++++----------------- src/builder/variable.rs | 4 +-- src/error.rs | 10 ++++--- src/lib.rs | 2 +- src/main.rs | 4 +-- src/metafile.rs | 4 ++- src/metafile/dir.rs | 2 +- src/metafile/file.rs | 21 +++------------ src/metafile/scope.rs | 14 +++++----- 12 files changed, 124 insertions(+), 102 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index 0ead5ea..c225e49 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -13,22 +13,22 @@ mod tests; use crate::{MetaError, MetaFile, Scope}; use eyre::Result; -pub fn build_metafile(file: &MetaFile) -> Result { +pub fn build_metafile(file: &MetaFile) -> Result> { if file.header.blank { return Ok(String::new()); } else if file.header.ignore { - return Err(MetaError::Ignored); + return Err(Box::new(MetaError::Ignored)); } - let html = get_source_html(file)?; + let html = get_source_html(file).map_err(MetaError::from)?; - let pattern = get_pattern("base", file)?; - let mut base = crate::parse_string(pattern, file.opts)?; + let pattern = get_pattern("base", file).map_err(MetaError::from)?; + let mut base = crate::parse_string(pattern, file.opts).map_err(MetaError::from)?; base.merge(file); base.patterns.insert(Scope::into_global("SOURCE"), html); - let output = metafile_to_string(&base)?; + let output = metafile_to_string(&base).map_err(MetaError::from)?; Ok(output) } diff --git a/src/builder/array.rs b/src/builder/array.rs index 52edfa3..763dbd4 100644 --- a/src/builder/array.rs +++ b/src/builder/array.rs @@ -1,4 +1,4 @@ -use crate::{MetaFile, Scope, Src}; +use crate::{MetaError, MetaFile, Scope, Src}; use eyre::Result; use std::collections::HashMap; @@ -20,16 +20,22 @@ pub fn expand_arrays(input: String, file: &MetaFile) -> Result { let name = file.name().unwrap_or_default(); let long_key = name + "." + key; - let value = if let Some(val) = file.get_arr(&Scope::into_global(long_key.to_string())) { + let value = if let Some(val) = file.get_arr(&Scope::into_global(&long_key)) { val - } else if let Some(val) = file.get_arr(&Scope::into_local(long_key.to_string())) { + } else if let Some(val) = file.get_arr(&Scope::into_local(&long_key)) { val } else if let Some(val) = file.get_arr(&Scope::into_global(key)) { val } else if let Some(val) = file.get_arr(&Scope::into_local(key)) { val } else if file.opts.undefined { - panic!("undefined array called: {}, {}", key, long_key); + panic!( + "{}", + MetaError::UndefinedExpand { + val: key.to_string(), + path: file.path.to_string_lossy().to_string(), + } + ) } else { &[] }; @@ -39,7 +45,16 @@ pub fn expand_arrays(input: String, file: &MetaFile) -> Result { // loop to duplicate the output template for each array member let mut expanded = String::new(); - for i in 0..get_max_size(&map) { + let size = match get_array_size(&map, file.header.equal_arrays) { + Ok(num) => Ok(num), + Err(e) => match e.as_ref() { + &MetaError::Array => Err(MetaError::UnequalArrays { + path: file.path.to_string_lossy().to_string(), + }), + _ => Err(MetaError::Unknown), + }, + }?; + for i in 0..size { // get a fresh copy of the file let mut str = input.clone(); // replace each key in the file @@ -55,12 +70,27 @@ pub fn expand_arrays(input: String, file: &MetaFile) -> Result { Ok(expanded) } -fn get_max_size(map: &HashMap) -> usize { +fn get_array_size( + map: &HashMap, + same_size: bool, +) -> Result> { + if same_size { + let mut size = (0, false); + for val in map.values() { + if !size.1 { + size = (val.len(), true); + } else if size.0 != val.len() { + return Err(Box::new(MetaError::Array)); + } + } + return Ok(size.0); + } + let mut max = 0; for val in map.values() { if max < val.len() { max = val.len(); } } - max + Ok(max) } diff --git a/src/builder/source.rs b/src/builder/source.rs index f16b500..0ae6544 100644 --- a/src/builder/source.rs +++ b/src/builder/source.rs @@ -1,5 +1,6 @@ use crate::{MetaError, MetaFile, Src}; use eyre::Result; +use pandoc::{InputFormat, InputKind, OutputFormat, OutputKind, Pandoc}; use super::array::*; use super::*; @@ -7,13 +8,13 @@ use super::*; pub fn get_source_html(file: &MetaFile) -> Result { let string = metafile_to_string(file)?; - if file.opts.no_pandoc || !file.header.pandoc || string == "" { + if file.opts.no_pandoc || !file.header.pandoc || string.is_empty() { return Ok(string); } - let input: pandoc::InputFormat; - let output: pandoc::OutputFormat; - if let Ok(io) = get_pandoc_io(&file) { + let input: InputFormat; + let output: OutputFormat; + if let Ok(io) = get_pandoc_io(file) { input = io.0; output = io.1; } else { @@ -21,10 +22,10 @@ pub fn get_source_html(file: &MetaFile) -> Result { return Ok(string); } - let mut pandoc = pandoc::Pandoc::new(); + let mut pandoc = Pandoc::new(); pandoc - .set_input(pandoc::InputKind::Pipe(string)) - .set_output(pandoc::OutputKind::Pipe) + .set_input(InputKind::Pipe(string)) + .set_output(OutputKind::Pipe) .set_input_format(input, vec![]) .set_output_format(output, vec![]); @@ -70,10 +71,7 @@ pub fn metafile_to_string(file: &MetaFile) -> Result { fn get_pandoc_io( file: &MetaFile, -) -> Result<(pandoc::InputFormat, pandoc::OutputFormat), MetaError> { - let input: pandoc::InputFormat; - let output: pandoc::OutputFormat; - +) -> Result<(pandoc::InputFormat, pandoc::OutputFormat), Box> { let mut source_type = ""; if !file.header.source.is_empty() { source_type = &file.header.source; @@ -81,14 +79,14 @@ fn get_pandoc_io( source_type = &file.opts.input; } - match source_type { - "markdown" => input = pandoc::InputFormat::Markdown, - "html" => input = pandoc::InputFormat::Html, - "org" => input = pandoc::InputFormat::Org, - "json" => input = pandoc::InputFormat::Json, - "latex" => input = pandoc::InputFormat::Latex, - _ => return Err(MetaError::Filetype.into()), - } + let input = match source_type { + "markdown" => Ok(InputFormat::Markdown), + "html" => Ok(InputFormat::Html), + "org" => Ok(InputFormat::Org), + "json" => Ok(InputFormat::Json), + "latex" => Ok(InputFormat::Latex), + _ => Err(Box::new(MetaError::Filetype)), + }?; let mut filetype = ""; if !file.header.filetype.is_empty() { @@ -97,18 +95,18 @@ fn get_pandoc_io( filetype = &file.opts.output; } - match filetype { - "html" => output = pandoc::OutputFormat::Html, - "markdown" => output = pandoc::OutputFormat::Markdown, - "man" => output = pandoc::OutputFormat::Man, - "txt" => output = pandoc::OutputFormat::Plain, - "org" => output = pandoc::OutputFormat::Org, - "json" => output = pandoc::OutputFormat::Json, - "latex" => output = pandoc::OutputFormat::Latex, - "asciidoc" => output = pandoc::OutputFormat::Asciidoc, - "pdf" => output = pandoc::OutputFormat::Pdf, - _ => return Err(MetaError::Filetype.into()), - }; + let output = match filetype { + "html" => Ok(OutputFormat::Html), + "markdown" => Ok(OutputFormat::Markdown), + "man" => Ok(OutputFormat::Man), + "txt" => Ok(OutputFormat::Plain), + "org" => Ok(OutputFormat::Org), + "json" => Ok(OutputFormat::Json), + "latex" => Ok(OutputFormat::Latex), + "asciidoc" => Ok(OutputFormat::Asciidoc), + "pdf" => Ok(OutputFormat::Pdf), + _ => Err(Box::new(MetaError::Filetype)), + }?; Ok((input, output)) } diff --git a/src/builder/tests.rs b/src/builder/tests.rs index 6708742..4ac2c09 100644 --- a/src/builder/tests.rs +++ b/src/builder/tests.rs @@ -47,29 +47,30 @@ fn clean_build_dir() -> Result<()> { fn builder_tests() -> Result<()> { clean_build_dir()?; - let mut tests: Vec<(&str, &str)> = Vec::new(); - tests.push(("find_dest", "\n\n")); - tests.push(("blank/blank_pattern", "")); - tests.push(("blank/blank_variable", "\n\n")); - tests.push(("blank/blank_array", "\n\n")); - tests.push(("blank/comment", "\n\n")); - tests.push(( - "blank/inline_comment", - "\n

inline comment

\n\n", - )); - tests.push(( - "expand/variable_in_source", - "\n

GOOD

\n\n", - )); - tests.push(("expand/variable_in_pattern", "\nGOOD\n")); - tests.push(("expand/array_in_source", "\n

12345

\n\n")); - tests.push(("expand/array_in_pattern", "\n12345\n")); - tests.push(("expand/pattern_in_source", "

GOOD

\n")); - tests.push(("expand/pattern_in_pattern", "\nGOOD\nGOOD\n\n")); - tests.push(("override/variable", "\n

GOOD

\n\n")); - tests.push(("override/pattern", "\nGOOD\nGOOD\n\n")); - tests.push(("header/pandoc", "# This should not become html\n")); - tests.push(("header/blank", "")); + let tests: Vec<(&str, &str)> = vec![ + ("find_dest", "\n\n"), + ("blank/blank_pattern", ""), + ("blank/blank_variable", "\n\n"), + ("blank/blank_array", "\n\n"), + ("blank/comment", "\n\n"), + ( + "blank/inline_comment", + "\n

inline comment

\n\n", + ), + ( + "expand/variable_in_source", + "\n

GOOD

\n\n", + ), + ("expand/variable_in_pattern", "\nGOOD\n"), + ("expand/array_in_source", "\n

12345

\n\n"), + ("expand/array_in_pattern", "\n12345\n"), + ("expand/pattern_in_source", "

GOOD

\n"), + ("expand/pattern_in_pattern", "\nGOOD\nGOOD\n\n"), + ("override/variable", "\n

GOOD

\n\n"), + ("override/pattern", "\nGOOD\nGOOD\n\n"), + ("header/pandoc", "# This should not become html\n"), + ("header/blank", ""), + ]; let mut err = false; let mut errs: Vec> = Vec::new(); @@ -85,7 +86,7 @@ fn builder_tests() -> Result<()> { if err { for e in errs.iter() { - eprintln!("{}", e.to_string()); + eprintln!("{}", e); } return Err(eyre::eyre!("failed tests")); } diff --git a/src/builder/variable.rs b/src/builder/variable.rs index 8560836..8753eac 100644 --- a/src/builder/variable.rs +++ b/src/builder/variable.rs @@ -3,9 +3,9 @@ use eyre::Result; pub fn get_variable(key: &str, file: &MetaFile) -> Result { let long_key = file.name()? + "." + key; - if let Some(val) = file.get_var(&Scope::into_local(long_key.to_string())) { + if let Some(val) = file.get_var(&Scope::into_local(&long_key)) { Ok(val.clone()) - } else if let Some(val) = file.get_var(&Scope::into_global(long_key.to_string())) { + } else if let Some(val) = file.get_var(&Scope::into_global(&long_key)) { Ok(val.clone()) } else if let Some(val) = file.get_var(&Scope::into_local(key)) { Ok(val.clone()) diff --git a/src/error.rs b/src/error.rs index 0bbbf1d..8bd0893 100644 --- a/src/error.rs +++ b/src/error.rs @@ -3,12 +3,16 @@ use thiserror::Error; #[derive(Error, Debug)] pub enum MetaError { - #[error("unknown error")] + #[error("unknown internal error")] Unknown, - #[error("ignored")] + #[error("internal break switch")] Ignored, - #[error("filetype")] + #[error("internal filetype error")] Filetype, + #[error("internal array error")] + Array, + #[error("mismatched array sizes in {path}")] + UnequalArrays { path: String }, #[error("could not find {path}")] FileNotFound { path: String }, #[error("could not determine name from {file}")] diff --git a/src/lib.rs b/src/lib.rs index a99ab4c..1296074 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -44,7 +44,7 @@ pub fn build_site(opts: &Options) -> Result<()> { pub fn single_file(opts: &Options) -> Result { let path = opts.file.as_ref().ok_or(MetaError::Unknown)?; - let source = match fs::read_to_string(&path) { + let source = match fs::read_to_string(path) { Ok(str) => Ok(str), Err(_) => Err(eyre::Error::from(MetaError::FileNotFound { path: path.to_string_lossy().to_string(), diff --git a/src/main.rs b/src/main.rs index d51cac3..eb86cc0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,11 +5,11 @@ fn main() -> eyre::Result<()> { return metaforge::new_site(&opts); } - if let Some(_) = &opts.file { + if opts.file.is_some() { let str = metaforge::single_file(&opts)?; println!("{str}"); Ok(()) } else { - return metaforge::build_site(&opts); + metaforge::build_site(&opts) } } diff --git a/src/metafile.rs b/src/metafile.rs index 5efa2cc..c23fa8b 100644 --- a/src/metafile.rs +++ b/src/metafile.rs @@ -35,8 +35,10 @@ impl Src { pub fn to_str(str: impl ToString) -> Self { Src::Str(str.to_string()) } +} - pub fn to_string(&self) -> String { +impl ToString for Src { + fn to_string(&self) -> String { match self { Src::Var(x) | Src::Arr(x) | Src::Pat(x) | Src::Str(x) => x.to_string(), } diff --git a/src/metafile/dir.rs b/src/metafile/dir.rs index 940d3f4..e8ab48f 100644 --- a/src/metafile/dir.rs +++ b/src/metafile/dir.rs @@ -72,7 +72,7 @@ impl<'a> DirNode<'a> { continue; } else { // we raise an ignored error to quickly abort any file parsing - if let MetaError::Ignored = e { + if let MetaError::Ignored = *e { continue; // anything else gets wrapped up and passed up the calling chain } else { diff --git a/src/metafile/file.rs b/src/metafile/file.rs index cf16ed4..d7b7162 100644 --- a/src/metafile/file.rs +++ b/src/metafile/file.rs @@ -102,33 +102,18 @@ impl<'a> MetaFile<'a> { } pub fn var_defined(&self, key: &str) -> bool { - if self.variables.contains_key(&Scope::into_local(key)) + self.variables.contains_key(&Scope::into_local(key)) || self.variables.contains_key(&Scope::into_global(key)) - { - true - } else { - false - } } pub fn arr_defined(&self, key: &str) -> bool { - if self.arrays.contains_key(&Scope::into_local(key)) + self.arrays.contains_key(&Scope::into_local(key)) || self.arrays.contains_key(&Scope::into_global(key)) - { - true - } else { - false - } } pub fn pat_defined(&self, key: &str) -> bool { - if self.patterns.contains_key(&Scope::into_local(key)) + self.patterns.contains_key(&Scope::into_local(key)) || self.patterns.contains_key(&Scope::into_global(key)) - { - true - } else { - false - } } pub fn merge(&mut self, other: &Self) { diff --git a/src/metafile/scope.rs b/src/metafile/scope.rs index 3af714b..8e23b1d 100644 --- a/src/metafile/scope.rs +++ b/src/metafile/scope.rs @@ -13,12 +13,6 @@ impl Scope { Scope::Global(str.to_string()) } - pub fn to_string(&self) -> String { - match self { - Scope::Local(x) | Scope::Global(x) => x.to_string(), - } - } - pub fn is_global(&self) -> bool { match self { Scope::Local(_) => false, @@ -41,3 +35,11 @@ impl Scope { Scope::Global(self.to_string()) } } + +impl ToString for Scope { + fn to_string(&self) -> String { + match self { + Scope::Local(x) | Scope::Global(x) => x.to_string(), + } + } +} -- 2.44.2