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::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::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(())
}Modules§
- json
json - With the
jsonfeature, this module provides theJsontype, - ron
ron - With the
ronfeature, this module provides theRontype, - toml
toml - With the
tomlfeature, this module provides theTomltype, - yaml
yaml - With the
yamlfeature, this module provides theYamltype,
Macros§
- dir_
children_ wrapper - A simple macro that generates a
DirChildrennewtype, together with a few impls to make it easy to use.
Structs§
- Clean
Dir - A newtype that will clean the directory it is written to, before writing the value.
- Clean
DirRef Wr WriteToimpl forCleanDir- Deferred
Read - A wrapper that defers the reading of a file until it is actually needed.
- DirChild
- A single child of a
DirChildrenstructure. - DirChildren
- A directory structure where we don’t know the names of the folders at compile-time, and as such we cannot use the derive macro.
- DirChildren
Iter - A
DirChildreniterator. It iterates over the children of aDirChildrenstructure. - File
Bytes - A newtype around a
Vec<u8>. - File
Bytes RefWr - The
WriteTowrapper around a reference to a[u8]. - File
StrWr - The
WriteTowrapper around a reference to astr. - File
String - A newtype around a
String. - FmtWrapper
- A wrapper around a type which will use the
DisplayandFromStrimplementations for serialization / deserialization. - FmtWrapper
RefWr - A
WriteTowrapper around a reference to a type which will use theDisplayimplementation to write the value. - NoFilter
- A
Filterthat allows all paths. - Versioned
- A versioned value. This is a wrapper around a value that will keep track of how many times it has been changed. This is useful to not write the value to disk if it hasn’t changed.
Enums§
- Deferred
Read OrOwn - A wrapper that defers the reading of a file until it is actually needed, but can also store the value.
- Error
- The error type for this library.
Traits§
- DirStructure
- The main trait. This is implemented for all directory structures by the derive macro.
- DirStructure
Item - Helper trait, implemented for all types that have a
ReadFromandWriteToimplementation. - Filter
- A filter for the children of a
DirChildrenstructure. - From
RefFor Writer - Trait to use when using the
with_newtypeattribute. - Newtype
ToInner - Trait to use when using the
with_newtypeattribute. - Read
From - Trait for types / structures that can be read from disk, either from a file or a directory.
- WriteTo
- Trait for types / structures that can be written to disk. All types in the library that write to files first check that the parent directories exist, so implementations of this that create the whole directory are not necessary (unless used empty children directories, in which case no directories will really be created).