use std::io;
use std::path::Path;
pub const FORMAT_VERSION_V5: u32 = 5;
pub const FORMAT_VERSION_V4: u32 = 4;
pub const FORMAT_VERSION_V3: u32 = 3;
pub const FORMAT_VERSION_V2: u32 = 2;
pub const FORMAT_VERSION_V1: u32 = 1;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FormatVersion(pub u32);
impl FormatVersion {
pub fn v4() -> Self {
Self(FORMAT_VERSION_V4)
}
pub fn v3() -> Self {
Self(FORMAT_VERSION_V3)
}
pub fn v2() -> Self {
Self(FORMAT_VERSION_V2)
}
pub fn v1() -> Self {
Self(FORMAT_VERSION_V1)
}
pub fn read_from_meta(meta_dir: &Path) -> io::Result<Option<Self>> {
let path = meta_dir.join("format_version.bin");
if !path.exists() {
return Ok(None);
}
let bytes = std::fs::read(path)?;
if bytes.len() != 4 {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"format_version.bin must be 4 bytes",
));
}
Ok(Some(Self(u32::from_le_bytes(bytes.try_into().unwrap()))))
}
pub fn write_to_meta(&self, meta_dir: &Path) -> io::Result<()> {
std::fs::create_dir_all(meta_dir)?;
let path = meta_dir.join("format_version.bin");
let tmp = path.with_extension("tmp");
std::fs::write(&tmp, self.0.to_le_bytes())?;
std::fs::rename(&tmp, path)
}
}