builder/
builder.rs

1use std::fs;
2use tree_fs::{Settings, TreeBuilder};
3
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}