2023-04-14 13:25:07 +03:00
|
|
|
use anyhow::{Context, Result};
|
|
|
|
|
2023-04-17 12:13:57 +03:00
|
|
|
pub struct DirWriter {
|
|
|
|
root: std::path::PathBuf,
|
2023-04-12 15:14:39 +03:00
|
|
|
}
|
|
|
|
|
2023-05-31 10:29:26 +03:00
|
|
|
impl DirWriter {
|
2023-04-17 12:13:57 +03:00
|
|
|
pub fn open(root: std::path::PathBuf) -> Self {
|
2023-04-13 14:37:46 +03:00
|
|
|
Self { root }
|
|
|
|
}
|
2023-04-12 15:14:39 +03:00
|
|
|
}
|
|
|
|
|
2023-12-20 17:08:24 +03:00
|
|
|
impl DirWriter {
|
2023-04-17 12:13:57 +03:00
|
|
|
fn write(&self, path: &str, contents: &[u8]) -> Result<()> {
|
2023-04-13 14:37:46 +03:00
|
|
|
let file_path = self.root.join(path);
|
2023-10-19 16:25:27 +03:00
|
|
|
let dir_path = file_path.parent().context("failed to get parent")?;
|
2023-06-05 17:12:25 +03:00
|
|
|
std::fs::create_dir_all(dir_path).context("failed to create directory")?;
|
|
|
|
std::fs::write(file_path, contents)?;
|
2023-04-12 15:14:39 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-12-20 17:08:24 +03:00
|
|
|
pub fn remove(&self, path: &str) -> Result<()> {
|
2023-06-07 13:43:07 +03:00
|
|
|
let file_path = self.root.join(path);
|
2023-07-01 11:30:00 +03:00
|
|
|
if file_path.is_dir() {
|
|
|
|
match std::fs::remove_dir_all(file_path) {
|
2023-10-11 12:29:41 +03:00
|
|
|
Ok(()) => Ok(()),
|
2023-07-01 11:30:00 +03:00
|
|
|
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
|
|
|
|
Err(e) => Err(e.into()),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
match std::fs::remove_file(file_path) {
|
2023-10-11 12:29:41 +03:00
|
|
|
Ok(()) => Ok(()),
|
2023-07-01 11:30:00 +03:00
|
|
|
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
|
|
|
|
Err(e) => Err(e.into()),
|
|
|
|
}
|
2023-06-07 13:43:07 +03:00
|
|
|
}
|
|
|
|
}
|
2023-12-20 17:08:24 +03:00
|
|
|
|
|
|
|
pub fn write_usize(&self, path: &str, contents: &usize) -> Result<()> {
|
|
|
|
self.write_string(path, &contents.to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write_u128(&self, path: &str, contents: &u128) -> Result<()> {
|
|
|
|
self.write_string(path, &contents.to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write_bool(&self, path: &str, contents: &bool) -> Result<()> {
|
|
|
|
self.write_string(path, &contents.to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write_string(&self, path: &str, contents: &str) -> Result<()> {
|
|
|
|
self.write(path, contents.as_bytes())
|
|
|
|
}
|
2023-04-12 15:14:39 +03:00
|
|
|
}
|
2023-06-05 17:12:25 +03:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_write() {
|
|
|
|
let root = tempfile::tempdir().unwrap();
|
|
|
|
let writer = DirWriter::open(root.path().to_path_buf());
|
|
|
|
writer.write("foo/bar", b"baz").unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
std::fs::read_to_string(root.path().join("foo/bar")).unwrap(),
|
|
|
|
"baz"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-06-07 13:43:07 +03:00
|
|
|
#[test]
|
|
|
|
fn test_remove() {
|
|
|
|
let root = tempfile::tempdir().unwrap();
|
|
|
|
let writer = DirWriter::open(root.path().to_path_buf());
|
|
|
|
writer.remove("foo/bar").unwrap();
|
|
|
|
assert!(!root.path().join("foo/bar").exists());
|
|
|
|
writer.write("foo/bar", b"baz").unwrap();
|
|
|
|
writer.remove("foo/bar").unwrap();
|
|
|
|
assert!(!root.path().join("foo/bar").exists());
|
2023-07-01 11:30:00 +03:00
|
|
|
writer.write("parent/child", b"baz").unwrap();
|
|
|
|
writer.remove("parent").unwrap();
|
|
|
|
assert!(!root.path().join("parent").exists());
|
2023-06-07 13:43:07 +03:00
|
|
|
}
|
2023-06-05 17:12:25 +03:00
|
|
|
}
|