Expand description
A library for reading and writing directory structures.
This library provides a macro for defining directory structures, and a trait for reading and writing those structures to / from disk.
§Example
§Writing a structure to disk
use std::path::Path;
fn main() -> Result<(), Box<dyn std::error::Error>> {
use dir_structure::traits::sync::DirStructureItem;
#[derive(dir_structure::DirStructure)]
struct Dir {
#[dir_structure(path = "f1.txt")]
f1: String,
#[dir_structure(path = "subdir/f2.txt")]
f2: String,
// default path is just a file name from the field's name.
f3: String,
// also works with nested structures
#[dir_structure(path = "subdir2")]
subdir: Subdir,
}
#[derive(dir_structure::DirStructure)]
struct Subdir {
#[dir_structure(path = "f4.txt")]
f4: String,
}
let d = Path::new("dir");
Dir {
f1: "f1".to_owned(),
f2: "f2".to_owned(),
f3: "f3".to_owned(),
subdir: Subdir {
f4: "f4".to_owned(),
},
}.write(&d)?;
assert_eq!(std::fs::read_to_string(d.join("f1.txt"))?, "f1");
assert_eq!(std::fs::read_to_string(d.join("subdir/f2.txt"))?, "f2");
assert_eq!(std::fs::read_to_string(d.join("f3"))?, "f3");
assert_eq!(std::fs::read_to_string(d.join("subdir2/f4.txt"))?, "f4");
Ok(())
}§Reading a structure from disk
use std::path::Path;
fn main() -> Result<(), Box<dyn std::error::Error>> {
use dir_structure::traits::sync::DirStructureItem;
#[derive(dir_structure::DirStructure)]
struct Dir {
#[dir_structure(path = "f1.txt")]
f1: String,
#[dir_structure(path = "subdir/f2.txt")]
f2: String,
// default path is just a file name from the field's name.
f3: String,
// also works with nested structures
#[dir_structure(path = "subdir2")]
subdir: Subdir,
}
#[derive(dir_structure::DirStructure)]
struct Subdir {
#[dir_structure(path = "f4.txt")]
f4: String,
}
let d = Path::new("dir");
std::fs::create_dir_all(&d)?;
std::fs::create_dir_all(d.join("subdir"))?;
std::fs::create_dir_all(d.join("subdir2"))?;
std::fs::write(d.join("f1.txt"), "f1")?;
std::fs::write(d.join("subdir/f2.txt"), "f2")?;
std::fs::write(d.join("f3"), "f3")?;
std::fs::write(d.join("subdir2/f4.txt"), "f4")?;
let dir = Dir::read(&d)?;
assert_eq!(dir.f1, "f1");
assert_eq!(dir.f2, "f2");
assert_eq!(dir.f3, "f3");
assert_eq!(dir.subdir.f4, "f4");
Ok(())
}Re-exports§
pub extern crate include_dir;pub extern crate pin_project;
Modules§
- atomic_
dir - A module to enable atomic directory writes.
- clean_
dir - A newtype that will clean the directory it is written to, before writing the value.
- data_
formats - Serde
ReadFromandWriteToimplementations. - deferred_
read - A wrapper that defers the reading of a file until it is actually needed.
- deferred_
read_ or_ own - A wrapper that defers the reading of a file until it is actually needed.
- dir_
children - A structure representing the children of a directory.
- dir_
descendants - A structure representing the descendants of a directory.
- error
- Error type, see
Error. - fmt_
wrapper ReadFromandWriteToimplementations usingFromStr::from_strandDisplay::fmt.- image
image - Implementations of
ReadFromandWriteTofor image files. - option
Option<T>implementations.- prelude
- A prelude for the most commonly used items in this crate.
- std_
types - Implementations for standard library types.
- traits
- Crate traits.
- try_
parse - A wrapper that tries to parse a value of type
T, keeping the original error if it fails. - versioned
- A versioned value.
- versioned_
hash - A module for working with versioned values.
- vfs
- Virtual file system implementations.
Macros§
- dir_
children_ wrapper_ with_ vfs - A wrapper around
DirChildrenthat adds the <’vfs, Vfs> generics. - ext_
filter - Creates a
Filtertype that only allows files / folders with a specific extension. - file_
prefix_ filter - Creates a
Filtertype that only allows files / folders with a specific prefix. - include_
dir_ vfs include_dir - Convenience macro to
include_dir!(...)and wrap it in anIncludeDirVfs. - stem_
filter - Creates a
Filtertype that only allows files / folders with a specific stem.
Structs§
- NoFilter
- A
Filter,FileFilter,FolderFilter, andFolderRecurseFilterthat allows all paths.