@@ -39,11 +39,11 @@ pub async fn generate(args: GenerateArgs) -> Result<()> {
3939
4040async fn generate_handler ( name : & str ) -> Result < ( ) > {
4141 let handlers_dir = Path :: new ( "src/handlers" ) ;
42-
42+
4343 // Create handlers directory if it doesn't exist
4444 if !handlers_dir. exists ( ) {
4545 fs:: create_dir_all ( handlers_dir) . await ?;
46-
46+
4747 // Create mod.rs
4848 let mod_content = format ! ( "pub mod {};\n " , name) ;
4949 fs:: write ( handlers_dir. join ( "mod.rs" ) , mod_content) . await ?;
@@ -131,27 +131,50 @@ pub struct Update{type_name} {{
131131 let handler_path = handlers_dir. join ( format ! ( "{}.rs" , name) ) ;
132132 fs:: write ( & handler_path, handler_content) . await ?;
133133
134- println ! ( "{} Generated handler: {}" , style( "✓" ) . green( ) , handler_path. display( ) ) ;
134+ println ! (
135+ "{} Generated handler: {}" ,
136+ style( "✓" ) . green( ) ,
137+ handler_path. display( )
138+ ) ;
135139 println ! ( ) ;
136140 println ! ( "Don't forget to register the routes in main.rs:" ) ;
137- println ! ( " {}" , style( format!( ".mount(handlers::{}::list)" , name) ) . cyan( ) ) ;
138- println ! ( " {}" , style( format!( ".mount(handlers::{}::get)" , name) ) . cyan( ) ) ;
139- println ! ( " {}" , style( format!( ".mount(handlers::{}::create)" , name) ) . cyan( ) ) ;
140- println ! ( " {}" , style( format!( ".mount(handlers::{}::update)" , name) ) . cyan( ) ) ;
141- println ! ( " {}" , style( format!( ".mount(handlers::{}::delete)" , name) ) . cyan( ) ) ;
141+ println ! (
142+ " {}" ,
143+ style( format!( ".mount(handlers::{}::list)" , name) ) . cyan( )
144+ ) ;
145+ println ! (
146+ " {}" ,
147+ style( format!( ".mount(handlers::{}::get)" , name) ) . cyan( )
148+ ) ;
149+ println ! (
150+ " {}" ,
151+ style( format!( ".mount(handlers::{}::create)" , name) ) . cyan( )
152+ ) ;
153+ println ! (
154+ " {}" ,
155+ style( format!( ".mount(handlers::{}::update)" , name) ) . cyan( )
156+ ) ;
157+ println ! (
158+ " {}" ,
159+ style( format!( ".mount(handlers::{}::delete)" , name) ) . cyan( )
160+ ) ;
142161
143162 Ok ( ( ) )
144163}
145164
146165async fn generate_model ( name : & str ) -> Result < ( ) > {
147166 let models_dir = Path :: new ( "src/models" ) ;
148-
167+
149168 // Create models directory if it doesn't exist
150169 if !models_dir. exists ( ) {
151170 fs:: create_dir_all ( models_dir) . await ?;
152-
171+
153172 // Create mod.rs
154- let mod_content = format ! ( "mod {};\n pub use {}::*;\n " , name. to_lowercase( ) , name. to_lowercase( ) ) ;
173+ let mod_content = format ! (
174+ "mod {};\n pub use {}::*;\n " ,
175+ name. to_lowercase( ) ,
176+ name. to_lowercase( )
177+ ) ;
155178 fs:: write ( models_dir. join ( "mod.rs" ) , mod_content) . await ?;
156179 } else {
157180 // Append to existing mod.rs
@@ -160,7 +183,10 @@ async fn generate_model(name: &str) -> Result<()> {
160183 let mut content = fs:: read_to_string ( & mod_path) . await ?;
161184 let lower_name = name. to_lowercase ( ) ;
162185 if !content. contains ( & format ! ( "mod {};" , lower_name) ) {
163- content. push_str ( & format ! ( "mod {};\n pub use {}::*;\n " , lower_name, lower_name) ) ;
186+ content. push_str ( & format ! (
187+ "mod {};\n pub use {}::*;\n " ,
188+ lower_name, lower_name
189+ ) ) ;
164190 fs:: write ( & mod_path, content) . await ?;
165191 }
166192 }
@@ -210,16 +236,23 @@ impl {} {{
210236 let model_path = models_dir. join ( format ! ( "{}.rs" , name. to_lowercase( ) ) ) ;
211237 fs:: write ( & model_path, model_content) . await ?;
212238
213- println ! ( "{} Generated model: {}" , style( "✓" ) . green( ) , model_path. display( ) ) ;
239+ println ! (
240+ "{} Generated model: {}" ,
241+ style( "✓" ) . green( ) ,
242+ model_path. display( )
243+ ) ;
214244
215245 Ok ( ( ) )
216246}
217247
218248async fn generate_crud ( name : & str ) -> Result < ( ) > {
219249 // Generate both handler and model
220250 let type_name = to_pascal_case ( name) ;
221-
222- println ! ( "{}" , style( format!( "Generating CRUD for '{}'..." , name) ) . bold( ) ) ;
251+
252+ println ! (
253+ "{}" ,
254+ style( format!( "Generating CRUD for '{}'..." , name) ) . bold( )
255+ ) ;
223256 println ! ( ) ;
224257
225258 generate_model ( & type_name) . await ?;
@@ -238,16 +271,18 @@ fn capitalize(s: &str) -> String {
238271}
239272
240273fn to_pascal_case ( s : & str ) -> String {
241- s. split ( & [ '-' , '_' ] [ ..] )
242- . map ( capitalize)
243- . collect ( )
274+ s. split ( & [ '-' , '_' ] [ ..] ) . map ( capitalize) . collect ( )
244275}
245276
246277fn singularize ( s : & str ) -> String {
247- if s. ends_with ( "ies" ) {
248- format ! ( "{}y" , & s[ ..s. len( ) - 3 ] )
249- } else if s. ends_with ( 's' ) && !s. ends_with ( "ss" ) {
250- s[ ..s. len ( ) - 1 ] . to_string ( )
278+ if let Some ( stripped) = s. strip_suffix ( "ies" ) {
279+ format ! ( "{}y" , stripped)
280+ } else if let Some ( stripped) = s. strip_suffix ( 's' ) {
281+ if !s. ends_with ( "ss" ) {
282+ stripped. to_string ( )
283+ } else {
284+ s. to_string ( )
285+ }
251286 } else {
252287 s. to_string ( )
253288 }
0 commit comments