Struct fs_tree::FsTree

source ·
pub struct FsTree {
    pub path: PathBuf,
    pub file_type: TreeNode,
}
Expand description

A filesystem tree recursive type.

Methods for iteration: .iter(), .nodes() or .paths().

Fields§

§path: PathBuf

The filename of this file.

§file_type: TreeNode

The TreeNode of this file.

Implementations§

source§

impl FsTree

source

pub fn new_regular(path: impl Into<PathBuf>) -> Self

Creates a FsTree::Regular from arguments.

source

pub fn new_directory(path: impl Into<PathBuf>, children: Vec<Self>) -> Self

Creates a FsTree::Directory from arguments.

Creates a FsTree::Symlink from arguments.

source

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

Collects a Vec of FsTree from path that is a directory.

Collects a Vec of FsTree from path that is a directory, entries can be symlinks.

source

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

Builds a FsTree from path, follows symlinks.

Similar to from_path_symlink.

If file at path is a regular file, will return a FsTree::Regular. If file at path is a directory file, FsTree::Directory (with .children).

Errors:
  • If Io::Error from fs::metadata(path)
  • If it is a directory, and Io::Error from fs::read_dir(path) iterator usage
  • If unexpected file type at path

This function traverses symlinks until final destination, and then reads it, so it can never return Ok(FsTree::Symlink { .. ]}), if you wish otherwise, use FsTree::from_path_symlink instead.

Builds a FsTree from path, follows symlinks.

Similar to from_path_symlink.

If file at path is a regular file, will return a FsTree::Regular. If file at path is a directory file, FsTree::Directory (with children field). If file at path is a symlink file, FsTree::Symlink (with target_path field).

Errors:
  • If Io::Error from fs::metadata(path)
  • If it is a directory, and Io::Error from fs::read_dir(path) iterator usage
  • If it is a symlink, and Io::Error from fs::read_link(path)
  • If unexpected file type at path

If you wish to traverse symlinks until final destination, instead, use FsTree::from_path.

source

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

Splits Path pieces into a FsTree.

Returns None if the string is empty.

Can only build Regular and Directory, not symlink.

Example:

use fs_tree::{FsTree, tree};

let result = FsTree::from_path_text("dir/inner/file");

let expected = tree! {
    dir: {
        inner: {
            file
        }
    }
};

assert_eq!(result, Some(expected));
source

pub fn from_path_pieces<I, P>(path_iter: I) -> Option<Self>where I: IntoIterator<Item = P>, P: AsRef<Path>,

Generic version of FsTree::from_path_text.

Returns None if path is empty.

source

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

An iterator over (&FsTree, PathBuf).

source

pub fn files(&self) -> FilesIter<'_>

Iterator of all FsTrees in the structure

source

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

Shorthand for self.files().paths(), see link to .paths() method

source

pub fn make_paths_relative(&mut self)

Fix relative paths from each node piece.

If you manually build a structure like:

"a": [
    "b": [
        "c",
    ]
]

Using the create methods, then you need to run this function to make them relative paths.

"a": [
    "a/b": [
        "a/b/c",
    ]
]

Then, you can access any of the files only by looking at their path.

source

pub fn make_paths_absolute(&mut self) -> Result<()>

Makes all paths in the tree absolute.

Errors:

In case std::fs::canonicalize fails at any path, this function will stop and return an IoError, leave the tree in a mixed state in terms of canonical paths.

source

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

Merge this tree with other FsTree.

This function is currently experimental and likely to change in future versions.

Errors:

This errs if:

  • The trees have different roots and thus cannot be merged.
  • There are file conflicts.
source

pub fn children(&self) -> Option<&[Self]>

Reference to children vec if self.is_directory().

source

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

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

source

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

Reference to target_path if self.is_symlink().

source

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

Reference to target_path if self.is_symlink(), mutable.

source

pub fn apply_to_children0(&mut self, f: impl FnMut(&mut Self))

Apply a closure for each direct child of this FsTree.

Only 1 level deep.

source

pub fn apply_to_all_children1(&mut self, f: impl FnMut(&mut Self) + Copy)

Apply a closure to all direct and indirect descendants inside of this structure.

Calls recursively for all levels.

source

pub fn apply_to_all(&mut self, f: impl FnMut(&mut Self) + Copy)

Apply a closure to all direct and indirect descendants inside, also includes root.

Calls recursively for all levels.

source

pub fn is_regular(&self) -> bool

Shorthand for file.file_type.is_regular()

source

pub fn is_dir(&self) -> bool

Shorthand for file.file_type.is_dir()

Shorthand for file.file_type.is_symlink()

source

pub fn to_regular(self) -> Self

Turn this node of the tree into a regular file.

Beware the possible recursive drop of nested nodes if this node was a directory.

source

pub fn to_directory(self, children: Vec<Self>) -> Self

Turn this node of the tree into a directory.

Beware the possible recursive drop of nested nodes if this node was a directory.

Turn this node of the tree into a symlink.

Beware the possible recursive drop of nested nodes if this node was a directory.

source

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

Checks if the FsTree file type is the same as other FsTree.

source

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

Create the tree folder structure in the path

source

pub fn create(&self) -> Result<()>

Create FsTree in the current directory.

Alias to self.create_at(".").

source

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

Returns a reference to the node at the path.

Examples:
use fs_tree::FsTree;

let root = FsTree::from_path_text("root/b/c/d").unwrap();

// Indexing is relative from `root`, so `root` cannot be indexed.
assert!(root.get("root").is_none());

assert_eq!(root["b"], FsTree::from_path_text("b/c/d").unwrap());
assert_eq!(root["b/c"], FsTree::from_path_text("c/d").unwrap());
assert_eq!(root["b"]["c"], FsTree::from_path_text("c/d").unwrap());
assert_eq!(root["b/c/d"], FsTree::from_path_text("d").unwrap());
assert_eq!(root["b/c"]["d"], FsTree::from_path_text("d").unwrap());
assert_eq!(root["b"]["c/d"], FsTree::from_path_text("d").unwrap());
assert_eq!(root["b"]["c"]["d"], FsTree::from_path_text("d").unwrap());

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 FsTreewhere 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) -> Selfwhere Self: Sized,

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

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

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

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

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

impl PartialEq<FsTree> 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<FsTree> 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 Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere 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 Twhere 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 Twhere 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.