Struct FileTree

Source
pub struct FileTree { /* private fields */ }
Expand description

Creates a directory structure suitable for storing large numbers of files. Optionally deletes the created directory and files when dropped.

Slots for new files are allocated using get_new_file(). This struct will create new subdirectories as needed to ensure that no subdirectory contains more than 1,000 files/subdirectories.

Implementations§

Source§

impl FileTree

Source

pub fn new_in(path: PathBuf, persistent: bool) -> Result<FileTree>

Create a new directory structure under path. If persistent is false the directory and all it’s contents will be deleted when the returned FileTree is dropped

§Examples

Create a new temporary data structure and make sure the base path exists

use file_tree::FileTree;
use std::env::temp_dir;

let file_tree = FileTree::new_in(temp_dir(), false).unwrap();
assert!(file_tree.get_root().exists());
§Errors

If persistent is false, the directory will be created using tempdir::TempDir, and any related errors will be returned here

Source

pub fn new(persistent: bool) -> Result<FileTree>

Create a new directory structure. If persistent is false the directory and all it’s contents will be deleted when the returned FileTree is dropped.

§Examples

Create a new temporary data structure and make sure the base path exists

use file_tree::FileTree;

let file_tree = FileTree::new(false).unwrap();
assert!(file_tree.get_root().exists());
§Errors

If persistent is false, the directory will be created using tempdir::TempDir, and any related errors will be returned here

Examples found in repository?
examples/file_tree.rs (line 4)
3fn main() {
4    let mut file_tree = FileTree::new(false).unwrap();
5
6    let writeable_path = file_tree.get_new_file().unwrap();
7    assert_eq!(
8        writeable_path,
9        file_tree.get_root().join("000/000/000/000000000000")
10    );
11
12    let writeable_path_2 = file_tree.get_new_file().unwrap();
13    assert_eq!(
14        writeable_path_2,
15        file_tree.get_root().join("000/000/000/000000000001")
16    );
17}
Source

pub fn from_existing(path: PathBuf) -> FileTree

Creates a FileTree from an existing directory structure. path should be equivalent to the result of calling get_root() on the previous (persistent) FileTree.

§Examples

Re-create a FileTree using an existing file structure

use file_tree::FileTree;
use std::fs::File;

// create a `FileTree` with one file
let mut ft = FileTree::new(true).unwrap();
let file_path = ft.get_new_file().unwrap();
File::create(file_path.clone()).unwrap();
let base = ft.get_root();
drop(ft);

// create a `FileTree` using the existing path, and make sure that the
// files we pull back don't overwrite the existing one
let mut ft2 = FileTree::from_existing(base);
let file2 = ft2.get_new_file().unwrap();
assert_eq!(file_path.file_name().unwrap(), "000000000000");
assert_eq!(file2.file_name().unwrap(), "000000000001");
Source

pub fn get_new_file(&mut self) -> Result<PathBuf>

Returns a PathBuf pointing to an available slot in the file tree. The file pointed to by the returned PathBuf will not be created by this method call, but a new directory will be created if necessary.

This method will ensure that the file pointed to by the returned PathBuf does not exist. If this struct was created using an existing directory structure existing files will be skipped over when generating new file names to return.

File paths are generated such that each new leaf directory (starting with 000/000/000/) will be filled entirely before creating a new directory (next would be 000/000/001/).

§Examples

Retrieve two distinct file paths via get_new_file()

use file_tree::FileTree;

let mut file_tree = FileTree::new(false).unwrap();

let writeable_path = file_tree.get_new_file().unwrap();
assert_eq!(
    writeable_path,
    file_tree.get_root().join("000/000/000/000000000000")
);

let writeable_path_2 = file_tree.get_new_file().unwrap();
assert_eq!(
    writeable_path_2,
    file_tree.get_root().join("000/000/000/000000000001")
);
§Errors

If a new subdirectory is required, fs::create_dir_all will be called. Any errors from that call will be returned here

Examples found in repository?
examples/file_tree.rs (line 6)
3fn main() {
4    let mut file_tree = FileTree::new(false).unwrap();
5
6    let writeable_path = file_tree.get_new_file().unwrap();
7    assert_eq!(
8        writeable_path,
9        file_tree.get_root().join("000/000/000/000000000000")
10    );
11
12    let writeable_path_2 = file_tree.get_new_file().unwrap();
13    assert_eq!(
14        writeable_path_2,
15        file_tree.get_root().join("000/000/000/000000000001")
16    );
17}
Source

pub fn get_root(&self) -> PathBuf

Return the root path for the file tree

Examples found in repository?
examples/file_tree.rs (line 9)
3fn main() {
4    let mut file_tree = FileTree::new(false).unwrap();
5
6    let writeable_path = file_tree.get_new_file().unwrap();
7    assert_eq!(
8        writeable_path,
9        file_tree.get_root().join("000/000/000/000000000000")
10    );
11
12    let writeable_path_2 = file_tree.get_new_file().unwrap();
13    assert_eq!(
14        writeable_path_2,
15        file_tree.get_root().join("000/000/000/000000000001")
16    );
17}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where 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 T
where 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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.