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
impl TempDirectoryBuilder
Sourcepub fn root_folder(self, dir: impl AsRef<Path>) -> Self
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().
Sourcepub const fn delete_on_drop(self, delete_on_drop: bool) -> Self
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.
Sourcepub fn add_empty_file<P: AsRef<Path>>(self, path: P) -> Self
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 errorBuildError::EntryOutsideDirectorywill 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}Sourcepub fn add_directory(self, path: impl AsRef<Path>) -> Self
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 errorBuildError::EntryOutsideDirectorywill be returned.
Sourcepub fn add_text_file(self, path: impl AsRef<Path>, text: impl ToString) -> Self
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 errorBuildError::EntryOutsideDirectorywill 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}Sourcepub fn add_binary_file(self, path: impl AsRef<Path>, content: &[u8]) -> Self
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 errorBuildError::EntryOutsideDirectorywill be returned.content- The bytes to be written in the new file created.
Sourcepub fn add_file(self, path: impl AsRef<Path>, file: impl AsRef<Path>) -> Self
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 errorBuildError::EntryOutsideDirectorywill 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}Sourcepub fn build(&self) -> Result<TempDirectory, BuildError>
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
impl Debug for TempDirectoryBuilder
Auto Trait Implementations§
impl Freeze for TempDirectoryBuilder
impl RefUnwindSafe for TempDirectoryBuilder
impl Send for TempDirectoryBuilder
impl Sync for TempDirectoryBuilder
impl Unpin for TempDirectoryBuilder
impl UnwindSafe for TempDirectoryBuilder
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
Mutably borrows from an owned value. Read more