diff --git a/Cargo.toml b/Cargo.toml index 3f9891d..c8f806f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mdlinker" -version = "1.6.1" +version = "1.7.2" edition = "2021" [profile.dev] diff --git a/src/config.rs b/src/config.rs index 796077a..3d69bd6 100644 --- a/src/config.rs +++ b/src/config.rs @@ -28,9 +28,8 @@ pub enum NewConfigError { FileDoesNotParseError(#[from] toml::de::Error), #[error("ReplacePair compilation error")] ReplacePairCompilationError(#[from] ReplacePairCompilationError), - #[error("Pages directory missing")] - #[help("Please provide a pages directory argument in either your cli or config file")] - PagesDirectoryMissing, + #[error("No files were passed to the config")] + NoFilesPassedError, } /// Config which contains both the cli and the config file @@ -44,6 +43,7 @@ pub struct Config { /// See [`self::cli::Config::files`] #[builder(default=vec![])] pub files: Vec, + pub original_file_globs: Option>, /// See [`self::cli::Config::root_directory`] #[builder(default=PathBuf::from("."))] pub new_files_directory: PathBuf, @@ -118,6 +118,19 @@ fn combine_partials( Ok(Config::builder() .file_config(file_config.clone()) .cli_config(cli_config.clone()) + .files({ + let files = cli_config.files().or(file_config.files()); + match files { + Some(files) if !files.is_empty() => files, + _ => return Err(NewConfigError::NoFilesPassedError), + } + }) + .maybe_original_file_globs(file_config.original_file_globs()) + .maybe_new_files_directory( + cli_config + .new_files_directory() + .or(file_config.new_files_directory()), + ) .maybe_ngram_size(cli_config.ngram_size().or(file_config.ngram_size())) .maybe_boundary_pattern( cli_config @@ -165,12 +178,6 @@ fn combine_partials( }) .maybe_fix(cli_config.fix().or(file_config.fix())) .maybe_allow_dirty(cli_config.allow_dirty().or(file_config.allow_dirty())) - .files( - cli_config - .files() - .or(file_config.files()) - .expect("A default is set"), - ) .maybe_ignore_word_pairs( cli_config .ignore_word_pairs() diff --git a/src/config/file.rs b/src/config/file.rs index 3e47c0c..4f57176 100644 --- a/src/config/file.rs +++ b/src/config/file.rs @@ -65,18 +65,17 @@ impl Config { std::fs::read_to_string(path).map_err(NewConfigError::FileDoesNotReadError)?; toml::from_str(&contents).map_err(NewConfigError::FileDoesNotParseError) } + + #[must_use] + pub fn original_file_globs(&self) -> Option> { + self.files.clone() + } } impl From for Config { fn from(value: MasterConfig) -> Self { Self { - files: Some( - value - .files - .into_iter() - .map(|file| file.to_string_lossy().to_string()) - .collect(), - ), + files: value.original_file_globs, new_files_directory: Some(value.new_files_directory), ngram_size: Some(value.ngram_size), boundary_pattern: Some(value.boundary_pattern), @@ -99,25 +98,17 @@ impl Partial for Config { Some(files) => { for file in files { if file.contains('*') { - let pattern = glob::Pattern::new(file); - match pattern { - Ok(_) => { - let globs = glob::glob(file); - match globs { - Ok(globs) => { - for glob in globs { - match glob { - Ok(path) => out.push(path), - Err(e) => { - eprintln!( - "Error processing glob '{file}': {e}", - ); - } - } + let globs = glob::glob(file); + match globs { + Ok(globs) => { + for glob in globs { + match glob { + Ok(path) => { + out.push(path); + } + Err(e) => { + eprintln!("Error processing glob '{file}': {e}",); } - } - Err(e) => { - eprintln!("Error parsing glob pattern '{file}': {e}"); } } } @@ -126,11 +117,19 @@ impl Partial for Config { return None; } } + } else { + let path = PathBuf::from(file); + if path.exists() { + out.push(path); + } else { + eprintln!("File does not exist: {file}"); + } } } } } if out.is_empty() { + eprintln!("No valid files found in the configuration."); None } else { Some(out)