diff --git a/crates/gitbutler-core/src/lib.rs b/crates/gitbutler-core/src/lib.rs index eacfae098..7faa57f0e 100644 --- a/crates/gitbutler-core/src/lib.rs +++ b/crates/gitbutler-core/src/lib.rs @@ -14,7 +14,6 @@ )] pub mod askpass; -// pub mod assets; pub mod dedup; pub mod error; pub mod fs; @@ -29,8 +28,6 @@ pub mod ssh; pub mod storage; pub mod time; pub mod types; -// pub mod users; pub mod virtual_branches; #[cfg(target_os = "windows")] pub mod windows; -pub mod writer; diff --git a/crates/gitbutler-core/src/writer.rs b/crates/gitbutler-core/src/writer.rs deleted file mode 100644 index dff82d562..000000000 --- a/crates/gitbutler-core/src/writer.rs +++ /dev/null @@ -1,114 +0,0 @@ -use std::path::Path; - -use anyhow::Result; - -use crate::lock; - -pub struct DirWriter(lock::Dir); - -impl DirWriter { - pub fn open>(root: P) -> Result { - lock::Dir::new(root).map(Self) - } -} - -impl DirWriter { - fn write(&self, path: P, contents: C) -> Result<(), std::io::Error> - where - P: AsRef, - C: AsRef<[u8]>, - { - self.batch(&[BatchTask::Write(path, contents)]) - } - - pub fn remove>(&self, path: P) -> Result<(), std::io::Error> { - self.0.batch(|root| { - let path = root.join(path); - if path.exists() { - if path.is_dir() { - std::fs::remove_dir_all(path) - } else { - std::fs::remove_file(path) - } - } else { - Ok(()) - } - })? - } - - pub fn batch(&self, values: &[BatchTask]) -> Result<(), std::io::Error> - where - P: AsRef, - C: AsRef<[u8]>, - { - self.0.batch(|root| { - for value in values { - match value { - BatchTask::Write(path, contents) => { - let path = root.join(path); - if let Some(dir_path) = path.parent() { - if !dir_path.exists() { - std::fs::create_dir_all(dir_path)?; - } - }; - std::fs::write(path, contents)?; - } - BatchTask::Remove(path) => { - let path = root.join(path); - if path.exists() { - if path.is_dir() { - std::fs::remove_dir_all(path)?; - } else { - std::fs::remove_file(path)?; - } - } - } - } - } - Ok(()) - })? - } - - pub fn write_string>( - &self, - path: P, - contents: &str, - ) -> Result<(), std::io::Error> { - self.write(path, contents) - } -} - -pub enum BatchTask, C: AsRef<[u8]>> { - Write(P, C), - Remove(P), -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn write() { - let root = tempfile::tempdir().unwrap(); - let writer = DirWriter::open(root.path()).unwrap(); - writer.write("foo/bar", b"baz").unwrap(); - assert_eq!( - std::fs::read_to_string(root.path().join("foo/bar")).unwrap(), - "baz" - ); - } - - #[test] - fn remove() { - let root = tempfile::tempdir().unwrap(); - let writer = DirWriter::open(root.path()).unwrap(); - 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()); - writer.write("parent/child", b"baz").unwrap(); - writer.remove("parent").unwrap(); - assert!(!root.path().join("parent").exists()); - } -}