Enum fs_tree::FsTree

source ·
pub enum FsTree {
    Regular,
    Directory(TrieMap),
    Symlink(PathBuf),
}
Expand description

A filesystem tree recursive type.

Iterators:

See the iterator module documentation.

Variants§

§

Regular

A regular file.

§

Directory(TrieMap)

A directory, might have children FsTrees inside.

Symbolic link, and it’s target path (the link might be broken).

Implementations§

source§

impl FsTree

source

pub fn new_dir() -> Self

Creates an empty directory node.

This is an alias to FsTree::Directory(Default::default()).

use fs_tree::{FsTree, TrieMap};

let result = FsTree::new_dir();
let expected = FsTree::Directory(TrieMap::new());

assert_eq!(result, expected);
source

pub fn len_leafs(&self) -> usize

Calculate the length by counting the leafs.

source

pub fn len_all(&self) -> usize

Calculate the length by counting all tree nodes, including the root.

source

pub fn read_at(path: impl AsRef<Path>) -> Result<Self>

Construct a FsTree by reading from path, follows symlinks.

If you want symlink-awareness, check symlink_read_at.

Errors:
  • If any IO error occurs.
  • If any file has an unexpected file type.

Construct a FsTree by reading from path.

If you don’t want symlink-awareness, check read_at.

Errors:
  • If any IO error occurs.
  • If any file has an unexpected file type.
source

pub fn read_copy_at(&self, path: impl AsRef<Path>) -> Result<Self>

Construct a structural copy of this FsTree by reading files at the given path.

In other words, the returned tree is formed of all paths in self that are also found in the given path (intersection), missing files are skipped and types might differ.

This function can be useful if you need to load a subtree from a huge folder and cannot afford to load the whole folder, or if you just want to filter out every node outside of the specified structure.

This function will make at maximum self.len() syscalls.

If you don’t want symlink-awareness, check FsTree::symlink_read_copy_at.

Examples:
use fs_tree::FsTree;

fn dynamically_load_structure() -> FsTree {
    ...
}

let structure = dynamically_load_structure();

let new_tree = structure.read_copy_at("path_here").unwrap();

// It is guaranteed that every path in here is present in `structure`
for path in new_tree.paths() {
    assert!(structure.get(path).is_some());
}
Errors:

Construct a structural copy of this FsTree by reading files at the given path.

In other words, the returned tree is formed of all paths in self that are also found in the given path (intersection), missing files are skipped and types might differ.

This function can be useful if you need to load a subtree from a huge folder and cannot afford to load the whole folder, or if you just want to filter out every node outside of the specified structure.

This function will make at maximum self.len() syscalls.

If you don’t want symlink-awareness, check FsTree::read_copy_at.

Examples:
use fs_tree::FsTree;

fn dynamically_load_structure() -> FsTree {
    ...
}

let structure = dynamically_load_structure();

let new_tree = structure.symlink_read_copy_at("path_here").unwrap();

// It is guaranteed that every path in here is present in `structure`
for path in new_tree.paths() {
    assert!(structure.get(path).is_some());
}
Errors:
source

pub fn from_path_text(path: impl AsRef<Path>) -> Self

Construct a FsTree from path pieces.

Returns None if the input is empty.

Returned value can correspond to a regular file or directory, but not a symlink.

Warning

The last piece is always a file, so inputs ending with /, like Path::new("example/") are NOT parsed as directories.

This might change in the future, for my personal usage cases (author writing), this was always OK, but if you’d like this to change, open an issue 👍.

Examples:
use fs_tree::{FsTree, tree};

let result = FsTree::from_path_text("a/b/c");

let expected = tree! {
    a: {
        b: {
            c
        }
    }
};

// The expected tree
assert_eq!(result, expected);

// Nodes are nested
assert!(result.is_dir());
assert!(result["a"].is_dir());
assert!(result["a"]["b"].is_dir());
assert!(result["a"]["b"]["c"].is_regular());
source

pub fn from_path_pieces<I, P>(path_iter: I) -> Self
where I: IntoIterator<Item = P>, P: Into<PathBuf>,

Generic iterator version of from_path_text.

source

