pub struct CleanDir<T: DirStructureItem>(pub T);Expand description
A newtype that will clean the directory it is written to, before writing the value.
This is useful when we want to write a directory structure, but we want to make sure that the directory is clean before writing it, so that there are no old files / directories left in it.
use std::path::Path;
use dir_structure::DirStructureItem;
use dir_structure::CleanDir;
#[derive(dir_structure::DirStructure)]
struct Dir {
#[dir_structure(path = "f.txt")]
f: String,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let d = Path::new("dir");
std::fs::create_dir_all(&d)?;
std::fs::write(d.join("f.txt"), "Hello, world!")?;
std::fs::write(d.join("f2.txt"), "Hello, world! (2)")?;
let dir = Dir::read(&d)?;
assert_eq!(dir.f, "Hello, world!");
assert_eq!(std::fs::read_to_string(d.join("f2.txt"))?, "Hello, world! (2)");
CleanDir(dir).write(&d)?;
assert_eq!(std::fs::read_to_string(d.join("f.txt"))?, "Hello, world!");
assert!(!d.join("f2.txt").exists());
Ok(())
}Tuple Fields§
§0: TTrait Implementations§
Source§impl<'a, T> FromRefForWriter<'a> for CleanDir<T>where
T: DirStructureItem + 'a,
impl<'a, T> FromRefForWriter<'a> for CleanDir<T>where
T: DirStructureItem + 'a,
Source§impl<T> NewtypeToInner for CleanDir<T>where
T: DirStructureItem,
impl<T> NewtypeToInner for CleanDir<T>where
T: DirStructureItem,
Source§impl<T> ReadFrom for CleanDir<T>where
T: DirStructureItem,
impl<T> ReadFrom for CleanDir<T>where
T: DirStructureItem,
Auto Trait Implementations§
impl<T> Freeze for CleanDir<T>where
T: Freeze,
impl<T> RefUnwindSafe for CleanDir<T>where
T: RefUnwindSafe,
impl<T> Send for CleanDir<T>where
T: Send,
impl<T> Sync for CleanDir<T>where
T: Sync,
impl<T> Unpin for CleanDir<T>where
T: Unpin,
impl<T> UnwindSafe for CleanDir<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more