use std::path::{Path, PathBuf};
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum XsdRetryInputSource {
Primary { path: PathBuf },
ArchivedFallback { path: PathBuf },
EmptyFallback,
}
pub(crate) fn decide_xsd_retry_input_source(
primary_exists: bool,
archived_exists: bool,
primary_path: &Path,
archived_path: &Path,
) -> XsdRetryInputSource {
if primary_exists {
XsdRetryInputSource::Primary {
path: primary_path.to_path_buf(),
}
} else if archived_exists {
XsdRetryInputSource::ArchivedFallback {
path: archived_path.to_path_buf(),
}
} else {
XsdRetryInputSource::EmptyFallback
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn prefers_primary_when_both_exist() {
let source = decide_xsd_retry_input_source(
true,
true,
Path::new(".agent/tmp/issues.xml"),
Path::new(".agent/tmp/issues.xml.processed"),
);
assert_eq!(
source,
XsdRetryInputSource::Primary {
path: PathBuf::from(".agent/tmp/issues.xml")
}
);
}
#[test]
fn uses_archived_when_primary_missing() {
let source = decide_xsd_retry_input_source(
false,
true,
Path::new(".agent/tmp/issues.xml"),
Path::new(".agent/tmp/issues.xml.processed"),
);
assert_eq!(
source,
XsdRetryInputSource::ArchivedFallback {
path: PathBuf::from(".agent/tmp/issues.xml.processed")
}
);
}
#[test]
fn uses_empty_fallback_when_both_missing() {
let source = decide_xsd_retry_input_source(
false,
false,
Path::new(".agent/tmp/issues.xml"),
Path::new(".agent/tmp/issues.xml.processed"),
);
assert_eq!(source, XsdRetryInputSource::EmptyFallback);
}
#[test]
fn ignores_archived_when_primary_exists() {
let source = decide_xsd_retry_input_source(
true,
false, Path::new(".agent/tmp/issues.xml"),
Path::new(".agent/tmp/issues.xml.processed"),
);
assert_eq!(
source,
XsdRetryInputSource::Primary {
path: PathBuf::from(".agent/tmp/issues.xml")
}
);
}
}