TempDirectoryBuilder

Struct TempDirectoryBuilder 

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

A temporary directory builder that contains a list of entries to be created.

§Examples

use temp_dir_builder::TempDirectoryBuilder;
let temp_dir = TempDirectoryBuilder::default()
    .add_text_file("test/foo.txt", "bar")
    .add_binary_file("test/foo2.txt", &[98u8, 97u8, 114u8])
    .add_empty_file("test/folder-a/folder-b/bar.txt")
    .add_file("test/file.rs", file!())
    .add_directory("test/dir")
    .build()
    .expect("create temp dir");
println!("created successfully in {}", temp_dir.path().display());

Implementations§

Source§

impl TempDirectoryBuilder

Source

pub fn root_folder(self, dir: impl AsRef<Path>) -> Self

Sets the root folder where the tree will be created.
By default this is the temporary directory path returned by std::env::temp_dir().

Source

pub const fn delete_on_drop(self, delete_on_drop: bool) -> Self

Specifies whether to automatically delete the temporary folder when the TempDirectory instance is dropped.
By default this is value is set to true.

Source

pub fn add_empty_file<P: AsRef<Path>>(self, path: P) -> Self

Adds an empty file.

  • path - Path of the file to create. This path must be relative to the created directory. If the path is outside the created directory (e.g: “../foo”) the error BuildError::EntryOutsideDirectory will be returned.
Examples found in repository?
examples/builder.rs (line 6)
3fn main() {
4    let temp_directory = TempDirectoryBuilder::default()
5        .add_text_file("test/foo.txt", "bar")
6        .add_empty_file("test/folder-a/folder-b/bar.txt")
7        .add_file("test_file.rs", file!())
8        .build()
9        .expect("create tree fs");
10
11    println!(
12        "created successfully in {}",
13        temp_directory.path().display()
14    );
15
16    let path = temp_directory.path().to_path_buf();
17
18    assert!(path.exists());
19
20    drop(temp_directory);
21
22    assert!(!path.exists());
23}
Source

pub fn add_directory(self, path: impl AsRef<Path>) -> Self

Adds a directory.

  • path - Path of the directory to create. This path must be relative to the created directory. If the path is outside the created directory (e.g: “../foo”) the error BuildError::EntryOutsideDirectory will be returned.
Source

pub fn add_text_file(self, path: impl AsRef<Path>, text: impl ToString) -> Self

Adds a text file specifying the content.

  • path - Path of the text file to create. This path must be relative to the created directory. If the path is outside the created directory (e.g: “../foo”) the error BuildError::EntryOutsideDirectory will be returned.
  • text - Text to be written in the new file created.
Examples found in repository?
examples/builder.rs (line 5)
3fn main() {
4    let temp_directory = TempDirectoryBuilder::default()
5        .add_text_file("test/foo.txt", "bar")
6        .add_empty_file("test/folder-a/folder-b/bar.txt")
7        .add_file("test_file.rs", file!())
8        .build()
9        .expect("create tree fs");
10
11    println!(
12        "created successfully in {}",
13        temp_directory.path().display()
14    );
15
16    let path = temp_directory.path().to_path_buf();
17
18    assert!(path.exists());
19
20    drop(temp_directory);
21
22    assert!(!path.exists());
23}
Source

pub fn add_binary_file(self, path: impl AsRef<Path>, content: &[u8]) -> Self

Adds a binary file specifying the content.

  • path - Path of the binary file to create. This path must be relative to the created directory. If the path is outside the created directory (e.g: “../foo”) the error BuildError::EntryOutsideDirectory will be returned.
  • content - The bytes to be written in the new file created.
Source

pub fn add_file(self, path: impl AsRef<Path>, file: impl AsRef<Path>) -> Self

Adds a file specifying a source file to be copied.

  • path - Path of the file to create. This path must be relative to the created directory. If the path is outside the created directory (e.g: “../foo”) the error BuildError::EntryOutsideDirectory will be returned.
  • file - Path of the file to be copied. This path must be absolute.
Examples found in repository?
examples/builder.rs (line 7)
3fn main() {
4    let temp_directory = TempDirectoryBuilder::default()
5        .add_text_file("test/foo.txt", "bar")
6        .add_empty_file("test/folder-a/folder-b/bar.txt")
7        .add_file("test_file.rs", file!())
8        .build()
9        .expect("create tree fs");
10
11    println!(
12        "created successfully in {}",
13        temp_directory.path().display()
14    );
15
16    let path = temp_directory.path().to_path_buf();
17
18    assert!(path.exists());
19
20    drop(temp_directory);
21
22    assert!(!path.exists());
23}
Source

pub fn build(&self) -> Result<TempDirectory, BuildError>

Builds the file tree by generating files and directories based on the list of Entrys.

§Errors

A BuildError is returned in case of error.

Examples found in repository?
examples/builder.rs (line 8)
3fn main() {
4    let temp_directory = TempDirectoryBuilder::default()
5        .add_text_file("test/foo.txt", "bar")
6        .add_empty_file("test/folder-a/folder-b/bar.txt")
7        .add_file("test_file.rs", file!())
8        .build()
9        .expect("create tree fs");
10
11    println!(
12        "created successfully in {}",
13        temp_directory.path().display()
14    );
15
16    let path = temp_directory.path().to_path_buf();
17
18    assert!(path.exists());
19
20    drop(temp_directory);
21
22    assert!(!path.exists());
23}

Trait Implementations§

Source§

impl Debug for TempDirectoryBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for TempDirectoryBuilder

Source§

fn default() -> Self

Creates a default TempDirectoryBuilder instance with an empty file list,

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V