[][src]Struct file_tree::FileTree

pub struct FileTree { /* fields omitted */ }

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.

Methods

impl FileTree[src]

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

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

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

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

pub fn from_existing(path: PathBuf) -> FileTree[src]

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");

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

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

pub fn get_root(&self) -> PathBuf[src]

Return the root path for the file tree

Auto Trait Implementations

Blanket Implementations

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]