|
| 1 | +use crate::settings::ConfigSettings; |
| 2 | +use glob::Pattern; |
| 3 | +use log::error; |
| 4 | +use regex::Regex; |
| 5 | +use std::path::Path; |
| 6 | + |
| 7 | +/// Insert a word into the allowlist, returning true when it was newly added. |
| 8 | +pub(crate) fn insert_word(settings: &mut ConfigSettings, word: &str) -> bool { |
| 9 | + let word = word.to_ascii_lowercase(); |
| 10 | + if settings.words.contains(&word) { |
| 11 | + return false; |
| 12 | + } |
| 13 | + settings.words.push(word); |
| 14 | + settings.words.sort(); |
| 15 | + settings.words.dedup(); |
| 16 | + true |
| 17 | +} |
| 18 | + |
| 19 | +/// Insert a path into the ignore list, returning true when it was newly added. |
| 20 | +pub(crate) fn insert_ignore(settings: &mut ConfigSettings, file: &str) -> bool { |
| 21 | + let file = file.to_string(); |
| 22 | + if settings.ignore_paths.contains(&file) { |
| 23 | + return false; |
| 24 | + } |
| 25 | + settings.ignore_paths.push(file); |
| 26 | + settings.ignore_paths.sort(); |
| 27 | + settings.ignore_paths.dedup(); |
| 28 | + true |
| 29 | +} |
| 30 | + |
| 31 | +/// Resolve configured dictionary IDs, providing a default when none are set. |
| 32 | +pub(crate) fn dictionary_ids(settings: &ConfigSettings) -> Vec<String> { |
| 33 | + if settings.dictionaries.is_empty() { |
| 34 | + vec!["en_us".to_string()] |
| 35 | + } else { |
| 36 | + settings.dictionaries.clone() |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +/// Determine whether a path should be ignored based on the configured glob patterns. |
| 41 | +pub(crate) fn should_ignore_path(settings: &ConfigSettings, path: &Path) -> bool { |
| 42 | + let path_str = path.to_string_lossy(); |
| 43 | + settings.ignore_paths.iter().any(|pattern| { |
| 44 | + Pattern::new(pattern) |
| 45 | + .map(|p| p.matches(&path_str)) |
| 46 | + .unwrap_or(false) |
| 47 | + }) |
| 48 | +} |
| 49 | + |
| 50 | +/// Check if a word is explicitly allowed. |
| 51 | +pub(crate) fn is_allowed_word(settings: &ConfigSettings, word: &str) -> bool { |
| 52 | + let word = word.to_ascii_lowercase(); |
| 53 | + settings.words.iter().any(|w| w == &word) |
| 54 | +} |
| 55 | + |
| 56 | +/// Check if a word should be flagged. |
| 57 | +pub(crate) fn should_flag_word(settings: &ConfigSettings, word: &str) -> bool { |
| 58 | + let word = word.to_ascii_lowercase(); |
| 59 | + settings.flag_words.iter().any(|w| w == &word) |
| 60 | +} |
| 61 | + |
| 62 | +/// Compile user-provided ignore regex patterns, dropping invalid entries. |
| 63 | +pub(crate) fn build_ignore_regexes(patterns: &[String]) -> Vec<Regex> { |
| 64 | + patterns |
| 65 | + .iter() |
| 66 | + .filter_map(|pattern| match Regex::new(pattern) { |
| 67 | + Ok(regex) => Some(regex), |
| 68 | + Err(e) => { |
| 69 | + error!("Ignoring invalid regex pattern '{pattern}': {e}"); |
| 70 | + None |
| 71 | + } |
| 72 | + }) |
| 73 | + .collect() |
| 74 | +} |
| 75 | + |
| 76 | +/// Retrieve the configured minimum word length. |
| 77 | +pub(crate) fn min_word_length(settings: &ConfigSettings) -> usize { |
| 78 | + settings.min_word_length |
| 79 | +} |
0 commit comments