pub struct FileStructure { /* private fields */ }
Expand description
A struct which represents a (sub)-filestructure.
Implementations§
Source§impl FileStructure
impl FileStructure
Sourcepub fn from_path(path: &str) -> Self
pub fn from_path(path: &str) -> Self
Constructs a FileStructure
completely from a unix path.
If the path contains a starting /
, will return a structure with a root node. If not, the root will be the first name of the path.
§Example
use filestructure::{FileStructure, FileType};
let s = "/some/path";
let fs = FileStructure::from_path(s);
let expected = FileStructure::new("".to_string(),
FileType::Dir(vec![
FileStructure::new("some".to_string(),
FileType::Dir(vec![
FileStructure::new("path".to_string(),
FileType::Dir(vec![])
)
])
)
])
);
assert_eq!(fs, expected);
Sourcepub fn write_to_disk(&self, root: &Path) -> Result<()>
pub fn write_to_disk(&self, root: &Path) -> Result<()>
Given a full FileStructure
and a starting path, attempts to write it all to disk.
Directories are handled recursively. Everything else is converted to bytes and written to a specific file.
§Errors
Errors whenever we fail to either write to a file or create a dir. See the
documentation for File::create
, Write::write_all
and create_dir
for more
info.
Sourcepub fn get(&self, path: &str) -> Option<&Self>
pub fn get(&self, path: &str) -> Option<&Self>
Tries finding a file in the structure by its full path.
Path should be written unix style.
Sourcepub fn get_mut(&mut self, path: &str) -> Option<&mut Self>
pub fn get_mut(&mut self, path: &str) -> Option<&mut Self>
Tries finding a file in the structure by its full path, mutably
Path should be written unix style.
Sourcepub fn insert_dir(&mut self, path: &str) -> Option<&mut Self>
pub fn insert_dir(&mut self, path: &str) -> Option<&mut Self>
Inserts a directory into the FileStructure
. Returns a None
if we fail, returns a mutable reference to the bottom directory if we succeed.
Functions like mkdir -p
, meaning that it will automatically create directories as needed until the full path has been added.
Sourcepub fn insert_blob(&mut self, path: &str, blob: Box<[u8]>) -> Option<&mut Self>
pub fn insert_blob(&mut self, path: &str, blob: Box<[u8]>) -> Option<&mut Self>
Insert binary blob data at some path relative to the root FileStructure
.
Will automatically create directories if needed.
Sourcepub fn insert_string(&mut self, path: &str, data: String) -> Option<&mut Self>
pub fn insert_string(&mut self, path: &str, data: String) -> Option<&mut Self>
Insert a String
at some path relative to the root FileStructure
.
Will automatically create directories if needed.
Sourcepub fn insert(&mut self, child: Self) -> Option<&mut Self>
pub fn insert(&mut self, child: Self) -> Option<&mut Self>
Insert a FileStructure
as a child to this FileStructure
.
Returns a None
if this FileStructure
is not a directory. Returns a mutable reference to the child otherwise.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Get the “len” (file count) of the structure.
TODO: Precalculate this instead of exploring the whole tree.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
A filestructure is deemed empty if there are 0 and 0 subdirs in it.
Sourcepub fn iter(&self) -> FileIterator<'_> ⓘ
pub fn iter(&self) -> FileIterator<'_> ⓘ
Convert the FileStructure
into a usable iterator.
This will iterate over all the nodes in a DFS style.