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
use std::fs;
use std::io::{self, BufRead};
use std::path::Path;

pub fn create_tree(input: &str, base_path: &Path) -> io::Result<()> {
    let reader = io::BufReader::new(input.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(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    #[test]
    fn test_create_simple_structure() -> io::Result<()> {
        let temp_dir = TempDir::new()?;
        let input = "
test_project/
    src/
        main.rs
    Cargo.toml
";
        create_tree(input, temp_dir.path())?;

        assert!(temp_dir.path().join("test_project").is_dir());
        assert!(temp_dir.path().join("test_project/src").is_dir());
        assert!(temp_dir.path().join("test_project/src/main.rs").is_file());
        assert!(temp_dir.path().join("test_project/Cargo.toml").is_file());

        Ok(())
    }

    #[test]
    fn test_create_nested_structure() -> io::Result<()> {
        let temp_dir = TempDir::new()?;
        let input = "
nested_project/
    src/
        module1/
            mod.rs
        module2/
            submodule/
                mod.rs
    tests/
        integration_tests.rs
    Cargo.toml
";
        create_tree(input, temp_dir.path())?;

        assert!(temp_dir.path().join("nested_project").is_dir());
        assert!(temp_dir.path().join("nested_project/src").is_dir());
        assert!(temp_dir.path().join("nested_project/src/module1").is_dir());
        assert!(temp_dir
            .path()
            .join("nested_project/src/module1/mod.rs")
            .is_file());
        assert!(temp_dir
            .path()
            .join("nested_project/src/module2/submodule")
            .is_dir());
        assert!(temp_dir
            .path()
            .join("nested_project/src/module2/submodule/mod.rs")
            .is_file());
        assert!(temp_dir.path().join("nested_project/tests").is_dir());
        assert!(temp_dir
            .path()
            .join("nested_project/tests/integration_tests.rs")
            .is_file());
        assert!(temp_dir.path().join("nested_project/Cargo.toml").is_file());

        Ok(())
    }

    #[test]
    fn test_create_existing_structure() -> io::Result<()> {
        let temp_dir = TempDir::new()?;
        fs::create_dir(temp_dir.path().join("existing_project"))?;
        fs::create_dir(temp_dir.path().join("existing_project/src"))?;
        fs::File::create(temp_dir.path().join("existing_project/src/main.rs"))?;

        let input = "
existing_project/
    src/
        main.rs
    Cargo.toml
";
        create_tree(input, temp_dir.path())?;

        assert!(temp_dir.path().join("existing_project").is_dir());
        assert!(temp_dir.path().join("existing_project/src").is_dir());
        assert!(temp_dir
            .path()
            .join("existing_project/src/main.rs")
            .is_file());
        assert!(temp_dir
            .path()
            .join("existing_project/Cargo.toml")
            .is_file());

        Ok(())
    }
}