use std::path::{Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH};
use ignore::WalkBuilder;
use crate::observer::lang::Language;
#[must_use]
pub(crate) fn walk_supported_files(root: &Path, excluded: &[String]) -> Vec<PathBuf> {
WalkBuilder::new(root)
.require_git(false)
.build()
.filter_map(Result::ok)
.filter(|entry| entry.file_type().is_some_and(|ft| ft.is_file()))
.filter_map(|entry| {
let path = entry.into_path();
Language::from_path(&path)?;
if is_path_excluded(&path, excluded) {
return None;
}
Some(path)
})
.collect()
}
#[must_use]
pub(crate) fn is_path_excluded(path: &Path, patterns: &[String]) -> bool {
if patterns.is_empty() {
return false;
}
let s = path.to_string_lossy();
patterns.iter().any(|pat| s.contains(pat.as_str()))
}
#[must_use]
pub(crate) fn since_cutoff(since_days: u32) -> i64 {
let Ok(now) = SystemTime::now().duration_since(UNIX_EPOCH) else {
return i64::MIN;
};
let secs = i64::try_from(now.as_secs()).unwrap_or(i64::MAX);
secs.saturating_sub(i64::from(since_days).saturating_mul(86_400))
}