11use crate :: error:: Result ;
22use crate :: templates;
3- use rohas_parser:: { Api , Event , FieldType , Model , Schema , WebSocket } ;
3+ use rohas_parser:: { Api , Event , FieldType , Model , Schema , Type , WebSocket } ;
44use std:: fs;
55use std:: path:: Path ;
66
@@ -89,13 +89,28 @@ pub fn generate_dtos(schema: &Schema, output_dir: &Path) -> Result<()> {
8989 fs:: write ( dto_dir. join ( file_name) , content) ?;
9090 }
9191
92+ for type_def in & schema. types {
93+ let content = generate_model_content ( & rohas_parser:: Model {
94+ name : type_def. name . clone ( ) ,
95+ fields : type_def. fields . clone ( ) ,
96+ attributes : vec ! [ ] ,
97+ } ) ;
98+ let file_name = format ! ( "{}.rs" , templates:: to_snake_case( & type_def. name) ) ;
99+ fs:: write ( dto_dir. join ( file_name) , content) ?;
100+ }
101+
92102 let mut mod_content = String :: new ( ) ;
93103 mod_content. push_str ( "// Auto-generated module declarations\n " ) ;
94104 for input in & schema. inputs {
95105 let mod_name = templates:: to_snake_case ( & input. name ) ;
96106 mod_content. push_str ( & format ! ( "pub mod {};\n " , mod_name) ) ;
97107 mod_content. push_str ( & format ! ( "pub use {}::{};\n " , mod_name, input. name) ) ;
98108 }
109+ for type_def in & schema. types {
110+ let mod_name = templates:: to_snake_case ( & type_def. name ) ;
111+ mod_content. push_str ( & format ! ( "pub mod {};\n " , mod_name) ) ;
112+ mod_content. push_str ( & format ! ( "pub use {}::{};\n " , mod_name, type_def. name) ) ;
113+ }
99114 fs:: write ( dto_dir. join ( "mod.rs" ) , mod_content) ?;
100115
101116 Ok ( ( ) )
@@ -105,7 +120,7 @@ pub fn generate_apis(schema: &Schema, output_dir: &Path) -> Result<()> {
105120 let api_dir = output_dir. join ( "generated/api" ) ;
106121
107122 for api in & schema. apis {
108- let content = generate_api_content ( api) ;
123+ let content = generate_api_content ( api, schema ) ;
109124 let file_name = format ! ( "{}.rs" , templates:: to_snake_case( & api. name) ) ;
110125 fs:: write ( api_dir. join ( file_name) , content) ?;
111126 }
@@ -133,7 +148,7 @@ pub fn generate_apis(schema: &Schema, output_dir: &Path) -> Result<()> {
133148 Ok ( ( ) )
134149}
135150
136- fn generate_api_content ( api : & Api ) -> String {
151+ fn generate_api_content ( api : & Api , schema : & Schema ) -> String {
137152 let mut content = String :: new ( ) ;
138153
139154 content. push_str ( "use serde::{Deserialize, Serialize};\n " ) ;
@@ -151,7 +166,15 @@ fn generate_api_content(api: &Api) -> String {
151166 let is_custom_response = matches ! ( response_field_type, rohas_parser:: FieldType :: Custom ( _) ) ;
152167 if is_custom_response {
153168 let response_type_snake = templates:: to_snake_case ( & api. response ) ;
154- content. push_str ( & format ! ( "use crate::generated::models::{}::{};\n " , response_type_snake, api. response) ) ;
169+
170+ let is_type = schema. types . iter ( ) . any ( |t| t. name == api. response ) ;
171+ let is_input = schema. inputs . iter ( ) . any ( |i| i. name == api. response ) ;
172+
173+ if is_type || is_input {
174+ content. push_str ( & format ! ( "use crate::generated::dto::{}::{};\n " , response_type_snake, api. response) ) ;
175+ } else {
176+ content. push_str ( & format ! ( "use crate::generated::models::{}::{};\n " , response_type_snake, api. response) ) ;
177+ }
155178 }
156179 content. push_str ( "\n " ) ;
157180
0 commit comments