Struct DirChildren

Source
pub struct DirChildren<T, F: Filter = NoFilter>{
    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: PathBuf

The 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,

Source

pub fn new() -> Self

Creates an empty DirChildren, with no children.

Source

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.

Source

pub fn map<U, MapF>(self, f: MapF) -> DirChildren<U, F>
where MapF: FnMut(DirChild<T>) -> DirChild<U>, U: DirStructureItem,

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())),
        ],
    )
);
Source

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>();
Source

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);
Source

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);
Source

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);
Source

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);
Source

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>
where T: DirStructureItem + Clone, F: Filter,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T, F: Debug + Filter> Debug for DirChildren<T, F>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Default for DirChildren<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T> IntoIterator for DirChildren<T>

Source§

type Item = DirChild<T>

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<<DirChildren<T> as IntoIterator>::Item>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T, F: PartialEq + Filter> PartialEq for DirChildren<T, F>

Source§

fn eq(&self, other: &DirChildren<T, F>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T, F> ReadFrom for DirChildren<T, F>
where T: DirStructureItem, F: Filter,

Source§

fn read_from(path: &Path) -> Result<Self>
where Self: Sized,

Reads the structure from the specified path, which can be either a file or a directory.
Source§

impl<T, F> WriteTo for DirChildren<T, F>
where T: DirStructureItem, F: Filter,

Source§

fn write_to(&self, path: &Path) -> Result<()>

Writes the structure to the specified path.
Source§

impl<T, F: Eq + Filter> Eq for DirChildren<T, F>
where T: DirStructureItem + Eq,

Source§

impl<T, F: Filter> StructuralPartialEq for DirChildren<T, F>

Auto Trait Implementations§

§

impl<T, F> Freeze for DirChildren<T, F>

§

impl<T, F> RefUnwindSafe for DirChildren<T, F>

§

impl<T, F> Send for DirChildren<T, F>
where F: Send, T: Send,

§

impl<T, F> Sync for DirChildren<T, F>
where F: Sync, T: Sync,

§

impl<T, F> Unpin for DirChildren<T, F>
where F: Unpin, T: Unpin,

§

impl<T, F> UnwindSafe for DirChildren<T, F>
where F: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DirStructureItem for T
where T: ReadFrom + WriteTo,

Source§

fn read(path: impl AsRef<Path>) -> Result<Self>
where Self: Sized,

Uses the ReadFrom implementation to read the structure from disk, from the specified path.
Source§

fn write(&self, path: impl AsRef<Path>) -> Result<()>

Uses the WriteTo implementation to write the structure to disk at the specified path.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.