Struct TreeBuilder

Source
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

Source

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?
examples/builder.rs (line 30)
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}
Source

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?
examples/drop.rs (line 7)
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
Hide additional examples
examples/builder.rs (line 43)
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}
Source

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?
examples/builder.rs (line 42)
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}
Source

pub fn add<P: AsRef<Path>>(self, path: P, content: &str) -> Self

Adds a file with content to the tree.

Source

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?
examples/drop.rs (line 5)
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
Hide additional examples
examples/builder.rs (line 9)
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}
Source

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

Adds a file with a empty content.

Source

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

Adds a file with a empty content.

Examples found in repository?
examples/drop.rs (line 6)
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
Hide additional examples
examples/builder.rs (line 32)
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}
Source

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

Adds a directory to the tree.

Examples found in repository?
examples/builder.rs (line 33)
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}
Source

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?
examples/builder.rs (lines 36-40)
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}
Source

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.

Source

pub fn add_directory_with_settings<P: AsRef<Path>>( self, path: P, settings: Settings, ) -> Self

Adds a directory with custom settings to the tree.

Source

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?
examples/builder.rs (line 41)
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}
Source

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

Convenience method for adding a read-only empty file.

Source

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?
examples/drop.rs (line 8)
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
Hide additional examples
examples/builder.rs (line 10)
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}

Trait Implementations§

Source§

impl Debug for TreeBuilder

Source§

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

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

impl Default for TreeBuilder

Source§

fn default() -> Self

Creates a default Tree instance with an empty file list,

Source§

impl<'de> Deserialize<'de> for TreeBuilder

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more

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

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,