11use std:: fs;
22use std:: io:: Write ;
3- use std:: path:: { PathBuf } ;
3+ use std:: path:: PathBuf ;
44
55pub struct ProjectGenerator {
66 project_name : String ,
@@ -35,39 +35,42 @@ impl ProjectGenerator {
3535
3636 // Create standard directories
3737 self . create_directories ( ) ?;
38-
38+
3939 // Create project.toml
4040 self . create_project_toml ( ) ?;
41-
41+
4242 // Create basic example file
4343 self . create_example_file ( ) ?;
4444
45- println ! ( "Successfully created new AIScript project: {}" , self . project_name) ;
45+ println ! (
46+ "Successfully created new AIScript project: {}" ,
47+ self . project_name
48+ ) ;
4649 println ! ( "Project structure:" ) ;
4750 println ! ( "{}" , self . display_project_structure( ) ) ;
48- println ! ( "" ) ;
51+ println ! ( ) ;
4952 println ! ( "Run `aiscript serve` to start the server." ) ;
5053
5154 Ok ( ( ) )
5255 }
5356
5457 fn create_directories ( & self ) -> Result < ( ) , String > {
5558 let dirs = vec ! [ "lib" , "routes" ] ;
56-
59+
5760 for dir in dirs {
5861 let dir_path = self . project_path . join ( dir) ;
5962 fs:: create_dir_all ( & dir_path) . map_err ( |e| {
6063 format ! ( "Failed to create directory '{}': {}" , dir_path. display( ) , e)
6164 } ) ?;
6265 }
63-
66+
6467 Ok ( ( ) )
6568 }
6669
6770 fn create_project_toml ( & self ) -> Result < ( ) , String > {
6871 let toml_path = self . project_path . join ( "project.toml" ) ;
6972 let username = whoami:: username ( ) ;
70-
73+
7174 let toml_content = format ! (
7275 r#"[project]
7376name = "{}"
@@ -86,22 +89,20 @@ path = "/docs"
8689"# ,
8790 self . project_name, username
8891 ) ;
89-
90- let mut file = fs:: File :: create ( & toml_path) . map_err ( |e| {
91- format ! ( "Failed to create project.toml: {}" , e)
92- } ) ?;
93-
94- file. write_all ( toml_content. as_bytes ( ) ) . map_err ( |e| {
95- format ! ( "Failed to write to project.toml: {}" , e)
96- } ) ?;
97-
92+
93+ let mut file = fs:: File :: create ( & toml_path)
94+ . map_err ( |e| format ! ( "Failed to create project.toml: {}" , e) ) ?;
95+
96+ file. write_all ( toml_content. as_bytes ( ) )
97+ . map_err ( |e| format ! ( "Failed to write to project.toml: {}" , e) ) ?;
98+
9899 Ok ( ( ) )
99100 }
100101
101102 fn create_example_file ( & self ) -> Result < ( ) , String > {
102103 let routes_dir = self . project_path . join ( "routes" ) ;
103104 let example_path = routes_dir. join ( "index.ai" ) ;
104-
105+
105106 let example_content = r#"// Example AIScript route handler
106107get /hello {
107108 query {
@@ -111,15 +112,13 @@ get /hello {
111112 return { message: f"Hello, {query.name}!" };
112113}
113114"# ;
114-
115- let mut file = fs:: File :: create ( & example_path) . map_err ( |e| {
116- format ! ( "Failed to create example file: {}" , e)
117- } ) ?;
118-
119- file. write_all ( example_content. as_bytes ( ) ) . map_err ( |e| {
120- format ! ( "Failed to write to example file: {}" , e)
121- } ) ?;
122-
115+
116+ let mut file = fs:: File :: create ( & example_path)
117+ . map_err ( |e| format ! ( "Failed to create example file: {}" , e) ) ?;
118+
119+ file. write_all ( example_content. as_bytes ( ) )
120+ . map_err ( |e| format ! ( "Failed to write to example file: {}" , e) ) ?;
121+
123122 Ok ( ( ) )
124123 }
125124
@@ -129,7 +128,7 @@ get /hello {
129128 result. push_str ( "├── routes/\n " ) ;
130129 result. push_str ( "│ └── index.ai\n " ) ;
131130 result. push_str ( "└── project.toml\n " ) ;
132-
131+
133132 result
134133 }
135134}
@@ -145,65 +144,84 @@ mod tests {
145144 // Use tempdir to ensure test files are cleaned up
146145 let temp_dir = tempdir ( ) . unwrap ( ) ;
147146 let temp_path = temp_dir. path ( ) ;
148-
147+
149148 // Create a test project in the temp directory
150149 let project_name = "test_project" ;
151-
150+
152151 // Create an absolute path for the project
153152 let project_path = temp_path. join ( project_name) ;
154-
153+
155154 // Create a generator with the project name
156155 let generator = ProjectGenerator :: new ( project_name) ;
157-
156+
158157 // Override the project path for testing
159158 let generator = ProjectGenerator {
160159 project_name : project_name. to_string ( ) ,
161160 project_path : project_path. clone ( ) ,
162161 } ;
163-
162+
164163 let result = generator. generate ( ) ;
165-
164+
166165 assert ! ( result. is_ok( ) , "Project generation failed: {:?}" , result) ;
167-
166+
168167 // Verify project structure
169168 assert ! ( project_path. exists( ) , "Project directory not created" ) ;
170- assert ! ( project_path. join( "lib" ) . exists( ) , "lib directory not created" ) ;
171- assert ! ( project_path. join( "routes" ) . exists( ) , "routes directory not created" ) ;
172- assert ! ( project_path. join( "project.toml" ) . exists( ) , "project.toml not created" ) ;
173- assert ! ( project_path. join( "routes/index.ai" ) . exists( ) , "Example file not created" ) ;
174-
169+ assert ! (
170+ project_path. join( "lib" ) . exists( ) ,
171+ "lib directory not created"
172+ ) ;
173+ assert ! (
174+ project_path. join( "routes" ) . exists( ) ,
175+ "routes directory not created"
176+ ) ;
177+ assert ! (
178+ project_path. join( "project.toml" ) . exists( ) ,
179+ "project.toml not created"
180+ ) ;
181+ assert ! (
182+ project_path. join( "routes/index.ai" ) . exists( ) ,
183+ "Example file not created"
184+ ) ;
185+
175186 // Verify project.toml content
176187 let toml_content = fs:: read_to_string ( project_path. join ( "project.toml" ) ) . unwrap ( ) ;
177188 assert ! ( toml_content. contains( & format!( "name = \" {}\" " , project_name) ) ) ;
178189 assert ! ( toml_content. contains( "version = \" 0.1.0\" " ) ) ;
179-
190+
180191 // Verify example file content
181192 let example_content = fs:: read_to_string ( project_path. join ( "routes/index.ai" ) ) . unwrap ( ) ;
182193 assert ! ( example_content. contains( "get /hello" ) ) ;
183194 }
184-
195+
185196 #[ test]
186197 fn test_project_already_exists ( ) {
187198 // Use tempdir to ensure test files are cleaned up
188199 let temp_dir = tempdir ( ) . unwrap ( ) ;
189200 let temp_path = temp_dir. path ( ) ;
190-
201+
191202 // Create a directory that will conflict
192203 let project_name = "existing_project" ;
193204 let project_path = temp_path. join ( project_name) ;
194205 fs:: create_dir_all ( & project_path) . unwrap ( ) ;
195-
206+
196207 // Create a generator with absolute path
197208 let generator = ProjectGenerator {
198209 project_name : project_name. to_string ( ) ,
199210 project_path,
200211 } ;
201-
212+
202213 let result = generator. generate ( ) ;
203-
204- assert ! ( result. is_err( ) , "Project generation should fail for existing directory" ) ;
214+
215+ assert ! (
216+ result. is_err( ) ,
217+ "Project generation should fail for existing directory"
218+ ) ;
205219 if let Err ( err) = result {
206- assert ! ( err. contains( "already exists" ) , "Wrong error message: {}" , err) ;
220+ assert ! (
221+ err. contains( "already exists" ) ,
222+ "Wrong error message: {}" ,
223+ err
224+ ) ;
207225 }
208226 }
209227}
0 commit comments