pub fn from_yaml_str(content: &str) -> Result<Tree, Error>Expand description
Creates a file tree based on a YAML-formatted string.
ยงErrors
Returns a Result containing the path to the root folder of the generated file tree on success,
or an error if the operation fails.
Examples found in repository?
examples/yaml-str.rs (line 27)
3fn main() {
4 println!("--- Tree from YAML String Example ---");
5 let comprehensive_yaml = r#"
6 root: "custom_yaml_str_root" # Example of setting a custom root via YAML
7 drop: false # Don't auto-delete when dropped
8 override_file: true # Override existing files
9 entries:
10 - path: text_file.txt # Text file
11 type: text_file
12 content: Content from YAML string
13 - path: empty_file.txt # Empty file
14 type: empty_file
15 - path: directory # Directory
16 type: directory
17 - path: directory/nested.txt
18 type: text_file
19 content: Nested in directory from string
20 - path: secrets.conf
21 type: text_file
22 content: "api_key=verysecret"
23 settings:
24 readonly: true # This file will be read-only
25 "#;
26
27 let tree = tree_fs::from_yaml_str(comprehensive_yaml)
28 .expect("Failed to create comprehensive tree from YAML string");
29
30 println!("Comprehensive tree created at: {}", tree.root.display());
31 println!(" (This tree will NOT be auto-deleted and uses a custom root name if not in temp)");
32
33 let readonly_secret_path = tree.root.join("secrets.conf");
34 match fs::metadata(&readonly_secret_path) {
35 Ok(metadata) => {
36 assert!(
37 metadata.permissions().readonly(),
38 "secrets.conf should be read-only"
39 );
40 println!("Verified: {} is read-only.", readonly_secret_path.display());
41 }
42 Err(e) => eprintln!(
43 "Could not get metadata for {}: {}",
44 readonly_secret_path.display(),
45 e
46 ),
47 }
48
49 println!(
50 "Example finished. If root was not in temp, manually delete: {}",
51 tree.root.display()
52 );
53}