MCPcopy Create free account
hub / github.com/codeinred/untree / create_tree

Function create_tree

lib/functions.rs:142–182  ·  view source on GitHub ↗

Create a tree based on a sequence of lines describing the tree structure inside the given directory Example: ```rust use untree::*; use std::io::{BufRead, BufReader, stdin, Lines}; let options = UntreeOptions::new() .dry_run(true) .verbose(true); let lines = BufReader::new(stdin()).lines(); create_tree("path/to/directory", lines, options)?; # Ok::<(), Error>(()) ```

(
    directory: impl Into<PathBuf>,
    mut lines: Lines<impl BufRead>,
    options: UntreeOptions,
)

Source from the content-addressed store, hash-verified

140/// # Ok::<(), Error>(())
141/// ```
142pub fn create_tree(
143 directory: impl Into<PathBuf>,
144 mut lines: Lines<impl BufRead>,
145 options: UntreeOptions,
146) -> Result<()> {
147 let mut path = directory.into();
148
149 let mut old_depth = 0;
150
151 // Get the first line
152 if let Some(result) = lines.next() {
153 let line = result?;
154 let (depth, filename) = get_entry(line.as_ref());
155 path.push(filename);
156 path = normalize_path(path.as_path());
157 old_depth = depth;
158 }
159
160 // Get remaining lines
161 for result in lines {
162 let line = result?;
163 if line.is_empty() {
164 break;
165 }
166 let (depth, filename) = get_entry(line.as_ref());
167 if depth <= old_depth {
168 create_path(path.as_path(), FilePath, options)?;
169 for _ in depth..old_depth {
170 path.pop();
171 }
172 path.set_file_name(filename);
173 } else {
174 create_path(path.as_path(), Directory, options)?;
175 path.push(filename);
176 }
177 old_depth = depth;
178 }
179
180 // Create file for last line
181 create_path(path.as_path(), FilePath, options)
182}

Callers 1

runFunction · 0.85

Calls 3

get_entryFunction · 0.85
normalize_pathFunction · 0.85
create_pathFunction · 0.85

Tested by

no test coverage detected