use std::path::Path;
use fallow_output::BaselineMode;
use super::{BaselineKind, BaselineStaleness, BaselineStalenessWarning};
use crate::shell_quote::shell_quote;
impl BaselineKind {
#[must_use]
pub fn prune_command(self, path: &Path, mode: BaselineMode) -> String {
let absolute = std::path::absolute(path).unwrap_or_else(|_| path.to_path_buf());
let quoted = shell_quote(&absolute.display().to_string());
format!(
"fallow baselines prune{} --{}-baseline {quoted}",
mode.flags(),
self.as_str()
)
}
#[must_use]
pub fn prune_command_for_root(self, path: &Path, mode: BaselineMode, root: &Path) -> String {
let command = self.prune_command(path, mode);
if std::env::current_dir().is_ok_and(|cwd| cwd == root) {
return command;
}
format!("{command} --root {}", shell_quote(&root.to_string_lossy()))
}
#[must_use]
pub const fn entry_noun(self) -> &'static str {
match self {
Self::DeadCode => "issue",
Self::Dupes => "clone group",
Self::Health => "finding",
}
}
#[expect(
clippy::print_stderr,
reason = "baseline warnings accompany the report on stderr"
)]
pub fn warn_on_staleness(
self,
staleness: BaselineStaleness,
path: &Path,
saved_mode: BaselineMode,
root: &Path,
) {
let label = match self {
Self::DeadCode => "baseline",
Self::Dupes => "duplication baseline",
Self::Health => "health baseline",
};
let entries = staleness.entries;
let noun = self.entry_noun();
match staleness.warning() {
BaselineStalenessWarning::None => {}
BaselineStalenessWarning::ZeroOverlap => eprintln!(
"Warning: {label} has {entries} entries but matched 0 current {noun}s. \
Your paths may have changed, or the baseline was saved on a different machine. \
Re-save with: --save-baseline {}",
path.display(),
),
BaselineStalenessWarning::Partial => eprintln!(
"Warning: {label} is partially stale: {} of {entries} entries matched no current {noun}, \
so the gate protects less than what was saved. Remove only the stale entries with: {}, \
or re-save with: --save-baseline {}",
staleness.stale_entries(),
self.prune_command_for_root(path, saved_mode, root),
path.display(),
),
}
}
}