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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
//! # GraphQL Code Generator for Rust ORMs
//!
//! Transform GraphQL schemas into production-ready Rust code for Diesel and Sea-ORM.
//! Automatically generates entities, migrations, and schema definitions from your
//! GraphQL API's introspection.
//!
//! ## Key Features
//!
//! - **Dual ORM Support**: Generate code for both [Diesel](https://diesel.rs) and [Sea-ORM](https://www.sea-ql.org/SeaORM)
//! - **Database Agnostic**: Support for SQLite, PostgreSQL, and MySQL
//! - **Type Safety**: Compile-time guarantees with full GraphQL type mapping
//! - **Migration Ready**: Automatic database migration generation
//! - **Introspection Powered**: Works with any GraphQL API that supports introspection
//! - **Flexible Configuration**: TOML and YAML config support (with feature flag)
//!
//! ## Quick Start
//!
//! ### 1. Install the CLI
//!
//! ```bash
//! cargo install graphql-codegen-rust
//! ```
//!
//! ### 2. Initialize your project
//!
//! ```bash
//! graphql-codegen-rust init --url https://api.example.com/graphql
//! ```
//!
//! ### 3. Generate code
//!
//! ```bash
//! graphql-codegen-rust generate
//! ```
//!
//! ## Library Usage
//!
//! For programmatic use in your Rust applications:
//!
//! ```rust,no_run
//! use graphql_codegen_rust::{CodeGenerator, Config};
//! use graphql_codegen_rust::cli::{OrmType, DatabaseType};
//!
//! # async fn example() -> anyhow::Result<()> {
//! // Create configuration programmatically
//! let config = Config {
//! url: "https://api.example.com/graphql".to_string(),
//! orm: OrmType::Diesel,
//! db: DatabaseType::Postgres,
//! output_dir: "./generated".into(),
//! headers: std::collections::HashMap::new(),
//! type_mappings: std::collections::HashMap::new(),
//! scalar_mappings: std::collections::HashMap::new(),
//! table_naming: Default::default(),
//! generate_migrations: true,
//! generate_entities: true,
//! };
//!
//! // Generate code
//! let generator = CodeGenerator::new(&config.orm);
//! generator.generate_from_config(&config).await?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Configuration
//!
//! ### TOML Configuration (`graphql-codegen-rust.toml`)
//!
//! ```toml
//! url = "https://api.example.com/graphql"
//! orm = "Diesel"
//! db = "Postgres"
//! output_dir = "./generated"
//!
//! [headers]
//! Authorization = "Bearer your-token-here"
//!
//! [type_mappings]
//! # Custom type mappings if needed
//! ```
//!
//! ### YAML Configuration (`codegen.yml`) - *Requires `yaml-codegen-config` feature*
//!
//! ```yaml
//! schema:
//! url: https://api.example.com/graphql
//! headers:
//! Authorization: Bearer your-token-here
//!
//! rust_codegen:
//! orm: Diesel
//! db: Postgres
//! output_dir: ./generated
//! ```
//!
//! ## Generated Code Structure
//!
//! ```text
//! generated/
//! ├── src/
//! │ ├── schema.rs # Diesel schema definitions
//! │ ├── entities/
//! │ │ ├── user.rs # Entity structs and implementations
//! │ │ └── post.rs
//! │ └── mod.rs # Sea-ORM module definitions
//! └── migrations/
//! └── 0001_create_users_table/
//! ├── up.sql
//! └── down.sql
//! ```
//!
//! ## Error Handling
//!
//! The library uses [`anyhow`](https://docs.rs/anyhow) for error handling, providing
//! detailed error messages with context. Common error scenarios:
//!
//! - **Network errors**: GraphQL endpoint unreachable or authentication failures
//! - **Schema errors**: Invalid GraphQL schema or introspection disabled
//! - **Configuration errors**: Missing or invalid configuration files
//! - **Generation errors**: Unsupported GraphQL types or ORM constraints
//!
//! ## Feature Flags
//!
//! - `yaml-codegen-config`: Enable YAML configuration file support
//! - Default features include TOML support and both Diesel and Sea-ORM generators
//!
//! ## Requirements
//!
//! - Rust 1.86+
//! - A GraphQL API that supports introspection
//! - Appropriate database dependencies based on your ORM choice
pub use Config;
pub use create_generator;
use Path;
use fs_err as fs;
/// High-level interface for generating Rust ORM code from GraphQL schemas.
///
/// The `CodeGenerator` provides a unified API for generating code regardless of the
/// underlying ORM (Diesel or Sea-ORM). It handles the complete code generation
/// pipeline: schema introspection, parsing, and code emission.
///
/// ## ORM Support
///
/// - **Diesel**: Generates table schemas, entity structs, and database migrations
/// - **Sea-ORM**: Generates entity models, active records, and migration files
///
/// ## Example
///
/// ```rust,no_run
/// use graphql_codegen_rust::{CodeGenerator, Config};
/// use graphql_codegen_rust::cli::{OrmType, DatabaseType};
///
/// # async fn example() -> anyhow::Result<()> {
/// let config = Config {
/// url: "https://api.example.com/graphql".to_string(),
/// orm: OrmType::Diesel,
/// db: DatabaseType::Postgres,
/// output_dir: "./generated".into(),
/// ..Default::default()
/// };
///
/// let generator = CodeGenerator::new(&config.orm);
/// generator.generate_from_config(&config).await?;
/// # Ok(())
/// # }
/// ```
/// Generates ORM code directly from a configuration file path.
///
/// This is a convenience function that combines configuration loading and code generation
/// into a single call. It automatically detects the configuration format (TOML or YAML)
/// and creates the appropriate code generator.
///
/// # Supported Configuration Formats
///
/// - **TOML**: `graphql-codegen-rust.toml` or any `.toml` file
/// - **YAML**: `codegen.yml`, `codegen.yaml` (requires `yaml-codegen-config` feature)
///
/// # Parameters
/// - `config_path`: Path to the configuration file. Can be any type that converts to `Path`.
///
/// # Returns
/// - `Ok(())` on successful code generation
/// - `Err(anyhow::Error)` with context about what failed
///
/// # Errors
/// This function can fail due to:
/// - Configuration file not found or unreadable
/// - Invalid configuration format or content
/// - Network issues during GraphQL introspection
/// - Code generation failures
///
/// # Example
/// ```rust,no_run
/// use graphql_codegen_rust::generate_from_config_file;
///
/// # async fn example() -> anyhow::Result<()> {
/// // Generate from TOML config
/// generate_from_config_file("graphql-codegen-rust.toml").await?;
///
/// // Generate from YAML config (if feature enabled)
/// generate_from_config_file("codegen.yml").await?;
/// # Ok(())
/// # }
/// ```
pub async
pub async