pub struct FileTree {
pub path: PathBuf,
pub file_type: FileTreeType,
}
Expand description
A filesystem tree recursive type.
Fields
path: PathBuf
The filename of this file.
file_type: FileTreeType
The filetype of this file.
Implementations
sourceimpl FileTree
impl FileTree
sourcepub fn new_regular(path: impl AsRef<Path>) -> Self
pub fn new_regular(path: impl AsRef<Path>) -> Self
Creates a FileTree::Regular
from arguments.
sourcepub fn new_directory(path: impl AsRef<Path>, children: Vec<Self>) -> Self
pub fn new_directory(path: impl AsRef<Path>, children: Vec<Self>) -> Self
Creates a FileTree::Directory
from arguments.
sourcepub fn new_symlink(path: impl AsRef<Path>, target_path: impl AsRef<Path>) -> Self
pub fn new_symlink(path: impl AsRef<Path>, target_path: impl AsRef<Path>) -> Self
Creates a FileTree::Symlink
from arguments.
sourcepub fn collect_from_directory(path: impl AsRef<Path>) -> Result<Vec<Self>>
pub fn collect_from_directory(path: impl AsRef<Path>) -> Result<Vec<Self>>
Collects a Vec
of FileTree
from path
that is a directory.
sourcepub fn collect_from_directory_symlink(
path: impl AsRef<Path>
) -> Result<Vec<Self>>
pub fn collect_from_directory_symlink(
path: impl AsRef<Path>
) -> Result<Vec<Self>>
Collects a Vec
of FileTree
from path
that is a directory, entries can be symlinks.
sourcepub fn collect_from_directory_cd(path: impl AsRef<Path>) -> Result<Vec<Self>>
pub fn collect_from_directory_cd(path: impl AsRef<Path>) -> Result<Vec<Self>>
Collects a Vec
of FileTree
from path
that is a directory.
sourcepub fn collect_from_directory_symlink_cd(
path: impl AsRef<Path>
) -> Result<Vec<Self>>
pub fn collect_from_directory_symlink_cd(
path: impl AsRef<Path>
) -> Result<Vec<Self>>
Collects a Vec
of FileTree
from path
that is a directory, entries can be symlinks.
sourcepub fn from_path(path: impl AsRef<Path>) -> Result<Self>
pub fn from_path(path: impl AsRef<Path>) -> Result<Self>
Builds a FileTree
from path
, follows symlinks.
Similar to from_path_symlink
.
If file at path
is a regular file, will return a FileTree::Regular
.
If file at path
is a directory file, FileTree::Directory
(with .children).
Errors:
- If
Io::Error
fromfs::metadata(path)
- If it is a directory, and
Io::Error
fromfs::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(FileTree::Symlink { .. ]})
, if you wish otherwise, use
FileTree::from_path_symlink
instead.
sourcepub fn from_path_symlink(path: impl AsRef<Path>) -> Result<Self>
pub fn from_path_symlink(path: impl AsRef<Path>) -> Result<Self>
Builds a FileTree
from path
, follows symlinks.
Similar to from_path_symlink
.
If file at path
is a regular file, will return a FileTree::Regular
.
If file at path
is a directory file, FileTree::Directory
(with children
field).
If file at path
is a symlink file, FileTree::Symlink
(with target_path
field).
Errors:
- If
Io::Error
fromfs::metadata(path)
- If it is a directory, and
Io::Error
fromfs::read_dir(path)
iterator usage - If it is a symlink, and
Io::Error
fromfs::read_link(path)
- If unexpected file type at
path
If you wish to traverse symlinks until final destination, instead, use
FileTree::from_path
.
sourcepub fn from_path_cd(path: impl AsRef<Path>) -> Result<Self>
pub fn from_path_cd(path: impl AsRef<Path>) -> Result<Self>
cd
into path, run from_path
, and come back.
TODO explain here why this is useful
sourcepub fn from_cd_symlink_path(path: impl AsRef<Path>) -> Result<Self>
pub fn from_cd_symlink_path(path: impl AsRef<Path>) -> Result<Self>
cd
into path, run from_path_symlink
, and come back.
TODO explain here why this is useful
sourcepub fn from_path_text(path: impl AsRef<Path>) -> Option<Self>
pub fn from_path_text(path: impl AsRef<Path>) -> Option<Self>
Splits a Path
components into a FileTree
.
Returns None
if the string is empty.
Can only build Regular and Directory, not symlink.
Example:
use fs_tree::FileTree;
let result = FileTree::from_path_text(".config/i3/file");
let expected = {
FileTree::new_directory(
".config/",
vec![FileTree::new_directory(
".config/i3/",
vec![FileTree::new_regular(".config/i3/file")],
)],
)
};
assert_eq!(result, Some(expected));
sourcepub fn from_path_pieces<I, P>(path_iter: I) -> Option<Self> where
I: IntoIterator<Item = P>,
P: AsRef<Path>,
pub fn from_path_pieces<I, P>(path_iter: I) -> Option<Self> where
I: IntoIterator<Item = P>,
P: AsRef<Path>,
More generic version of FileTree::from_path_text
.
sourcepub fn files(&self) -> FilesIter<'_>ⓘNotable traits for FilesIter<'a>impl<'a> Iterator for FilesIter<'a> type Item = &'a FileTree;
pub fn files(&self) -> FilesIter<'_>ⓘNotable traits for FilesIter<'a>impl<'a> Iterator for FilesIter<'a> type Item = &'a FileTree;
Iterator of all FileTree
s in the structure
sourcepub fn paths(&self) -> PathsIter<'_>ⓘNotable traits for PathsIter<'_>impl Iterator for PathsIter<'_> type Item = PathBuf;
pub fn paths(&self) -> PathsIter<'_>ⓘNotable traits for PathsIter<'_>impl Iterator for PathsIter<'_> type Item = PathBuf;
Shorthand for self.files().paths()
, see link to .paths()
method
sourcepub fn make_paths_relative(&mut self)
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.
sourcepub fn make_paths_absolute(&mut self) -> Result<()>
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.
sourcepub fn merge(self, other: Self) -> Option<Self>
pub fn merge(self, other: Self) -> Option<Self>
Merge this tree with other FileTree
.
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.
sourcepub fn children_mut(&mut self) -> Option<&mut Vec<Self>>
pub fn children_mut(&mut self) -> Option<&mut Vec<Self>>
Reference to children vec if self.is_directory(), mutable.
sourcepub fn target_mut(&mut self) -> Option<&mut PathBuf>
pub fn target_mut(&mut self) -> Option<&mut PathBuf>
Reference to target_path if self.is_symlink(), mutable.
sourcepub fn apply_to_children0(&mut self, f: impl FnMut(&mut Self))
pub fn apply_to_children0(&mut self, f: impl FnMut(&mut Self))
Apply a closure for each direct child of this FileTree.
Only 1 level deep.
sourcepub fn apply_to_all_children1(&mut self, f: impl FnMut(&mut Self) + Copy)
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.
sourcepub fn apply_to_all(&mut self, f: impl FnMut(&mut Self) + Copy)
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.
sourcepub fn path_mut(&mut self) -> &mut PathBuf
pub fn path_mut(&mut self) -> &mut PathBuf
Gets a mutable reference to the file path to this node.
sourcepub fn is_regular(&self) -> bool
pub fn is_regular(&self) -> bool
Shorthand for file.file_type.is_regular()
sourcepub fn is_symlink(&self) -> bool
pub fn is_symlink(&self) -> bool
Shorthand for file.file_type.is_symlink()
sourcepub fn to_regular(&mut self)
pub fn to_regular(&mut 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.
sourcepub fn to_directory(&mut self, children: Vec<Self>)
pub fn to_directory(&mut self, children: Vec<Self>)
Turn this node of the tree into a directory.
Beware the possible recursive drop of nested nodes if this node was a directory.
sourcepub fn to_symlink(&mut self, target_path: impl AsRef<Path>)
pub fn to_symlink(&mut self, target_path: impl AsRef<Path>)
Turn this node of the tree into a symlink.
Beware the possible recursive drop of nested nodes if this node was a directory.
sourcepub fn has_same_type_as(&self, other: &FileTree) -> bool
pub fn has_same_type_as(&self, other: &FileTree) -> bool
Checks if the FileTree file type is the same as other FileTree.
Trait Implementations
sourceimpl Ord for FileTree
impl Ord for FileTree
1.21.0 · sourcefn max(self, other: Self) -> Self
fn max(self, other: Self) -> Self
Compares and returns the maximum of two values. Read more
1.21.0 · sourcefn min(self, other: Self) -> Self
fn min(self, other: Self) -> Self
Compares and returns the minimum of two values. Read more
1.50.0 · sourcefn clamp(self, min: Self, max: Self) -> Self where
Self: PartialOrd<Self>,
fn clamp(self, min: Self, max: Self) -> Self where
Self: PartialOrd<Self>,
Restrict a value to a certain interval. Read more
sourceimpl PartialOrd<FileTree> for FileTree
impl PartialOrd<FileTree> for FileTree
sourcefn partial_cmp(&self, other: &FileTree) -> Option<Ordering>
fn partial_cmp(&self, other: &FileTree) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
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 · sourcefn le(&self, other: &Rhs) -> bool
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
impl Eq for FileTree
impl StructuralEq for FileTree
impl StructuralPartialEq for FileTree
Auto Trait Implementations
impl RefUnwindSafe for FileTree
impl Send for FileTree
impl Sync for FileTree
impl Unpin for FileTree
impl UnwindSafe for FileTree
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more