pub fn iter(&self) -> Iter<'_>

Creates an iterator that yields (&FsTree, PathBuf).

See iterator docs at the iter module documentation.

source

pub fn nodes(&self) -> NodesIter<'_>

Creates an iterator that yields &FsTree.

See iterator docs at the iter module documentation.

source

pub fn paths(&self) -> PathsIter<'_>

Creates an iterator that yields PathBuf.

See iterator docs at the iter module documentation.

source

pub fn is_same_type_as(&self, other: &Self) -> bool

Returns true if self type matches other type.

source

pub fn try_exists(&mut self) -> Result<bool>

Returns Ok(true) if all nodes exist in the filesystem.

Errors:

Similar to how Path::try_exists works, this function returns false if any IO error occurred when checking std::fs::symlink_metadata (except io::ErrorKind::NotFound).

source

pub fn try_merge(self, other: Self) -> Option<Self>

Merge two trees.

Errors:
  • Returns None if contents of both trees conflict.
source

pub fn children(&self) -> Option<&TrieMap>

Reference to children vec if self.is_directory().

source

pub fn children_mut(&mut self) -> Option<&mut TrieMap>

Reference to children vec if self.is_directory(), mutable.

source

pub fn target(&self) -> Option<&Path>

Reference to target path, if self is a symlink.

source

pub fn target_mut(&mut self) -> Option<&mut PathBuf>

Reference to target path, if self is a symlink, mutable.

source

pub fn is_leaf(&self) -> bool

Returns true if self is a leaf node.

source

pub fn variant_str(&self) -> &'static str

The variant string.

source

pub fn is_regular(&self) -> bool

Returns true if self matches the regular variant.

source

pub fn is_dir(&self) -> bool

Returns true if self matches the directory variant.

Returns true if self matches the symlink variant.

source

pub fn write_at(&self, folder: impl AsRef<Path>) -> Result<()>

Write the tree structure in the path.

Errors:
  • If provided folder doesn’t exist, or is not a directory.
  • If any other IO error occurs.
source

pub fn get(&self, path: impl AsRef<Path>) -> Option<&Self>

Returns a reference to the node at the path, if any.

Errors:
  • Returns None if there is no node at the given path.
Examples:
use fs_tree::FsTree;

let root = FsTree::from_path_text("a/b/c");

// Indexing is relative from `root`, so `root` cannot be indexed.
assert_eq!(root, FsTree::from_path_text("a/b/c"));
assert_eq!(root["a"], FsTree::from_path_text("b/c"));
assert_eq!(root["a/b"], FsTree::from_path_text("c"));
assert_eq!(root["a"]["b"], FsTree::from_path_text("c"));
assert_eq!(root["a/b/c"], FsTree::Regular);
assert_eq!(root["a/b"]["c"], FsTree::Regular);
assert_eq!(root["a"]["b/c"], FsTree::Regular);
assert_eq!(root["a"]["b"]["c"], FsTree::Regular);
source

pub fn get_mut(&mut self, path: impl AsRef<Path>) -> Option<&mut Self>

Returns a mutable reference to the node at the path, if any.

This is the mutable version of FsTree::get.

source

pub fn insert(&mut self, path: impl AsRef<Path>, node: Self)

Inserts a node at the given path.

Panics:
  • If there are no directories up to the path node in order to insert it.
  • If path is empty.

Trait Implementations§

source§

impl Clone for FsTree

source§

fn clone(&self) -> FsTree

Returns a copy 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 Debug for FsTree

source§

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

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

impl Hash for FsTree

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<P> Index<P> for FsTree
where P: AsRef<Path>,

§

type Output = FsTree

The returned type after indexing.
source§

fn index(&self, path: P) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl Ord for FsTree

source§

fn cmp(&self, other: &FsTree) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq for FsTree

source§

fn eq(&self, other: &FsTree) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for FsTree

source§

fn partial_cmp(&self, other: &FsTree) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

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

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

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

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

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

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Eq for FsTree

source§

impl StructuralEq for FsTree

source§

impl StructuralPartialEq for FsTree

Auto Trait Implementations§

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

§

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

§

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

§

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.