use std::path::{Path, PathBuf};
#[derive(Debug, Clone)]
pub struct VolumeConfig {
pub volume_size: u64,
base_path: PathBuf,
}
impl VolumeConfig {
pub fn new(base_path: impl AsRef<Path>, volume_size: u64) -> Self {
Self {
volume_size,
base_path: base_path.as_ref().to_path_buf(),
}
}
pub fn base_path(&self) -> &Path {
&self.base_path
}
pub fn volume_path(&self, volume_number: u32) -> PathBuf {
let base_str = self.base_path.to_string_lossy();
PathBuf::from(format!("{}.{:03}", base_str, volume_number))
}
pub fn volume_size(&self) -> u64 {
self.volume_size
}
pub fn with_default_size(base_path: impl AsRef<Path>) -> Self {
Self::new(base_path, 100 * 1024 * 1024)
}
pub fn dvd(base_path: impl AsRef<Path>) -> Self {
Self::new(base_path, 4700 * 1024 * 1024) }
pub fn cd(base_path: impl AsRef<Path>) -> Self {
Self::new(base_path, 700 * 1024 * 1024)
}
pub fn fat32(base_path: impl AsRef<Path>) -> Self {
Self::new(base_path, 4 * 1024 * 1024 * 1024 - 1)
}
}
impl Default for VolumeConfig {
fn default() -> Self {
Self {
volume_size: 100 * 1024 * 1024, base_path: PathBuf::from("archive.7z"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_volume_path_generation() {
let config = VolumeConfig::new("test.7z", 1024);
assert_eq!(config.volume_path(1), PathBuf::from("test.7z.001"));
assert_eq!(config.volume_path(2), PathBuf::from("test.7z.002"));
assert_eq!(config.volume_path(10), PathBuf::from("test.7z.010"));
assert_eq!(config.volume_path(100), PathBuf::from("test.7z.100"));
assert_eq!(config.volume_path(999), PathBuf::from("test.7z.999"));
}
#[test]
fn test_volume_path_with_directory() {
let config = VolumeConfig::new("/path/to/archive.7z", 1024);
assert_eq!(
config.volume_path(1),
PathBuf::from("/path/to/archive.7z.001")
);
}
#[test]
fn test_preset_sizes() {
let dvd = VolumeConfig::dvd("archive.7z");
assert_eq!(dvd.volume_size(), 4700 * 1024 * 1024);
let cd = VolumeConfig::cd("archive.7z");
assert_eq!(cd.volume_size(), 700 * 1024 * 1024);
let fat32 = VolumeConfig::fat32("archive.7z");
assert_eq!(fat32.volume_size(), 4 * 1024 * 1024 * 1024 - 1);
}
#[test]
fn test_default() {
let config = VolumeConfig::default();
assert_eq!(config.volume_size(), 100 * 1024 * 1024);
assert_eq!(config.base_path(), Path::new("archive.7z"));
}
}