use std::path::{Path, PathBuf};
mod manager;
mod metadata;
pub use manager::BackupManager;
pub use metadata::{BackupMetadata, BackupRecord};
pub type BackupId = String;
#[doc(hidden)]
pub fn create_backup_id() -> BackupId {
chrono::Local::now().format("%Y-%m-%d-%H%M%S-%3f").to_string()
}
#[doc(hidden)]
pub fn get_backup_root(workspace_root: &Path) -> PathBuf {
workspace_root.join("target").join("cargo-rail").join("backups")
}
#[doc(hidden)]
pub fn get_backup_dir(workspace_root: &Path, backup_id: &str) -> PathBuf {
get_backup_root(workspace_root).join(backup_id)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_backup_id_format() {
let id = create_backup_id();
assert_eq!(id.len(), 21); assert_eq!(id.chars().nth(4), Some('-'));
assert_eq!(id.chars().nth(7), Some('-'));
assert_eq!(id.chars().nth(10), Some('-'));
assert_eq!(id.chars().nth(17), Some('-'));
}
#[test]
fn test_backup_root_path() {
let workspace = PathBuf::from("/workspace");
let backup_root = get_backup_root(&workspace);
assert_eq!(backup_root, PathBuf::from("/workspace/target/cargo-rail/backups"));
}
#[test]
fn test_backup_dir_path() {
let workspace = PathBuf::from("/workspace");
let backup_dir = get_backup_dir(&workspace, "2024-01-15-143022");
assert_eq!(
backup_dir,
PathBuf::from("/workspace/target/cargo-rail/backups/2024-01-15-143022")
);
}
}