Crates.io | tree-fs |
lib.rs | tree-fs |
version | 0.3.1 |
created_at | 2023-12-02 07:41:08.608535+00 |
updated_at | 2025-05-26 07:48:34.362155+00 |
description | Provides a convenient way to create a tree of files |
homepage | |
repository | https://github.com/kaplanelad/tree-fs |
max_upload_size | |
id | 1055971 |
size | 60,912 |
tree-fs
is a Rust library designed to simplify the creation and management of temporary file system structures. Its core feature is creating directories and files programmatically, which are automatically cleaned up when the managing object goes out of scope. This makes it ideal for scenarios where you need a predictable, temporary workspace, such as testing, build processes, or data generation tasks.
tree-fs
?Manually creating and cleaning up temporary file structures can be cumbersome and error-prone. You might need to:
tree-fs
automates these tasks, making your code cleaner and more reliable. Consider these use cases:
config/app.json
) can use tree-fs
to generate temporary config files for specific runs.yaml
feature).Tree
instance goes out of scope (this can be disabled).Add tree-fs
to your Cargo.toml
:
[dependencies]
tree-fs = "0.3" # Replace with the latest version
The TreeBuilder
provides a fluent interface to construct your desired file system structure.
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());
For a more comprehensive example covering custom root directories, overriding files, and various file types, see examples/builder.rs
.
You can disable this behavior using .drop(false)
on the builder if you need the files to persist.
yaml
feature)To use YAML, enable the yaml
feature for tree-fs
in your Cargo.toml
.
You can define your file tree in a YAML file. This is useful for complex or reusable structures.
Example tests/fixtures/tree.yaml
:
override_file: false
entries:
- path: foo.json
type: text_file
content: |
{ "foo": "bar" }
- path: folder/bar.yaml
type: text_file
content: |
foo: bar
- path: readonly_config.ini
type: text_file
content: |
; Sample read-only INI file
[general]
setting = value
settings:
readonly: true
Rust code to load the YAML file:
See the example file examples/yaml-file.rs
for how to load this structure using tree_fs::from_yaml_file
.
For simpler or inline definitions, you can provide the YAML structure as a string.
See the example file examples/yaml-str.rs
for how to load a structure from a YAML string using tree_fs::from_yaml_str
, including defining settings like readonly
.
Contributions, issues, and feature requests are welcome! Feel free to check the issues page.
This project is licensed under the Apache-2.0 License - see the LICENSE file for details.