tree_fs/
yaml.rs

1use std::path::PathBuf;
2
3use thiserror::Error;
4
5#[derive(Error, Debug)]
6pub enum Error {
7    #[error(transparent)]
8    Yaml(#[from] serde_yaml::Error),
9    #[error(transparent)]
10    IO(#[from] std::io::Error),
11}
12
13pub type Result<T> = std::result::Result<T, Error>;
14
15/// Creates a file tree based on the content of a YAML file.
16///
17/// # Errors
18///
19/// Returns a `Result` containing the path to the root folder of the generated file tree on success,
20/// or an error if the operation fails.
21pub fn from_yaml_file(path: &PathBuf) -> Result<crate::Tree> {
22    let f = std::fs::File::open(path)?;
23    let tree_builder: crate::TreeBuilder = serde_yaml::from_reader(f)?;
24    Ok(tree_builder.create()?)
25}
26
27/// Creates a file tree based on a YAML-formatted string.
28///
29/// # Errors
30/// Returns a `Result` containing the path to the root folder of the generated file tree on success,
31/// or an error if the operation fails.
32pub fn from_yaml_str(content: &str) -> Result<crate::Tree> {
33    let tree_builder: crate::TreeBuilder = serde_yaml::from_str(content)?;
34    Ok(tree_builder.create()?)
35}
36
37/// Default is to drop the directory when the Tree is dropped
38pub const fn default_drop() -> bool {
39    true
40}