pub struct FsTree {
pub path: PathBuf,
pub file_type: TreeNode,
}
Expand description
Fields§
§path: PathBuf
The filename of this file.
file_type: TreeNode
The TreeNode of this file.
Implementations§
source§impl FsTree
impl FsTree
sourcepub fn new_regular(path: impl Into<PathBuf>) -> Self
pub fn new_regular(path: impl Into<PathBuf>) -> Self
Creates a FsTree::Regular
from arguments.
sourcepub fn new_directory(path: impl Into<PathBuf>, children: Vec<Self>) -> Self
pub fn new_directory(path: impl Into<PathBuf>, children: Vec<Self>) -> Self
Creates a FsTree::Directory
from arguments.
sourcepub fn new_symlink(
path: impl Into<PathBuf>,
target_path: impl Into<PathBuf>
) -> Self
pub fn new_symlink( path: impl Into<PathBuf>, target_path: impl Into<PathBuf> ) -> Self
Creates a FsTree::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 FsTree
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 FsTree
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 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
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(FsTree::Symlink { .. ]})
, if you wish otherwise, use
FsTree::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 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
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
FsTree::from_path
.
sourcepub fn from_path_text(path: impl AsRef<Path>) -> Option<Self>
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));
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>,
Generic version of FsTree::from_path_text
.
Returns None
if path is empty.
sourcepub fn paths(&self) -> PathsIter<'_> ⓘ
pub fn paths(&self) -> PathsIter<'_> ⓘ
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 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.
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 FsTree.
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 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(self) -> Self
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.
sourcepub fn to_directory(self, children: Vec<Self>) -> Self
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.
sourcepub fn to_symlink(self, target_path: impl Into<PathBuf>) -> Self
pub fn to_symlink(self, target_path: impl Into<PathBuf>) -> Self
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: &FsTree) -> bool
pub fn has_same_type_as(&self, other: &FsTree) -> bool
Checks if the FsTree file type is the same as other FsTree.
sourcepub fn create_at(&self, folder: impl AsRef<Path>) -> Result<()>
pub fn create_at(&self, folder: impl AsRef<Path>) -> Result<()>
Create the tree folder structure in the path
sourcepub fn create(&self) -> Result<()>
pub fn create(&self) -> Result<()>
Create FsTree
in the current directory.
Alias to self.create_at(".")
.
sourcepub fn get(&self, path: impl AsRef<Path>) -> Option<&FsTree>
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 Ord for FsTree
impl Ord for FsTree
source§impl PartialEq<FsTree> for FsTree
impl PartialEq<FsTree> for FsTree
source§impl PartialOrd<FsTree> for FsTree
impl PartialOrd<FsTree> for FsTree
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more