tree_create/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
use std::fs;
use std::io::{self, BufRead};
use std::path::Path;

#[cfg(test)]
mod tests;

#[derive(Debug, PartialEq)]
struct TreeNode {
    name: String,
    indent_level: usize,
}

#[derive(Debug)]
struct TreeStructure {
    nodes: Vec<TreeNode>,
    #[allow(dead_code)] // This is used implicitly in Debug
    indent_width: usize,
}

impl TreeStructure {
    /// Parse a string in either ASCII tree format or indented format into our internal representation
    pub fn from_string(input: &str) -> io::Result<Self> {
        if input.is_empty() {
            return Err(io::Error::new(io::ErrorKind::InvalidData, "Input is empty"));
        }

        let first_line = input.lines().next().unwrap_or("");
        let leading_spaces = first_line.chars().take_while(|c| c.is_whitespace()).count();

        if leading_spaces > 0 {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "Root directory (line 1) should not be indented",
            ));
        }

        if is_ascii_tree(input) {
            Self::from_ascii_tree(input)
        } else {
            Self::from_indented(input)
        }
    }

    /// Convert ASCII tree format ("├── file.txt") to our internal representation
    fn from_ascii_tree(input: &str) -> io::Result<Self> {
        let mut nodes: Vec<TreeNode> = Vec::new();

        for line in input.lines() {
            if line.trim().is_empty() {
                continue;
            }

            // Count the indent level based on the tree characters
            let prefixes = line
                .chars()
                .take_while(|&c| c == ' ' || c == '│' || c == '├' || c == '└')
                .count();

            // Each level consists of either "│   " (4 chars) or "├── " (4 chars)
            let indent_level = if prefixes == 0 { 0 } else { (prefixes + 3) / 4 };

            // Extract the name by trimming tree characters
            let name = line
                .trim_start_matches(|c: char| {
                    c.is_whitespace() || c == '│' || c == '├' || c == '└' || c == '─'
                })
                .to_string();

            nodes.push(TreeNode { name, indent_level });
        }

        if nodes.is_empty() {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "No valid nodes found",
            ));
        }

        Ok(Self {
            nodes,
            indent_width: 2, // Default indent width for output
        })
    }

    /// Parse simple indented format into our internal representation
    fn from_indented(input: &str) -> io::Result<Self> {
        let mut nodes: Vec<TreeNode> = Vec::new();
        let mut indent_width = None;

        for (line_num, line) in input.lines().enumerate() {
            if line.trim().is_empty() {
                continue;
            }

            let spaces = line.chars().take_while(|c| c.is_whitespace()).count();
            let name = line.trim().to_string();

            // If this is the first indented line, use it to determine indent width
            if spaces > 0 && indent_width.is_none() {
                indent_width = Some(spaces);
            }

            let indent_width = indent_width.unwrap_or(2);

            // Validate indentation is consistent
            if spaces % indent_width != 0 {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidData,
                    format!(
                        "Inconsistent indentation at line {}. Expected a multiple of {} spaces (found {} spaces)",
                        line_num + 1, indent_width, spaces
                    )
                ));
            }

            let indent_level = spaces / indent_width;

            // Validate indent level doesn't skip levels
            if let Some(prev_node) = nodes.last() {
                if indent_level > prev_node.indent_level + 1 {
                    return Err(io::Error::new(
                        io::ErrorKind::InvalidData,
                        format!(
                            "Invalid indentation at line {}. Indentation can only increase by one level at a time",
                            line_num + 1
                        )
                    ));
                }
            }

            nodes.push(TreeNode { name, indent_level });
        }

        if nodes.is_empty() {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "No valid nodes found",
            ));
        }

        Ok(Self {
            nodes,
            indent_width: indent_width.unwrap_or(2),
        })
    }

    /// Convert the internal representation to ASCII tree format
    fn to_ascii_tree(&self) -> String {
        let mut result = Vec::new();

        // First pass: determine which levels have subsequent siblings
        let mut level_has_next = [false; 32]; // 32 levels should be enough

        for (i, node) in self.nodes.iter().enumerate() {
            let current_level = node.indent_level;

            // Look ahead to see if there are any more nodes at this level
            if let Some(next_node) = self.nodes.get(i + 1) {
                if next_node.indent_level == current_level {
                    level_has_next[current_level] = true;
                }
            }
        }

        // Second pass: generate the tree
        for node in self.nodes.iter() {
            let mut prefix = String::new();

            // Add vertical lines for previous levels that have subsequent nodes
            prefix.extend(
                level_has_next
                    .iter()
                    .take(node.indent_level)
                    .skip(1)
                    .map(|&has_next| if has_next { "│   " } else { "    " }),
            );

            // Add the appropriate branch character
            if node.indent_level > 0 {
                prefix.push_str(if level_has_next[node.indent_level] {
                    "├── "
                } else {
                    "└── "
                });
            }

            result.push(format!("{}{}", prefix, node.name));
        }

        result.join("\n")
    }
}

/// Check if input is using ASCII tree format
fn is_ascii_tree(input: &str) -> bool {
    input.contains("├──") || input.contains("└──") || input.contains("│")
}

pub fn create_tree(input: &str, base_path: &Path) -> io::Result<()> {
    // First convert the input to our internal representation
    let tree = TreeStructure::from_string(input)?;

    // Then convert to ASCII tree format for processing
    let ascii_tree = tree.to_ascii_tree();

    let reader = io::BufReader::new(ascii_tree.as_bytes());
    let mut lines = reader.lines().peekable();

    // Get the root directory name from the first line
    let root_name = if let Some(Ok(first_line)) = lines.next() {
        first_line.trim_end_matches('/').to_string()
    } else {
        return Err(io::Error::new(io::ErrorKind::InvalidData, "Input is empty"));
    };

    let base_path = base_path.join(&root_name);
    if !base_path.exists() {
        fs::create_dir_all(&base_path)?;
        println!("Created root directory: {:?}", base_path);
    } else {
        println!("Root directory already exists: {:?}", base_path);
    }

    let mut current_depth = 0;
    let mut path_stack = vec![base_path.clone()];

    for line in lines {
        let line = line?;
        let depth = line
            .chars()
            .take_while(|&c| c == ' ' || c == '│' || c == '└' || c == '├')
            .count()
            / 4;
        let name = line
            .trim_start_matches(|c: char| {
                c.is_whitespace() || c == '│' || c == '└' || c == '├' || c == '─'
            })
            .to_string();

        // Adjust the path stack based on the new depth
        while depth < current_depth && !path_stack.is_empty() {
            path_stack.pop();
            current_depth -= 1;
        }
        current_depth = depth;

        let mut full_path = path_stack
            .last()
            .cloned()
            .unwrap_or_else(|| base_path.clone());
        full_path.push(&name);

        if name.ends_with('/') {
            if !full_path.exists() {
                fs::create_dir_all(&full_path)?;
                println!("Created directory: {:?}", full_path);
            } else {
                println!("Directory already exists: {:?}", full_path);
            }
            path_stack.push(full_path);
        } else {
            if let Some(parent) = full_path.parent() {
                fs::create_dir_all(parent)?;
            }
            if !full_path.exists() {
                fs::File::create(&full_path)?;
                println!("Created file: {:?}", full_path);
            } else {
                println!("File already exists: {:?}", full_path);
            }
        }
    }

    Ok(())
}