#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StopReason {
FilesystemRoot,
Home,
GitRoot,
MaxDepth,
WalkUpDisabled,
}
impl StopReason {
pub fn describe(self, max_depth: usize) -> String {
match self {
StopReason::FilesystemRoot => "reached the filesystem root".into(),
StopReason::Home => "reached $HOME".into(),
StopReason::GitRoot => "reached .git".into(),
StopReason::MaxDepth => format!("reached the max_depth limit ({})", dirs(max_depth)),
StopReason::WalkUpDisabled => "--no-walk-up / [discovery] walk_up = false".into(),
}
}
}
pub(crate) fn dirs(n: usize) -> String {
format!("{n} director{}", if n == 1 { "y" } else { "ies" })
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_single_directory_is_not_plural() {
assert_eq!(dirs(1), "1 directory");
assert_eq!(dirs(0), "0 directories");
assert_eq!(dirs(6), "6 directories");
assert_eq!(
StopReason::MaxDepth.describe(1),
"reached the max_depth limit (1 directory)"
);
}
#[test]
fn every_stop_reason_has_a_description() {
for (reason, max) in [
(StopReason::FilesystemRoot, 8),
(StopReason::Home, 8),
(StopReason::GitRoot, 8),
(StopReason::MaxDepth, 8),
(StopReason::WalkUpDisabled, 8),
] {
assert!(!reason.describe(max).is_empty());
}
}
}