pub struct DirChildren<T, F: Filter = NoFilter>where
T: DirStructureItem,{
pub self_path: PathBuf,
pub children: Vec<DirChild<T>>,
/* private fields */
}Expand description
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.
Instead we know that all the entries in the directory are folders,
and that they all have the same structure inside (defined by the T type parameter),
or they are all files (which can be read with DirChildren<String> for example).
In either case, ReadFrom::read_from must be able to read all the entries in
the directory.
The WriteTo implementation will directly write the children to the directory it
is passed, with no regards to the path stored in self_path.
Fields§
§self_path: PathBufThe path to the root directory.
This path doesn’t influence writing in any way, it is only to point out the directory after it has been read and parsed.
children: Vec<DirChild<T>>The children of the root directory.
Implementations§
Source§impl<T, F> DirChildren<T, F>where
T: DirStructureItem,
F: Filter,
impl<T, F> DirChildren<T, F>where
T: DirStructureItem,
F: Filter,
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates an empty DirChildren, with no children.
Sourcepub fn with_children_from_iter(
self_path: impl Into<PathBuf>,
children: impl IntoIterator<Item = DirChild<T>>,
) -> Self
pub fn with_children_from_iter( self_path: impl Into<PathBuf>, children: impl IntoIterator<Item = DirChild<T>>, ) -> Self
Creates a DirChildren with the given path and children.
Sourcepub fn map<U, MapF>(self, f: MapF) -> DirChildren<U, F>
pub fn map<U, MapF>(self, f: MapF) -> DirChildren<U, F>
Maps the children of this DirChildren to a new type.
This is useful for converting the children to a different type,
for example, if you want to convert the children to a different
type of DirStructureItem.
This is a convenience method that allows you to use the
map method on the children of this DirChildren.
§Examples
use std::path::Path;
use std::path::PathBuf;
use dir_structure::{DirStructure, DirStructureItem, DirChildren, DirChild, ReadFrom, WriteTo};
#[derive(Debug, PartialEq, Eq)]
struct NewType(String);
impl ReadFrom for NewType {
fn read_from(path: &Path) -> dir_structure::Result<Self> {
String::read_from(path).map(Self)
}
}
impl WriteTo for NewType {
fn write_to(&self, path: &Path) -> dir_structure::Result<()> {
self.0.write_to(path)
}
}
let d = PathBuf::from("dir");
let dir = DirChildren::<_, dir_structure::NoFilter>::with_children_from_iter(
d.clone(),
vec![
DirChild::new("file1.txt", "file1".to_owned()),
DirChild::new("file2.txt", "file2".to_owned()),
DirChild::new("file3.txt", "file3".to_owned()),
],
);
let dir = dir.map(|child| child.map_value(NewType));
assert_eq!(
dir,
DirChildren::with_children_from_iter(
d.clone(),
vec![
DirChild::new("file1.txt", NewType("file1".to_owned())),
DirChild::new("file2.txt", NewType("file2".to_owned())),
DirChild::new("file3.txt", NewType("file3".to_owned())),
],
)
);Sourcepub fn map_filter<NewF>(self) -> DirChildren<T, NewF>where
NewF: Filter,
pub fn map_filter<NewF>(self) -> DirChildren<T, NewF>where
NewF: Filter,
Maps the filter type. The children remain unchanged.
This is useful if you are trying to pass a DirChildren<T, F1> to
a function requiring a DirChildren<T, F2>, where F1 and F2 are two
distinct types implementing Filter.
§Examples
use std::path::Path;
use dir_structure::{Filter, DirChildren};
struct NewFilter;
impl Filter for NewFilter {
fn make_filter() -> Self {
Self
}
fn allows(&self, _path: &Path) -> bool {
true
}
}
let d = DirChildren::<String, dir_structure::NoFilter>::new();
let d2: DirChildren<String, NewFilter> = d.map_filter::<NewFilter>();Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of children.
§Examples
use std::path::{Path, PathBuf};
use dir_structure::{DirStructure, DirStructureItem, DirChildren, DirChild};
let d = DirChildren::<String, dir_structure::NoFilter>::new();
assert_eq!(d.len(), 0);
let d = DirChildren::<String, dir_structure::NoFilter>::with_children_from_iter(
PathBuf::new(),
vec![
DirChild::new("file1.txt", "file1".to_owned()),
DirChild::new("file2.txt", "file2".to_owned()),
],
);
assert_eq!(d.len(), 2);Sourcepub fn get(&self, index: usize) -> Option<&DirChild<T>>
pub fn get(&self, index: usize) -> Option<&DirChild<T>>
Gets the child at the specified index.
§Examples
use std::path::{Path, PathBuf};
use dir_structure::{DirStructure, DirStructureItem, DirChildren, DirChild};
let d = DirChildren::<String, dir_structure::NoFilter>::new();
assert_eq!(d.get(0), None);
assert_eq!(d.get(1), None);
assert_eq!(d.get(100), None);
let d = DirChildren::<String, dir_structure::NoFilter>::with_children_from_iter(
PathBuf::new(),
vec![
DirChild::new("file1.txt", "file1".to_owned()),
DirChild::new("file2.txt", "file2".to_owned()),
],
);
assert_eq!(d.get(0), Some(&DirChild::new("file1.txt", "file1".to_owned())));
assert_eq!(d.get(1), Some(&DirChild::new("file2.txt", "file2".to_owned())));
assert_eq!(d.get(2), None);
assert_eq!(d.get(100), None);Sourcepub fn get_name(&self, name: impl AsRef<OsStr>) -> Option<&DirChild<T>>
pub fn get_name(&self, name: impl AsRef<OsStr>) -> Option<&DirChild<T>>
Gets the child with the specified “file” name (last segment of path).
§Examples
use std::path::{Path, PathBuf};
use dir_structure::{DirStructure, DirStructureItem, DirChildren, DirChild};
let d = DirChildren::<String, dir_structure::NoFilter>::new();
assert_eq!(d.get_name(""), None);
assert_eq!(d.get_name("any_name"), None);
assert_eq!(d.get_name("aaaa"), None);
let d = DirChildren::<String, dir_structure::NoFilter>::with_children_from_iter(
PathBuf::new(),
vec![
DirChild::new("file1.txt", "file1".to_owned()),
DirChild::new("file2.txt", "file2".to_owned()),
],
);
assert_eq!(d.get_name("file1.txt"), Some(&DirChild::new("file1.txt", "file1".to_owned())));
assert_eq!(d.get_name("file2.txt"), Some(&DirChild::new("file2.txt", "file2".to_owned())));
assert_eq!(d.get_name("any_name"), None);
assert_eq!(d.get_name("aaaa"), None);Sourcepub fn get_value_by_name(&self, name: impl AsRef<OsStr>) -> Option<&T>
pub fn get_value_by_name(&self, name: impl AsRef<OsStr>) -> Option<&T>
Gets the value of the child with the specified “file” name (last segment of path).
§Examples
use std::path::{Path, PathBuf};
use dir_structure::{DirStructure, DirStructureItem, DirChildren, DirChild};
let d = DirChildren::<String, dir_structure::NoFilter>::new();
assert_eq!(d.get_value_by_name(""), None);
assert_eq!(d.get_value_by_name("any_name"), None);
assert_eq!(d.get_value_by_name("aaaa"), None);
let d = DirChildren::<String, dir_structure::NoFilter>::with_children_from_iter(
PathBuf::new(),
vec![
DirChild::new("file1.txt", "file1".to_owned()),
DirChild::new("file2.txt", "file2".to_owned()),
],
);
assert_eq!(d.get_value_by_name("file1.txt"), Some(&"file1".to_owned()));
assert_eq!(d.get_value_by_name("file2.txt"), Some(&"file2".to_owned()));
assert_eq!(d.get_value_by_name("any_name"), None);
assert_eq!(d.get_value_by_name("aaaa"), None);Sourcepub fn iter(&self) -> DirChildrenIter<'_, T> ⓘ
pub fn iter(&self) -> DirChildrenIter<'_, T> ⓘ
Returns an iterator over the children.
§Examples
use std::path::{Path, PathBuf};
use dir_structure::{DirStructure, DirStructureItem, DirChildren, DirChild};
let d = DirChildren::<String, dir_structure::NoFilter>::new();
let mut i = d.iter();
assert_eq!(i.next(), None);
let d = DirChildren::<String, dir_structure::NoFilter>::with_children_from_iter(
PathBuf::new(),
vec![
DirChild::new("file1.txt", "file1".to_owned()),
DirChild::new("file2.txt", "file2".to_owned()),
],
);
let mut i = d.iter();
assert_eq!(i.next(), Some(&DirChild::new("file1.txt", "file1".to_owned())));
assert_eq!(i.next(), Some(&DirChild::new("file2.txt", "file2".to_owned())));
assert_eq!(i.next(), None);Trait Implementations§
Source§impl<T, F> Clone for DirChildren<T, F>
impl<T, F> Clone for DirChildren<T, F>
Source§impl<T, F: Debug + Filter> Debug for DirChildren<T, F>where
T: DirStructureItem + Debug,
impl<T, F: Debug + Filter> Debug for DirChildren<T, F>where
T: DirStructureItem + Debug,
Source§impl<T> Default for DirChildren<T>where
T: DirStructureItem,
impl<T> Default for DirChildren<T>where
T: DirStructureItem,
Source§impl<T> IntoIterator for DirChildren<T>where
T: DirStructureItem,
impl<T> IntoIterator for DirChildren<T>where
T: DirStructureItem,
Source§impl<T, F: PartialEq + Filter> PartialEq for DirChildren<T, F>where
T: DirStructureItem + PartialEq,
impl<T, F: PartialEq + Filter> PartialEq for DirChildren<T, F>where
T: DirStructureItem + PartialEq,
Source§impl<T, F> ReadFrom for DirChildren<T, F>where
T: DirStructureItem,
F: Filter,
impl<T, F> ReadFrom for DirChildren<T, F>where
T: DirStructureItem,
F: Filter,
Source§impl<T, F> WriteTo for DirChildren<T, F>where
T: DirStructureItem,
F: Filter,
impl<T, F> WriteTo for DirChildren<T, F>where
T: DirStructureItem,
F: Filter,
impl<T, F: Eq + Filter> Eq for DirChildren<T, F>where
T: DirStructureItem + Eq,
impl<T, F: Filter> StructuralPartialEq for DirChildren<T, F>where
T: DirStructureItem,
Auto Trait Implementations§
impl<T, F> Freeze for DirChildren<T, F>
impl<T, F> RefUnwindSafe for DirChildren<T, F>where
F: RefUnwindSafe,
T: RefUnwindSafe,
impl<T, F> Send for DirChildren<T, F>
impl<T, F> Sync for DirChildren<T, F>
impl<T, F> Unpin for DirChildren<T, F>
impl<T, F> UnwindSafe for DirChildren<T, F>where
F: UnwindSafe,
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> DirStructureItem for T
impl<T> DirStructureItem for T
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.