pub enum FsTree {
Regular,
Directory(TrieMap),
Symlink(PathBuf),
}
Expand description
Variants§
Regular
A regular file.
Directory(TrieMap)
A directory, might have children FsTree
s inside.
Symlink(PathBuf)
Symbolic link, and it’s target path (the link might be broken).
Implementations§
Source§impl FsTree
impl FsTree
Sourcepub fn new_dir() -> Self
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);
Sourcepub fn len_all(&self) -> usize
pub fn len_all(&self) -> usize
Calculate the length by counting all tree nodes, including the root.
Sourcepub fn read_at(path: impl AsRef<Path>) -> Result<Self>
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.
Sourcepub fn symlink_read_at(path: impl AsRef<Path>) -> Result<Self>
pub fn symlink_read_at(path: impl AsRef<Path>) -> Result<Self>
Sourcepub fn read_structure_at(&self, path: impl AsRef<Path>) -> Result<Self>
pub fn read_structure_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_structure_at
.
§Examples:
use fs_tree::FsTree;
fn dynamically_load_structure() -> FsTree {
...
}
let structure = dynamically_load_structure();
let new_tree = structure.read_structure_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:
- If an IO error happens, except
io::ErrorKind::NotFound
Sourcepub fn symlink_read_structure_at(&self, path: impl AsRef<Path>) -> Result<Self>
pub fn symlink_read_structure_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::read_structure_at
.
§Examples:
use fs_tree::FsTree;
fn dynamically_load_structure() -> FsTree {
...
}
let structure = dynamically_load_structure();
let new_tree = structure.symlink_read_structure_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:
- If an IO error happens, except
io::ErrorKind::NotFound
Sourcepub fn from_path_text(path: impl AsRef<Path>) -> Self
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.
For my usage cases it’s OK, but open an issue if you think otherwise 👍.
§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());
Sourcepub fn from_path_pieces<I, P>(path_iter: I) -> Self
pub fn from_path_pieces<I, P>(path_iter: I) -> Self
Generic iterator version of from_path_text
.
Sourcepub fn iter(&self) -> Iter<'_> ⓘ
pub fn iter(&self) -> Iter<'_> ⓘ
Creates an iterator that yields (&FsTree, PathBuf)
.
See iterator docs at the iter
module documentation.
Sourcepub fn nodes(&self) -> NodesIter<'_> ⓘ
pub fn nodes(&self) -> NodesIter<'_> ⓘ
Creates an iterator that yields &FsTree
.
See iterator docs at the iter
module documentation.
Sourcepub fn paths(&self) -> PathsIter<'_> ⓘ
pub fn paths(&self) -> PathsIter<'_> ⓘ
Creates an iterator that yields PathBuf
.
See iterator docs at the iter
module documentation.
Sourcepub fn is_same_type_as(&self, other: &Self) -> bool
pub fn is_same_type_as(&self, other: &Self) -> bool
Returns true
if self
type matches other
type.
Sourcepub fn try_exists(&mut self) -> Result<bool>
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
).
Sourcepub fn merge(self, other: Self) -> Self
pub fn merge(self, other: Self) -> Self
Merge two trees.
When conflicts happen, entries from self
are kept, and the other
’s are discarded.
Sourcepub fn conflicts_with(&self, other: &Self) -> bool
pub fn conflicts_with(&self, other: &Self) -> bool
Checks for conflicts in case the two trees would be merged.
Also see Self::merge
.
Sourcepub fn children_mut(&mut self) -> Option<&mut TrieMap>
pub fn children_mut(&mut self) -> Option<&mut TrieMap>
Mutable reference to children if self.is_directory()
.
Sourcepub fn target_mut(&mut self) -> Option<&mut PathBuf>
pub fn target_mut(&mut self) -> Option<&mut PathBuf>
Mutable reference to target path, if self.is_symlink()
.
Sourcepub fn is_leaf(&self) -> bool
pub fn is_leaf(&self) -> bool
Returns true
if self
is a leaf node.
A leaf node might be of any type, including directory, however, a non-leaf node is always a directory.
Sourcepub fn variant_str(&self) -> &'static str
pub fn variant_str(&self) -> &'static str
The variant string, useful for showing to user.
Sourcepub fn is_regular(&self) -> bool
pub fn is_regular(&self) -> bool
Returns true
if self matches the FsTree::Regular
variant.
Sourcepub fn is_dir(&self) -> bool
pub fn is_dir(&self) -> bool
Returns true
if self matches the FsTree::Directory
variant.
Sourcepub fn is_symlink(&self) -> bool
pub fn is_symlink(&self) -> bool
Returns true
if self matches the FsTree::Symlink
variant.
Sourcepub fn write_at(&self, folder: impl AsRef<Path>) -> Result<()>
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.
Sourcepub fn get(&self, path: impl AsRef<Path>) -> Option<&Self>
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);
Trait Implementations§
Source§impl Ord for FsTree
impl Ord for FsTree
Source§impl PartialOrd for FsTree
impl PartialOrd for FsTree
impl Eq for FsTree
impl StructuralPartialEq for FsTree
Auto Trait Implementations§
impl Freeze for FsTree
impl RefUnwindSafe for FsTree
impl Send for FsTree
impl Sync for FsTree
impl Unpin for FsTree
impl UnwindSafe for FsTree
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)