pub struct TreeBuilder {
pub root: PathBuf,
/* private fields */
}
Expand description
Represents a file tree structure
§Examples
use tree_fs::{TreeBuilder, Settings};
let tree = TreeBuilder::default()
.add_file("config/app.conf", "host = localhost")
.add_empty_file("logs/app.log")
.add_directory("data/raw")
.add_file_with_settings(
"secrets/api.key",
"supersecretkey",
Settings::new().readonly(true)
)
.create()
.expect("create tree fs");
println!("Created a complex tree in: {}", tree.root.display());
// You can verify the readonly status (this requires std::fs)
// let key_path = tree.root.join("secrets/api.key");
// let metadata = std::fs::metadata(key_path).unwrap();
// assert!(metadata.permissions().readonly());
use tree_fs::TreeBuilder;
let tree = TreeBuilder::default()
.add_file("temp_data/file.txt", "temporary content")
.add_empty_file("temp_data/another.tmp")
.drop(true) // This is the default, but explicitly shown here
.create()
.expect("create tree fs for drop example");
println!("Temporary tree created at: {}", tree.root.display());
let path_to_check = tree.root.clone();
assert!(path_to_check.exists(), "Directory should exist before drop");
drop(tree); // tree_fs instance goes out of scope
assert!(!path_to_check.exists(), "Directory should be deleted after drop");
Fields§
§root: PathBuf
Root folder where the tree will be created.
Implementations§
Source§impl TreeBuilder
impl TreeBuilder
Sourcepub fn root_folder<P: AsRef<Path>>(self, dir: P) -> Self
pub fn root_folder<P: AsRef<Path>>(self, dir: P) -> Self
Sets the root folder where the tree will be created.
Examples found in repository?
4fn main() {
5 println!("--- Comprehensive TreeBuilder Example ---");
6
7 // 1. Default behavior: creates in a temporary directory, auto-drops
8 let default_tree = TreeBuilder::default()
9 .add_file("default_file.txt", "Content in default temp dir")
10 .create()
11 .expect("Failed to create default tree");
12 println!("Default tree created in: {}", default_tree.root.display());
13 let default_path = default_tree.root.clone();
14 assert!(default_path.exists());
15 drop(default_tree);
16 assert!(
17 !default_path.exists(),
18 "Default tree should be auto-deleted."
19 );
20 println!("Default tree auto-deleted successfully.");
21
22 // 2. Custom root, various file types, and settings
23 let custom_root_path = std::env::temp_dir().join("tree_fs_custom_example");
24 // Clean up previous run if any, for idempotency of example
25 if custom_root_path.exists() {
26 let _ = fs::remove_dir_all(&custom_root_path);
27 }
28
29 let complex_tree = TreeBuilder::default()
30 .root_folder(&custom_root_path) // Custom root
31 .add_file("project/README.md", "# My Project")
32 .add_empty_file("project/.gitignore")
33 .add_directory("project/src")
34 .add_file("project/src/main.rs", "fn main() { println!(\"Hello!\"); }")
35 .add_directory("project/config")
36 .add_file_with_settings(
37 "project/config/prod.json",
38 "{ \"api_key\": \"prod_secret\" }",
39 Settings::new().readonly(true), // Read-only setting
40 )
41 .add_readonly_file("project/config/default.json", "{ \"timeout\": 5000 }")
42 .override_file(true) // Allow overwriting if files exist (e.g. from previous run if not cleaned)
43 .drop(false) // Do not auto-delete this tree
44 .create()
45 .expect("Failed to create complex tree");
46
47 println!("Complex tree created at: {}", complex_tree.root.display());
48 println!(" (This tree will NOT be auto-deleted)");
49
50 // Verify read-only status
51 let readonly_config_path = complex_tree.root.join("project/config/prod.json");
52 match fs::metadata(&readonly_config_path) {
53 Ok(metadata) => {
54 assert!(
55 metadata.permissions().readonly(),
56 "prod.json should be read-only"
57 );
58 println!("Verified: {} is read-only.", readonly_config_path.display());
59 }
60 Err(e) => eprintln!(
61 "Could not get metadata for {}: {}",
62 readonly_config_path.display(),
63 e
64 ),
65 }
66
67 // Verify another readonly file
68 let default_config_path = complex_tree.root.join("project/config/default.json");
69 match fs::metadata(&default_config_path) {
70 Ok(metadata) => {
71 assert!(
72 metadata.permissions().readonly(),
73 "default.json should be read-only"
74 );
75 println!("Verified: {} is read-only.", default_config_path.display());
76 }
77 Err(e) => eprintln!(
78 "Could not get metadata for {}: {}",
79 default_config_path.display(),
80 e
81 ),
82 }
83
84 println!(
85 "Example finished. To clean up, manually delete: {}",
86 custom_root_path.display()
87 );
88}
Sourcepub const fn drop(self, yes: bool) -> Self
pub const fn drop(self, yes: bool) -> Self
Sets the drop
flag, indicating whether to automatically delete the temporary folder when the tree_fs
instance is dropped
Examples found in repository?
3fn main() {
4 let tree = TreeBuilder::default()
5 .add_file("temp_data/file.txt", "temporary content")
6 .add_empty_file("temp_data/another.tmp")
7 .drop(true)
8 .create()
9 .expect("create tree fs for drop example");
10
11 println!("Temporary tree created at: {}", tree.root.display());
12
13 let path_to_check = tree.root.clone();
14 assert!(path_to_check.exists(), "Directory should exist before drop");
15
16 drop(tree);
17 assert!(
18 !path_to_check.exists(),
19 "Directory should be deleted after drop"
20 );
21 println!("Drop example: Temporary tree auto-deleted successfully.");
22}
More examples
4fn main() {
5 println!("--- Comprehensive TreeBuilder Example ---");
6
7 // 1. Default behavior: creates in a temporary directory, auto-drops
8 let default_tree = TreeBuilder::default()
9 .add_file("default_file.txt", "Content in default temp dir")
10 .create()
11 .expect("Failed to create default tree");
12 println!("Default tree created in: {}", default_tree.root.display());
13 let default_path = default_tree.root.clone();
14 assert!(default_path.exists());
15 drop(default_tree);
16 assert!(
17 !default_path.exists(),
18 "Default tree should be auto-deleted."
19 );
20 println!("Default tree auto-deleted successfully.");
21
22 // 2. Custom root, various file types, and settings
23 let custom_root_path = std::env::temp_dir().join("tree_fs_custom_example");
24 // Clean up previous run if any, for idempotency of example
25 if custom_root_path.exists() {
26 let _ = fs::remove_dir_all(&custom_root_path);
27 }
28
29 let complex_tree = TreeBuilder::default()
30 .root_folder(&custom_root_path) // Custom root
31 .add_file("project/README.md", "# My Project")
32 .add_empty_file("project/.gitignore")
33 .add_directory("project/src")
34 .add_file("project/src/main.rs", "fn main() { println!(\"Hello!\"); }")
35 .add_directory("project/config")
36 .add_file_with_settings(
37 "project/config/prod.json",
38 "{ \"api_key\": \"prod_secret\" }",
39 Settings::new().readonly(true), // Read-only setting
40 )
41 .add_readonly_file("project/config/default.json", "{ \"timeout\": 5000 }")
42 .override_file(true) // Allow overwriting if files exist (e.g. from previous run if not cleaned)
43 .drop(false) // Do not auto-delete this tree
44 .create()
45 .expect("Failed to create complex tree");
46
47 println!("Complex tree created at: {}", complex_tree.root.display());
48 println!(" (This tree will NOT be auto-deleted)");
49
50 // Verify read-only status
51 let readonly_config_path = complex_tree.root.join("project/config/prod.json");
52 match fs::metadata(&readonly_config_path) {
53 Ok(metadata) => {
54 assert!(
55 metadata.permissions().readonly(),
56 "prod.json should be read-only"
57 );
58 println!("Verified: {} is read-only.", readonly_config_path.display());
59 }
60 Err(e) => eprintln!(
61 "Could not get metadata for {}: {}",
62 readonly_config_path.display(),
63 e
64 ),
65 }
66
67 // Verify another readonly file
68 let default_config_path = complex_tree.root.join("project/config/default.json");
69 match fs::metadata(&default_config_path) {
70 Ok(metadata) => {
71 assert!(
72 metadata.permissions().readonly(),
73 "default.json should be read-only"
74 );
75 println!("Verified: {} is read-only.", default_config_path.display());
76 }
77 Err(e) => eprintln!(
78 "Could not get metadata for {}: {}",
79 default_config_path.display(),
80 e
81 ),
82 }
83
84 println!(
85 "Example finished. To clean up, manually delete: {}",
86 custom_root_path.display()
87 );
88}
Sourcepub const fn override_file(self, yes: bool) -> Self
pub const fn override_file(self, yes: bool) -> Self
Sets the override_file
flag, indicating whether existing files should be overridden.
Examples found in repository?
4fn main() {
5 println!("--- Comprehensive TreeBuilder Example ---");
6
7 // 1. Default behavior: creates in a temporary directory, auto-drops
8 let default_tree = TreeBuilder::default()
9 .add_file("default_file.txt", "Content in default temp dir")
10 .create()
11 .expect("Failed to create default tree");
12 println!("Default tree created in: {}", default_tree.root.display());
13 let default_path = default_tree.root.clone();
14 assert!(default_path.exists());
15 drop(default_tree);
16 assert!(
17 !default_path.exists(),
18 "Default tree should be auto-deleted."
19 );
20 println!("Default tree auto-deleted successfully.");
21
22 // 2. Custom root, various file types, and settings
23 let custom_root_path = std::env::temp_dir().join("tree_fs_custom_example");
24 // Clean up previous run if any, for idempotency of example
25 if custom_root_path.exists() {
26 let _ = fs::remove_dir_all(&custom_root_path);
27 }
28
29 let complex_tree = TreeBuilder::default()
30 .root_folder(&custom_root_path) // Custom root
31 .add_file("project/README.md", "# My Project")
32 .add_empty_file("project/.gitignore")
33 .add_directory("project/src")
34 .add_file("project/src/main.rs", "fn main() { println!(\"Hello!\"); }")
35 .add_directory("project/config")
36 .add_file_with_settings(
37 "project/config/prod.json",
38 "{ \"api_key\": \"prod_secret\" }",
39 Settings::new().readonly(true), // Read-only setting
40 )
41 .add_readonly_file("project/config/default.json", "{ \"timeout\": 5000 }")
42 .override_file(true) // Allow overwriting if files exist (e.g. from previous run if not cleaned)
43 .drop(false) // Do not auto-delete this tree
44 .create()
45 .expect("Failed to create complex tree");
46
47 println!("Complex tree created at: {}", complex_tree.root.display());
48 println!(" (This tree will NOT be auto-deleted)");
49
50 // Verify read-only status
51 let readonly_config_path = complex_tree.root.join("project/config/prod.json");
52 match fs::metadata(&readonly_config_path) {
53 Ok(metadata) => {
54 assert!(
55 metadata.permissions().readonly(),
56 "prod.json should be read-only"
57 );
58 println!("Verified: {} is read-only.", readonly_config_path.display());
59 }
60 Err(e) => eprintln!(
61 "Could not get metadata for {}: {}",
62 readonly_config_path.display(),
63 e
64 ),
65 }
66
67 // Verify another readonly file
68 let default_config_path = complex_tree.root.join("project/config/default.json");
69 match fs::metadata(&default_config_path) {
70 Ok(metadata) => {
71 assert!(
72 metadata.permissions().readonly(),
73 "default.json should be read-only"
74 );
75 println!("Verified: {} is read-only.", default_config_path.display());
76 }
77 Err(e) => eprintln!(
78 "Could not get metadata for {}: {}",
79 default_config_path.display(),
80 e
81 ),
82 }
83
84 println!(
85 "Example finished. To clean up, manually delete: {}",
86 custom_root_path.display()
87 );
88}
Sourcepub fn add<P: AsRef<Path>>(self, path: P, content: &str) -> Self
pub fn add<P: AsRef<Path>>(self, path: P, content: &str) -> Self
Adds a file with content to the tree.
Sourcepub fn add_file<P: AsRef<Path>>(self, path: P, content: &str) -> Self
pub fn add_file<P: AsRef<Path>>(self, path: P, content: &str) -> Self
Adds a file with content to the tree.
This is an alias for add
.
Examples found in repository?
3fn main() {
4 let tree = TreeBuilder::default()
5 .add_file("temp_data/file.txt", "temporary content")
6 .add_empty_file("temp_data/another.tmp")
7 .drop(true)
8 .create()
9 .expect("create tree fs for drop example");
10
11 println!("Temporary tree created at: {}", tree.root.display());
12
13 let path_to_check = tree.root.clone();
14 assert!(path_to_check.exists(), "Directory should exist before drop");
15
16 drop(tree);
17 assert!(
18 !path_to_check.exists(),
19 "Directory should be deleted after drop"
20 );
21 println!("Drop example: Temporary tree auto-deleted successfully.");
22}
More examples
4fn main() {
5 println!("--- Comprehensive TreeBuilder Example ---");
6
7 // 1. Default behavior: creates in a temporary directory, auto-drops
8 let default_tree = TreeBuilder::default()
9 .add_file("default_file.txt", "Content in default temp dir")
10 .create()
11 .expect("Failed to create default tree");
12 println!("Default tree created in: {}", default_tree.root.display());
13 let default_path = default_tree.root.clone();
14 assert!(default_path.exists());
15 drop(default_tree);
16 assert!(
17 !default_path.exists(),
18 "Default tree should be auto-deleted."
19 );
20 println!("Default tree auto-deleted successfully.");
21
22 // 2. Custom root, various file types, and settings
23 let custom_root_path = std::env::temp_dir().join("tree_fs_custom_example");
24 // Clean up previous run if any, for idempotency of example
25 if custom_root_path.exists() {
26 let _ = fs::remove_dir_all(&custom_root_path);
27 }
28
29 let complex_tree = TreeBuilder::default()
30 .root_folder(&custom_root_path) // Custom root
31 .add_file("project/README.md", "# My Project")
32 .add_empty_file("project/.gitignore")
33 .add_directory("project/src")
34 .add_file("project/src/main.rs", "fn main() { println!(\"Hello!\"); }")
35 .add_directory("project/config")
36 .add_file_with_settings(
37 "project/config/prod.json",
38 "{ \"api_key\": \"prod_secret\" }",
39 Settings::new().readonly(true), // Read-only setting
40 )
41 .add_readonly_file("project/config/default.json", "{ \"timeout\": 5000 }")
42 .override_file(true) // Allow overwriting if files exist (e.g. from previous run if not cleaned)
43 .drop(false) // Do not auto-delete this tree
44 .create()
45 .expect("Failed to create complex tree");
46
47 println!("Complex tree created at: {}", complex_tree.root.display());
48 println!(" (This tree will NOT be auto-deleted)");
49
50 // Verify read-only status
51 let readonly_config_path = complex_tree.root.join("project/config/prod.json");
52 match fs::metadata(&readonly_config_path) {
53 Ok(metadata) => {
54 assert!(
55 metadata.permissions().readonly(),
56 "prod.json should be read-only"
57 );
58 println!("Verified: {} is read-only.", readonly_config_path.display());
59 }
60 Err(e) => eprintln!(
61 "Could not get metadata for {}: {}",
62 readonly_config_path.display(),
63 e
64 ),
65 }
66
67 // Verify another readonly file
68 let default_config_path = complex_tree.root.join("project/config/default.json");
69 match fs::metadata(&default_config_path) {
70 Ok(metadata) => {
71 assert!(
72 metadata.permissions().readonly(),
73 "default.json should be read-only"
74 );
75 println!("Verified: {} is read-only.", default_config_path.display());
76 }
77 Err(e) => eprintln!(
78 "Could not get metadata for {}: {}",
79 default_config_path.display(),
80 e
81 ),
82 }
83
84 println!(
85 "Example finished. To clean up, manually delete: {}",
86 custom_root_path.display()
87 );
88}
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 a file with a empty content.
Examples found in repository?
3fn main() {
4 let tree = TreeBuilder::default()
5 .add_file("temp_data/file.txt", "temporary content")
6 .add_empty_file("temp_data/another.tmp")
7 .drop(true)
8 .create()
9 .expect("create tree fs for drop example");
10
11 println!("Temporary tree created at: {}", tree.root.display());
12
13 let path_to_check = tree.root.clone();
14 assert!(path_to_check.exists(), "Directory should exist before drop");
15
16 drop(tree);
17 assert!(
18 !path_to_check.exists(),
19 "Directory should be deleted after drop"
20 );
21 println!("Drop example: Temporary tree auto-deleted successfully.");
22}
More examples
4fn main() {
5 println!("--- Comprehensive TreeBuilder Example ---");
6
7 // 1. Default behavior: creates in a temporary directory, auto-drops
8 let default_tree = TreeBuilder::default()
9 .add_file("default_file.txt", "Content in default temp dir")
10 .create()
11 .expect("Failed to create default tree");
12 println!("Default tree created in: {}", default_tree.root.display());
13 let default_path = default_tree.root.clone();
14 assert!(default_path.exists());
15 drop(default_tree);
16 assert!(
17 !default_path.exists(),
18 "Default tree should be auto-deleted."
19 );
20 println!("Default tree auto-deleted successfully.");
21
22 // 2. Custom root, various file types, and settings
23 let custom_root_path = std::env::temp_dir().join("tree_fs_custom_example");
24 // Clean up previous run if any, for idempotency of example
25 if custom_root_path.exists() {
26 let _ = fs::remove_dir_all(&custom_root_path);
27 }
28
29 let complex_tree = TreeBuilder::default()
30 .root_folder(&custom_root_path) // Custom root
31 .add_file("project/README.md", "# My Project")
32 .add_empty_file("project/.gitignore")
33 .add_directory("project/src")
34 .add_file("project/src/main.rs", "fn main() { println!(\"Hello!\"); }")
35 .add_directory("project/config")
36 .add_file_with_settings(
37 "project/config/prod.json",
38 "{ \"api_key\": \"prod_secret\" }",
39 Settings::new().readonly(true), // Read-only setting
40 )
41 .add_readonly_file("project/config/default.json", "{ \"timeout\": 5000 }")
42 .override_file(true) // Allow overwriting if files exist (e.g. from previous run if not cleaned)
43 .drop(false) // Do not auto-delete this tree
44 .create()
45 .expect("Failed to create complex tree");
46
47 println!("Complex tree created at: {}", complex_tree.root.display());
48 println!(" (This tree will NOT be auto-deleted)");
49
50 // Verify read-only status
51 let readonly_config_path = complex_tree.root.join("project/config/prod.json");
52 match fs::metadata(&readonly_config_path) {
53 Ok(metadata) => {
54 assert!(
55 metadata.permissions().readonly(),
56 "prod.json should be read-only"
57 );
58 println!("Verified: {} is read-only.", readonly_config_path.display());
59 }
60 Err(e) => eprintln!(
61 "Could not get metadata for {}: {}",
62 readonly_config_path.display(),
63 e
64 ),
65 }
66
67 // Verify another readonly file
68 let default_config_path = complex_tree.root.join("project/config/default.json");
69 match fs::metadata(&default_config_path) {
70 Ok(metadata) => {
71 assert!(
72 metadata.permissions().readonly(),
73 "default.json should be read-only"
74 );
75 println!("Verified: {} is read-only.", default_config_path.display());
76 }
77 Err(e) => eprintln!(
78 "Could not get metadata for {}: {}",
79 default_config_path.display(),
80 e
81 ),
82 }
83
84 println!(
85 "Example finished. To clean up, manually delete: {}",
86 custom_root_path.display()
87 );
88}
Sourcepub fn add_directory<P: AsRef<Path>>(self, path: P) -> Self
pub fn add_directory<P: AsRef<Path>>(self, path: P) -> Self
Adds a directory to the tree.
Examples found in repository?
4fn main() {
5 println!("--- Comprehensive TreeBuilder Example ---");
6
7 // 1. Default behavior: creates in a temporary directory, auto-drops
8 let default_tree = TreeBuilder::default()
9 .add_file("default_file.txt", "Content in default temp dir")
10 .create()
11 .expect("Failed to create default tree");
12 println!("Default tree created in: {}", default_tree.root.display());
13 let default_path = default_tree.root.clone();
14 assert!(default_path.exists());
15 drop(default_tree);
16 assert!(
17 !default_path.exists(),
18 "Default tree should be auto-deleted."
19 );
20 println!("Default tree auto-deleted successfully.");
21
22 // 2. Custom root, various file types, and settings
23 let custom_root_path = std::env::temp_dir().join("tree_fs_custom_example");
24 // Clean up previous run if any, for idempotency of example
25 if custom_root_path.exists() {
26 let _ = fs::remove_dir_all(&custom_root_path);
27 }
28
29 let complex_tree = TreeBuilder::default()
30 .root_folder(&custom_root_path) // Custom root
31 .add_file("project/README.md", "# My Project")
32 .add_empty_file("project/.gitignore")
33 .add_directory("project/src")
34 .add_file("project/src/main.rs", "fn main() { println!(\"Hello!\"); }")
35 .add_directory("project/config")
36 .add_file_with_settings(
37 "project/config/prod.json",
38 "{ \"api_key\": \"prod_secret\" }",
39 Settings::new().readonly(true), // Read-only setting
40 )
41 .add_readonly_file("project/config/default.json", "{ \"timeout\": 5000 }")
42 .override_file(true) // Allow overwriting if files exist (e.g. from previous run if not cleaned)
43 .drop(false) // Do not auto-delete this tree
44 .create()
45 .expect("Failed to create complex tree");
46
47 println!("Complex tree created at: {}", complex_tree.root.display());
48 println!(" (This tree will NOT be auto-deleted)");
49
50 // Verify read-only status
51 let readonly_config_path = complex_tree.root.join("project/config/prod.json");
52 match fs::metadata(&readonly_config_path) {
53 Ok(metadata) => {
54 assert!(
55 metadata.permissions().readonly(),
56 "prod.json should be read-only"
57 );
58 println!("Verified: {} is read-only.", readonly_config_path.display());
59 }
60 Err(e) => eprintln!(
61 "Could not get metadata for {}: {}",
62 readonly_config_path.display(),
63 e
64 ),
65 }
66
67 // Verify another readonly file
68 let default_config_path = complex_tree.root.join("project/config/default.json");
69 match fs::metadata(&default_config_path) {
70 Ok(metadata) => {
71 assert!(
72 metadata.permissions().readonly(),
73 "default.json should be read-only"
74 );
75 println!("Verified: {} is read-only.", default_config_path.display());
76 }
77 Err(e) => eprintln!(
78 "Could not get metadata for {}: {}",
79 default_config_path.display(),
80 e
81 ),
82 }
83
84 println!(
85 "Example finished. To clean up, manually delete: {}",
86 custom_root_path.display()
87 );
88}
Sourcepub fn add_file_with_settings<P: AsRef<Path>>(
self,
path: P,
content: &str,
settings: Settings,
) -> Self
pub fn add_file_with_settings<P: AsRef<Path>>( self, path: P, content: &str, settings: Settings, ) -> Self
Adds a file with content and custom settings to the tree.
Examples found in repository?
4fn main() {
5 println!("--- Comprehensive TreeBuilder Example ---");
6
7 // 1. Default behavior: creates in a temporary directory, auto-drops
8 let default_tree = TreeBuilder::default()
9 .add_file("default_file.txt", "Content in default temp dir")
10 .create()
11 .expect("Failed to create default tree");
12 println!("Default tree created in: {}", default_tree.root.display());
13 let default_path = default_tree.root.clone();
14 assert!(default_path.exists());
15 drop(default_tree);
16 assert!(
17 !default_path.exists(),
18 "Default tree should be auto-deleted."
19 );
20 println!("Default tree auto-deleted successfully.");
21
22 // 2. Custom root, various file types, and settings
23 let custom_root_path = std::env::temp_dir().join("tree_fs_custom_example");
24 // Clean up previous run if any, for idempotency of example
25 if custom_root_path.exists() {
26 let _ = fs::remove_dir_all(&custom_root_path);
27 }
28
29 let complex_tree = TreeBuilder::default()
30 .root_folder(&custom_root_path) // Custom root
31 .add_file("project/README.md", "# My Project")
32 .add_empty_file("project/.gitignore")
33 .add_directory("project/src")
34 .add_file("project/src/main.rs", "fn main() { println!(\"Hello!\"); }")
35 .add_directory("project/config")
36 .add_file_with_settings(
37 "project/config/prod.json",
38 "{ \"api_key\": \"prod_secret\" }",
39 Settings::new().readonly(true), // Read-only setting
40 )
41 .add_readonly_file("project/config/default.json", "{ \"timeout\": 5000 }")
42 .override_file(true) // Allow overwriting if files exist (e.g. from previous run if not cleaned)
43 .drop(false) // Do not auto-delete this tree
44 .create()
45 .expect("Failed to create complex tree");
46
47 println!("Complex tree created at: {}", complex_tree.root.display());
48 println!(" (This tree will NOT be auto-deleted)");
49
50 // Verify read-only status
51 let readonly_config_path = complex_tree.root.join("project/config/prod.json");
52 match fs::metadata(&readonly_config_path) {
53 Ok(metadata) => {
54 assert!(
55 metadata.permissions().readonly(),
56 "prod.json should be read-only"
57 );
58 println!("Verified: {} is read-only.", readonly_config_path.display());
59 }
60 Err(e) => eprintln!(
61 "Could not get metadata for {}: {}",
62 readonly_config_path.display(),
63 e
64 ),
65 }
66
67 // Verify another readonly file
68 let default_config_path = complex_tree.root.join("project/config/default.json");
69 match fs::metadata(&default_config_path) {
70 Ok(metadata) => {
71 assert!(
72 metadata.permissions().readonly(),
73 "default.json should be read-only"
74 );
75 println!("Verified: {} is read-only.", default_config_path.display());
76 }
77 Err(e) => eprintln!(
78 "Could not get metadata for {}: {}",
79 default_config_path.display(),
80 e
81 ),
82 }
83
84 println!(
85 "Example finished. To clean up, manually delete: {}",
86 custom_root_path.display()
87 );
88}
Sourcepub fn add_empty_file_with_settings<P: AsRef<Path>>(
self,
path: P,
settings: Settings,
) -> Self
pub fn add_empty_file_with_settings<P: AsRef<Path>>( self, path: P, settings: Settings, ) -> Self
Adds an empty file with custom settings to the tree.
Sourcepub fn add_directory_with_settings<P: AsRef<Path>>(
self,
path: P,
settings: Settings,
) -> Self
pub fn add_directory_with_settings<P: AsRef<Path>>( self, path: P, settings: Settings, ) -> Self
Adds a directory with custom settings to the tree.
Sourcepub fn add_readonly_file<P: AsRef<Path>>(self, path: P, content: &str) -> Self
pub fn add_readonly_file<P: AsRef<Path>>(self, path: P, content: &str) -> Self
Convenience method for adding a read-only file.
Examples found in repository?
4fn main() {
5 println!("--- Comprehensive TreeBuilder Example ---");
6
7 // 1. Default behavior: creates in a temporary directory, auto-drops
8 let default_tree = TreeBuilder::default()
9 .add_file("default_file.txt", "Content in default temp dir")
10 .create()
11 .expect("Failed to create default tree");
12 println!("Default tree created in: {}", default_tree.root.display());
13 let default_path = default_tree.root.clone();
14 assert!(default_path.exists());
15 drop(default_tree);
16 assert!(
17 !default_path.exists(),
18 "Default tree should be auto-deleted."
19 );
20 println!("Default tree auto-deleted successfully.");
21
22 // 2. Custom root, various file types, and settings
23 let custom_root_path = std::env::temp_dir().join("tree_fs_custom_example");
24 // Clean up previous run if any, for idempotency of example
25 if custom_root_path.exists() {
26 let _ = fs::remove_dir_all(&custom_root_path);
27 }
28
29 let complex_tree = TreeBuilder::default()
30 .root_folder(&custom_root_path) // Custom root
31 .add_file("project/README.md", "# My Project")
32 .add_empty_file("project/.gitignore")
33 .add_directory("project/src")
34 .add_file("project/src/main.rs", "fn main() { println!(\"Hello!\"); }")
35 .add_directory("project/config")
36 .add_file_with_settings(
37 "project/config/prod.json",
38 "{ \"api_key\": \"prod_secret\" }",
39 Settings::new().readonly(true), // Read-only setting
40 )
41 .add_readonly_file("project/config/default.json", "{ \"timeout\": 5000 }")
42 .override_file(true) // Allow overwriting if files exist (e.g. from previous run if not cleaned)
43 .drop(false) // Do not auto-delete this tree
44 .create()
45 .expect("Failed to create complex tree");
46
47 println!("Complex tree created at: {}", complex_tree.root.display());
48 println!(" (This tree will NOT be auto-deleted)");
49
50 // Verify read-only status
51 let readonly_config_path = complex_tree.root.join("project/config/prod.json");
52 match fs::metadata(&readonly_config_path) {
53 Ok(metadata) => {
54 assert!(
55 metadata.permissions().readonly(),
56 "prod.json should be read-only"
57 );
58 println!("Verified: {} is read-only.", readonly_config_path.display());
59 }
60 Err(e) => eprintln!(
61 "Could not get metadata for {}: {}",
62 readonly_config_path.display(),
63 e
64 ),
65 }
66
67 // Verify another readonly file
68 let default_config_path = complex_tree.root.join("project/config/default.json");
69 match fs::metadata(&default_config_path) {
70 Ok(metadata) => {
71 assert!(
72 metadata.permissions().readonly(),
73 "default.json should be read-only"
74 );
75 println!("Verified: {} is read-only.", default_config_path.display());
76 }
77 Err(e) => eprintln!(
78 "Could not get metadata for {}: {}",
79 default_config_path.display(),
80 e
81 ),
82 }
83
84 println!(
85 "Example finished. To clean up, manually delete: {}",
86 custom_root_path.display()
87 );
88}
Sourcepub fn add_readonly_empty_file<P: AsRef<Path>>(self, path: P) -> Self
pub fn add_readonly_empty_file<P: AsRef<Path>>(self, path: P) -> Self
Convenience method for adding a read-only empty file.
Sourcepub fn create(&self) -> Result<Tree>
pub fn create(&self) -> Result<Tree>
Creates the file tree by generating files and directories based on the specified metadata.
§Errors
Returns an std::io::Result
indicating success or failure in creating the file tree.
Examples found in repository?
3fn main() {
4 let tree = TreeBuilder::default()
5 .add_file("temp_data/file.txt", "temporary content")
6 .add_empty_file("temp_data/another.tmp")
7 .drop(true)
8 .create()
9 .expect("create tree fs for drop example");
10
11 println!("Temporary tree created at: {}", tree.root.display());
12
13 let path_to_check = tree.root.clone();
14 assert!(path_to_check.exists(), "Directory should exist before drop");
15
16 drop(tree);
17 assert!(
18 !path_to_check.exists(),
19 "Directory should be deleted after drop"
20 );
21 println!("Drop example: Temporary tree auto-deleted successfully.");
22}
More examples
4fn main() {
5 println!("--- Comprehensive TreeBuilder Example ---");
6
7 // 1. Default behavior: creates in a temporary directory, auto-drops
8 let default_tree = TreeBuilder::default()
9 .add_file("default_file.txt", "Content in default temp dir")
10 .create()
11 .expect("Failed to create default tree");
12 println!("Default tree created in: {}", default_tree.root.display());
13 let default_path = default_tree.root.clone();
14 assert!(default_path.exists());
15 drop(default_tree);
16 assert!(
17 !default_path.exists(),
18 "Default tree should be auto-deleted."
19 );
20 println!("Default tree auto-deleted successfully.");
21
22 // 2. Custom root, various file types, and settings
23 let custom_root_path = std::env::temp_dir().join("tree_fs_custom_example");
24 // Clean up previous run if any, for idempotency of example
25 if custom_root_path.exists() {
26 let _ = fs::remove_dir_all(&custom_root_path);
27 }
28
29 let complex_tree = TreeBuilder::default()
30 .root_folder(&custom_root_path) // Custom root
31 .add_file("project/README.md", "# My Project")
32 .add_empty_file("project/.gitignore")
33 .add_directory("project/src")
34 .add_file("project/src/main.rs", "fn main() { println!(\"Hello!\"); }")
35 .add_directory("project/config")
36 .add_file_with_settings(
37 "project/config/prod.json",
38 "{ \"api_key\": \"prod_secret\" }",
39 Settings::new().readonly(true), // Read-only setting
40 )
41 .add_readonly_file("project/config/default.json", "{ \"timeout\": 5000 }")
42 .override_file(true) // Allow overwriting if files exist (e.g. from previous run if not cleaned)
43 .drop(false) // Do not auto-delete this tree
44 .create()
45 .expect("Failed to create complex tree");
46
47 println!("Complex tree created at: {}", complex_tree.root.display());
48 println!(" (This tree will NOT be auto-deleted)");
49
50 // Verify read-only status
51 let readonly_config_path = complex_tree.root.join("project/config/prod.json");
52 match fs::metadata(&readonly_config_path) {
53 Ok(metadata) => {
54 assert!(
55 metadata.permissions().readonly(),
56 "prod.json should be read-only"
57 );
58 println!("Verified: {} is read-only.", readonly_config_path.display());
59 }
60 Err(e) => eprintln!(
61 "Could not get metadata for {}: {}",
62 readonly_config_path.display(),
63 e
64 ),
65 }
66
67 // Verify another readonly file
68 let default_config_path = complex_tree.root.join("project/config/default.json");
69 match fs::metadata(&default_config_path) {
70 Ok(metadata) => {
71 assert!(
72 metadata.permissions().readonly(),
73 "default.json should be read-only"
74 );
75 println!("Verified: {} is read-only.", default_config_path.display());
76 }
77 Err(e) => eprintln!(
78 "Could not get metadata for {}: {}",
79 default_config_path.display(),
80 e
81 ),
82 }
83
84 println!(
85 "Example finished. To clean up, manually delete: {}",
86 custom_root_path.display()
87 );
88}