use std::path::{Path, PathBuf};
#[derive(Debug, Clone, PartialEq)]
pub enum FileData {
Bytes(Vec<u8>),
Path(PathBuf),
}
impl FileData {
pub fn write_to(&self, dest: &Path) -> Result<(), std::io::Error> {
match self {
FileData::Bytes(data) => std::fs::write(dest, data),
FileData::Path(src) => {
match std::fs::rename(src, dest) {
Ok(()) => Ok(()),
Err(_) => {
std::fs::copy(src, dest)?;
let _ = std::fs::remove_file(src);
Ok(())
}
}
}
}
}
pub fn len(&self) -> Result<u64, std::io::Error> {
match self {
FileData::Bytes(data) => Ok(data.len() as u64),
FileData::Path(path) => std::fs::metadata(path).map(|m| m.len()),
}
}
pub fn is_empty(&self) -> Result<bool, std::io::Error> {
self.len().map(|n| n == 0)
}
pub fn copy_to(&self, dest: &Path) -> Result<(), std::io::Error> {
match self {
FileData::Bytes(data) => std::fs::write(dest, data),
FileData::Path(src) => {
std::fs::copy(src, dest)?;
Ok(())
}
}
}
pub fn into_bytes(self) -> Result<Vec<u8>, std::io::Error> {
match self {
FileData::Bytes(data) => Ok(data),
FileData::Path(path) => std::fs::read(path),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_filedata_bytes_write_to() {
let dir = std::env::temp_dir().join("bnto-test-filedata-bytes");
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let data = FileData::Bytes(b"hello world".to_vec());
let dest = dir.join("output.txt");
data.write_to(&dest).unwrap();
assert_eq!(std::fs::read(&dest).unwrap(), b"hello world");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn test_filedata_path_write_to_renames() {
let dir = std::env::temp_dir().join("bnto-test-filedata-path");
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let src = dir.join("source.bin");
std::fs::write(&src, b"file content").unwrap();
let data = FileData::Path(src.clone());
let dest = dir.join("moved.bin");
data.write_to(&dest).unwrap();
assert_eq!(std::fs::read(&dest).unwrap(), b"file content");
assert!(!src.exists());
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn test_filedata_bytes_len() {
let data = FileData::Bytes(vec![0u8; 42]);
assert_eq!(data.len().unwrap(), 42);
}
#[test]
fn test_filedata_path_len() {
let dir = std::env::temp_dir().join("bnto-test-filedata-len");
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join("sized.bin");
std::fs::write(&path, vec![0u8; 1024]).unwrap();
let data = FileData::Path(path);
assert_eq!(data.len().unwrap(), 1024);
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn test_filedata_bytes_is_empty() {
assert!(FileData::Bytes(vec![]).is_empty().unwrap());
assert!(!FileData::Bytes(vec![1]).is_empty().unwrap());
}
#[test]
fn test_filedata_bytes_copy_to() {
let dir = std::env::temp_dir().join("bnto-test-filedata-bytes-copy");
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let data = FileData::Bytes(b"copy me".to_vec());
let dest = dir.join("copied.txt");
data.copy_to(&dest).unwrap();
assert_eq!(std::fs::read(&dest).unwrap(), b"copy me");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn test_filedata_path_copy_to_preserves_source() {
let dir = std::env::temp_dir().join("bnto-test-filedata-path-copy");
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let src = dir.join("source.bin");
std::fs::write(&src, b"original content").unwrap();
let data = FileData::Path(src.clone());
let dest = dir.join("copied.bin");
data.copy_to(&dest).unwrap();
assert_eq!(std::fs::read(&dest).unwrap(), b"original content");
assert!(src.exists(), "Source should still exist after copy_to");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn test_filedata_bytes_into_bytes() {
let data = FileData::Bytes(b"content".to_vec());
assert_eq!(data.into_bytes().unwrap(), b"content");
}
#[test]
fn test_filedata_path_into_bytes() {
let dir = std::env::temp_dir().join("bnto-test-filedata-into");
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join("read-me.bin");
std::fs::write(&path, b"disk content").unwrap();
let data = FileData::Path(path);
assert_eq!(data.into_bytes().unwrap(), b"disk content");
let _ = std::fs::remove_dir_all(&dir);
}
}