#[derive(Debug, Clone, PartialEq, Eq)]
pub enum IndexDumpPlan {
Absent { path_display: String },
Proceed,
NotRequested,
}
impl IndexDumpPlan {
pub fn plan(dump: bool, present: bool, path_display: &str) -> Self {
if !dump {
Self::NotRequested
} else if present {
Self::Proceed
} else {
Self::Absent {
path_display: path_display.to_string(),
}
}
}
pub fn absent_message(&self) -> Option<String> {
match self {
Self::Absent { path_display } => Some(format!(
"No index found at {path_display}. Run a snapshot or status command first."
)),
_ => None,
}
}
}
pub fn index_missing_message(path_display: &str) -> String {
IndexDumpPlan::plan(true, false, path_display)
.absent_message()
.expect("Absent plan always has a message")
}
pub fn plan_index_absent_dump(dump: bool, present: bool) -> bool {
matches!(
IndexDumpPlan::plan(dump, present, ""),
IndexDumpPlan::Absent { .. }
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn missing_message() {
let msg = index_missing_message("/repo/.heddle/state/index.bin");
assert!(msg.contains("/repo/.heddle/state/index.bin"));
assert!(msg.contains("No index found"));
assert!(msg.contains("snapshot or status"));
}
#[test]
fn absent_dump_gate() {
assert!(plan_index_absent_dump(true, false));
assert!(!plan_index_absent_dump(true, true));
assert!(!plan_index_absent_dump(false, false));
assert!(!plan_index_absent_dump(false, true));
assert!(matches!(
IndexDumpPlan::plan(true, false, "p"),
IndexDumpPlan::Absent { .. }
));
assert_eq!(IndexDumpPlan::plan(true, true, "p"), IndexDumpPlan::Proceed);
}
}