spikard-cli 0.16.0-rc.4

Command-line interface for building and validating Spikard applications
Documentation
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
//! Rust GraphQL code generation using async-graphql macros.
//!
//! This generator produces production-ready Rust code with async-graphql for type-safe
//! GraphQL schema implementations. Generated code follows Rust 2024 edition standards,
//! includes comprehensive error handling, and respects async/await patterns.

use super::GraphQLGenerator;
use crate::codegen::graphql::spec_parser::{GraphQLSchema, GraphQLType, TypeKind};
use anyhow::Result;
use heck::{ToPascalCase, ToSnakeCase};

/// Rust GraphQL code generator
///
/// Generates Rust code with async-graphql macro-based implementations.
/// Supports Object types, Input types, Enums, Scalars, Unions, and Interfaces.
#[derive(Debug, Clone)]
pub struct RustGenerator {
    /// Include serde derives for serialization/deserialization
    include_serde: bool,
    /// Include Debug derives
    include_debug: bool,
    /// Use Result<T> return types for resolvers
    use_result_types: bool,
}

impl RustGenerator {
    /// Create a new Rust GraphQL generator with default settings
    pub const fn new() -> Self {
        Self {
            include_serde: true,
            include_debug: true,
            use_result_types: true,
        }
    }

    /// Builder method to disable serde derives
    #[allow(dead_code)]
    pub const fn without_serde(mut self) -> Self {
        self.include_serde = false;
        self
    }

    /// Builder method to disable debug derives
    #[allow(dead_code)]
    pub const fn without_debug(mut self) -> Self {
        self.include_debug = false;
        self
    }

    /// Builder method to use bare types instead of Result
    #[allow(dead_code)]
    pub const fn with_bare_types(mut self) -> Self {
        self.use_result_types = false;
        self
    }

    /// Map GraphQL scalar type to Rust type annotation
    fn map_scalar_type(&self, gql_type: &str) -> String {
        match gql_type {
            "String" => "String".to_string(),
            "Int" => "i32".to_string(),
            "Float" => "f64".to_string(),
            "Boolean" => "bool".to_string(),
            "ID" => "String".to_string(),
            custom => custom.to_pascal_case(),
        }
    }

    /// Map GraphQL type to Rust type with proper nullability and list handling
    fn map_type(&self, field_type: &str, is_nullable: bool, is_list: bool) -> String {
        // Default assumes list items can be nullable
        self.map_type_with_list_item_nullability(field_type, is_nullable, is_list, true)
    }

    /// Map GraphQL type to Rust type with explicit list item nullability
    fn map_type_with_list_item_nullability(
        &self,
        field_type: &str,
        is_nullable: bool,
        is_list: bool,
        list_item_nullable: bool,
    ) -> String {
        let base = self.map_scalar_type(field_type);
        let with_list = if is_list {
            if list_item_nullable {
                format!("Vec<Option<{base}>>")
            } else {
                format!("Vec<{base}>")
            }
        } else {
            base
        };
        if is_nullable {
            format!("Option<{with_list}>")
        } else {
            with_list
        }
    }

    /// Generate documentation from description
    fn gen_doc(&self, description: Option<&str>, indent: usize) -> String {
        if let Some(desc) = description {
            let indent_str = " ".repeat(indent);
            let lines: Vec<&str> = desc.lines().collect();
            let mut result = String::new();
            for line in lines {
                result.push_str(&format!("{indent_str}/// {line}\n"));
            }
            result
        } else {
            String::new()
        }
    }

    /// Generate Object type definition
    fn gen_object_type(&self, type_def: &GraphQLType) -> String {
        let mut code = String::new();
        code.push_str(&self.gen_doc(type_def.description.as_deref(), 0));

        code.push_str("#[derive(");
        let mut derives = vec!["Clone"];
        if self.include_debug {
            derives.push("Debug");
        }
        if self.include_serde {
            derives.push("serde::Serialize");
            derives.push("serde::Deserialize");
        }
        derives.push("async_graphql::SimpleObject");
        code.push_str(&derives.join(", "));
        code.push_str(")]\n");
        code.push_str(&format!("pub struct {} {{\n", type_def.name));

        for field in &type_def.fields {
            code.push_str(&self.gen_doc(field.description.as_deref(), 4));
            code.push_str(&format!(
                "    pub {}: {},\n",
                field.name.to_snake_case(),
                self.map_type(&field.type_name, field.is_nullable, field.is_list)
            ));
        }

        code.push_str("}\n");
        code
    }

    /// Generate `InputObject` type definition
    fn gen_input_object_type(&self, type_def: &GraphQLType) -> String {
        let mut code = String::new();
        code.push_str(&self.gen_doc(type_def.description.as_deref(), 0));

        code.push_str("#[derive(");
        let mut derives = vec!["Clone"];
        if self.include_debug {
            derives.push("Debug");
        }
        if self.include_serde {
            derives.push("serde::Serialize");
            derives.push("serde::Deserialize");
        }
        derives.push("async_graphql::InputObject");
        code.push_str(&derives.join(", "));
        code.push_str(")]\n");
        code.push_str(&format!("pub struct {} {{\n", type_def.name));

        for field in &type_def.input_fields {
            code.push_str(&self.gen_doc(field.description.as_deref(), 4));
            code.push_str(&format!(
                "    pub {}: {},\n",
                field.name.to_snake_case(),
                self.map_type(&field.type_name, field.is_nullable, field.is_list)
            ));
        }

        code.push_str("}\n");
        code
    }

    /// Generate Enum type definition
    fn gen_enum_type(&self, type_def: &GraphQLType) -> String {
        let mut code = String::new();
        code.push_str(&self.gen_doc(type_def.description.as_deref(), 0));

        code.push_str("#[derive(");
        let mut derives = vec!["Clone", "Copy", "PartialEq", "Eq"];
        if self.include_debug {
            derives.push("Debug");
        }
        if self.include_serde {
            derives.push("serde::Serialize");
            derives.push("serde::Deserialize");
        }
        derives.push("async_graphql::Enum");
        code.push_str(&derives.join(", "));
        code.push_str(")]\n");
        code.push_str(&format!("pub enum {} {{\n", type_def.name));

        for value in &type_def.enum_values {
            let variant_name = rust_enum_variant_name(&value.name);
            code.push_str(&self.gen_doc(value.description.as_deref(), 4));
            code.push_str(&format!("    #[graphql(name = \"{}\")]\n", value.name));
            if self.include_serde {
                code.push_str(&format!("    #[serde(rename = \"{}\")]\n", value.name));
            }
            code.push_str(&format!("    {variant_name},\n"));
        }

        code.push_str("}\n");
        code
    }

    /// Generate Scalar type alias
    fn gen_scalar_type(&self, type_def: &GraphQLType) -> String {
        format!(
            "/// Custom scalar type: {}\npub type {} = String;\n",
            type_def.description.as_deref().unwrap_or(""),
            type_def.name
        )
    }

    /// Generate Union type enum
    fn gen_union_type(&self, type_def: &GraphQLType) -> String {
        let mut code = String::new();
        code.push_str(&self.gen_doc(type_def.description.as_deref(), 0));

        code.push_str("#[derive(Clone");
        if self.include_debug {
            code.push_str(", Debug");
        }
        if self.include_serde {
            code.push_str(", serde::Serialize, serde::Deserialize");
        }
        code.push_str(", async_graphql::Union");
        code.push_str(")]\n");
        code.push_str(&format!("pub enum {} {{\n", type_def.name));

        for possible_type in &type_def.possible_types {
            code.push_str(&format!("    {possible_type}({possible_type}),\n"));
        }

        code.push_str("}\n");
        code
    }

    /// Generate Interface trait
    fn gen_interface_type(&self, type_def: &GraphQLType) -> String {
        let mut code = String::new();
        code.push_str(&self.gen_doc(type_def.description.as_deref(), 0));

        if type_def.possible_types.is_empty() {
            code.push_str(&format!(
                "// Interface {} has no discovered implementors in the parsed schema.\n",
                type_def.name
            ));
            return code;
        }

        code.push_str("#[derive(Clone");
        if self.include_debug {
            code.push_str(", Debug");
        }
        code.push_str(", async_graphql::Interface)]\n");

        for field in &type_def.fields {
            code.push_str("#[graphql(");
            code.push_str(&format!(
                "field(name = \"{}\", ty = \"{}\"",
                field.name,
                self.map_type_with_list_item_nullability(
                    &field.type_name,
                    field.is_nullable,
                    field.is_list,
                    field.list_item_nullable,
                )
            ));

            for arg in &field.arguments {
                code.push_str(&format!(
                    ", arg(name = \"{}\", ty = \"{}\")",
                    arg.name,
                    self.map_type_with_list_item_nullability(
                        &arg.type_name,
                        arg.is_nullable,
                        arg.is_list,
                        arg.list_item_nullable,
                    )
                ));
            }

            code.push_str("))]\n");
        }

        code.push_str(&format!("pub enum {} {{\n", type_def.name));
        for possible_type in &type_def.possible_types {
            code.push_str(&format!("    {possible_type}({possible_type}),\n"));
        }
        code.push_str("}\n");
        code
    }

    /// Generate type definition code
    fn gen_type_definition(&self, type_def: &GraphQLType) -> String {
        match type_def.kind {
            TypeKind::Object => self.gen_object_type(type_def),
            TypeKind::InputObject => self.gen_input_object_type(type_def),
            TypeKind::Enum => self.gen_enum_type(type_def),
            TypeKind::Scalar => self.gen_scalar_type(type_def),
            TypeKind::Union => self.gen_union_type(type_def),
            TypeKind::Interface => self.gen_interface_type(type_def),
            _ => String::new(),
        }
    }

    fn gen_types_body(&self, schema: &GraphQLSchema) -> String {
        let mut code = String::new();

        for (type_name, type_def) in &schema.types {
            if matches!(type_name.as_str(), "String" | "Int" | "Float" | "Boolean" | "ID") {
                continue;
            }

            code.push_str(&self.gen_type_definition(type_def));
            code.push('\n');
        }

        code
    }

    fn gen_resolvers_body(&self, schema: &GraphQLSchema) -> String {
        let mut code = String::new();
        let has_mutations = !schema.mutations.is_empty();
        let has_subscriptions = !schema.subscriptions.is_empty();

        code.push_str("/// Query root type resolver\n");
        code.push_str("#[derive(Default, Debug)]\n");
        code.push_str("pub struct Query {}\n\n");
        code.push_str("#[async_graphql::Object]\n");
        code.push_str("impl Query {\n");

        if schema.queries.is_empty() {
            code.push_str("    // No query fields were discovered in the parsed schema.\n");
        } else {
            for field in &schema.queries {
                code.push_str(&self.gen_doc(field.description.as_deref(), 4));
                code.push_str(&format!("    pub async fn {}(&self", field.name.to_snake_case()));

                for arg in &field.arguments {
                    code.push_str(&format!(
                        ", {}: {}",
                        arg.name.to_snake_case(),
                        self.map_type(&arg.type_name, arg.is_nullable, arg.is_list)
                    ));
                }

                let return_type = self.map_type(&field.type_name, field.is_nullable, field.is_list);
                if self.use_result_types {
                    code.push_str(&format!(") -> async_graphql::Result<{return_type}> {{\n"));
                    code.push_str(&format!(
                        "        Err(async_graphql::Error::new(\"Implement query resolver for {}\"))\n",
                        field.name
                    ));
                } else {
                    code.push_str(&format!(") -> {return_type} {{\n"));
                    code.push_str(
                        "        unreachable!(\"Enable result-based resolver stubs for safe scaffolding\")\n",
                    );
                }
                code.push_str("    }\n");
            }
        }

        code.push_str("}\n\n");

        if has_mutations {
            code.push_str("/// Mutation root type resolver\n");
            code.push_str("#[derive(Default, Debug)]\n");
            code.push_str("pub struct Mutation {}\n\n");
            code.push_str("#[async_graphql::Object]\n");
            code.push_str("impl Mutation {\n");

            for field in &schema.mutations {
                code.push_str(&self.gen_doc(field.description.as_deref(), 4));
                code.push_str(&format!("    pub async fn {}(&self", field.name.to_snake_case()));

                for arg in &field.arguments {
                    code.push_str(&format!(
                        ", {}: {}",
                        arg.name.to_snake_case(),
                        self.map_type(&arg.type_name, arg.is_nullable, arg.is_list)
                    ));
                }

                let return_type = self.map_type(&field.type_name, field.is_nullable, field.is_list);
                if self.use_result_types {
                    code.push_str(&format!(") -> async_graphql::Result<{return_type}> {{\n"));
                    code.push_str(&format!(
                        "        Err(async_graphql::Error::new(\"Implement mutation resolver for {}\"))\n",
                        field.name
                    ));
                } else {
                    code.push_str(&format!(") -> {return_type} {{\n"));
                    code.push_str(
                        "        unreachable!(\"Enable result-based resolver stubs for safe scaffolding\")\n",
                    );
                }
                code.push_str("    }\n");
            }

            code.push_str("}\n\n");
        }

        if has_subscriptions {
            code.push_str("/// Subscription root type resolver\n");
            code.push_str("#[derive(Default, Debug)]\n");
            code.push_str("pub struct Subscription {}\n\n");
            code.push_str("#[async_graphql::Subscription]\n");
            code.push_str("impl Subscription {\n");
            for field in &schema.subscriptions {
                code.push_str(&self.gen_doc(field.description.as_deref(), 4));
                code.push_str(&format!("    pub async fn {}(&self", field.name.to_snake_case()));

                for arg in &field.arguments {
                    code.push_str(&format!(
                        ", {}: {}",
                        arg.name.to_snake_case(),
                        self.map_type_with_list_item_nullability(
                            &arg.type_name,
                            arg.is_nullable,
                            arg.is_list,
                            arg.list_item_nullable,
                        )
                    ));
                }

                let item_type = self.map_type_with_list_item_nullability(&field.type_name, false, false, false);
                code.push_str(&format!(
                    ") -> impl futures_util::stream::Stream<Item = {}> {{\n",
                    item_type
                ));
                code.push_str(&format!("        futures_util::stream::empty::<{}>()\n", item_type));
                code.push_str("    }\n");
            }
            code.push_str("}\n\n");
        }

        code
    }

    fn gen_schema_body(&self, schema: &GraphQLSchema) -> String {
        let mut code = String::new();
        let query_value = "Query::default()";
        let mutation_root = if schema.mutations.is_empty() {
            "async_graphql::EmptyMutation"
        } else {
            "Mutation"
        };
        let subscription_root = if schema.subscriptions.is_empty() {
            "async_graphql::EmptySubscription"
        } else {
            "Subscription"
        };
        let mutation_value = if schema.mutations.is_empty() {
            "async_graphql::EmptyMutation"
        } else {
            "Mutation::default()"
        };
        let subscription_value = if schema.subscriptions.is_empty() {
            "async_graphql::EmptySubscription"
        } else {
            "Subscription::default()"
        };

        code.push_str("/// Build the complete GraphQL schema\n");
        code.push_str("///\n");
        code.push_str("/// Constructs the async-graphql schema with generated root resolvers.\n");
        code.push_str("/// The schema is type-safe and fully introspectable.\n");
        code.push_str(&format!(
            "pub fn build_schema() -> async_graphql::Schema<Query, {mutation_root}, {subscription_root}> {{\n"
        ));
        code.push_str(&format!(
            "    async_graphql::Schema::build({query_value}, {mutation_value}, {subscription_value})\n"
        ));
        code.push_str("        .finish()\n");
        code.push_str("}\n\n");

        code.push_str("#[cfg(test)]\n");
        code.push_str("mod tests {\n");
        code.push_str("    use super::*;\n\n");
        code.push_str("    #[tokio::test]\n");
        code.push_str("    async fn test_schema_builds_successfully() {\n");
        code.push_str("        let _schema = build_schema();\n");
        code.push_str("    }\n");
        code.push_str("}\n");

        code
    }
}

impl Default for RustGenerator {
    fn default() -> Self {
        Self::new()
    }
}

fn rust_enum_variant_name(name: &str) -> String {
    let candidate = name.to_pascal_case();
    match candidate.as_str() {
        "Self" => "SelfValue".to_string(),
        "Super" => "SuperValue".to_string(),
        _ if candidate.is_empty() => "UnknownValue".to_string(),
        _ => candidate,
    }
}

impl GraphQLGenerator for RustGenerator {
    fn generate_types(&self, schema: &GraphQLSchema) -> Result<String> {
        let mut code = String::new();

        code.push_str("// GraphQL type definitions\n");
        code.push_str("// Auto-generated by Spikard CLI with async-graphql macros\n");
        code.push_str("// Rust 2024 edition, zero-copy serialization ready\n\n");
        code.push_str(&self.gen_types_body(schema));

        Ok(code)
    }

    fn generate_resolvers(&self, schema: &GraphQLSchema) -> Result<String> {
        let mut code = String::new();

        code.push_str("// GraphQL resolvers (Query, Mutation, Subscription)\n");
        code.push_str("// Auto-generated by Spikard CLI\n\n");
        code.push_str(&self.gen_resolvers_body(schema));

        Ok(code)
    }

    fn generate_schema_definition(&self, _schema: &GraphQLSchema) -> Result<String> {
        let mut code = String::new();

        code.push_str("// GraphQL schema builder\n");
        code.push_str("// Auto-generated by Spikard CLI\n\n");
        code.push_str(&self.gen_schema_body(_schema));

        Ok(code)
    }

    fn generate_complete(&self, schema: &GraphQLSchema) -> Result<String> {
        let mut code = String::new();

        code.push_str("// GraphQL schema implementation\n");
        code.push_str("// Auto-generated by Spikard CLI with async-graphql\n");
        code.push_str("// Rust 2024 edition, ready for integration into a Spikard app\n\n");
        code.push_str(&self.gen_types_body(schema));
        code.push('\n');
        code.push_str(&self.gen_resolvers_body(schema));
        code.push_str(&self.gen_schema_body(schema));

        Ok(code)
    }
}

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

    #[test]
    fn test_map_scalar_types() {
        let generator = RustGenerator::new();
        assert_eq!(generator.map_scalar_type("String"), "String");
        assert_eq!(generator.map_scalar_type("Int"), "i32");
        assert_eq!(generator.map_scalar_type("Float"), "f64");
        assert_eq!(generator.map_scalar_type("Boolean"), "bool");
        assert_eq!(generator.map_scalar_type("ID"), "String");
    }

    #[test]
    fn test_map_type_non_nullable() {
        let generator = RustGenerator::new();
        assert_eq!(generator.map_type("String", false, false), "String");
    }

    #[test]
    fn test_map_type_nullable() {
        let generator = RustGenerator::new();
        assert_eq!(generator.map_type("String", true, false), "Option<String>");
    }

    #[test]
    fn test_map_type_list() {
        let generator = RustGenerator::new();
        // Default list_item_nullable is true, so [String] (without !) → Vec<Option<String>>
        assert_eq!(generator.map_type("String", false, true), "Vec<Option<String>>");
    }

    #[test]
    fn test_map_type_nullable_list() {
        let generator = RustGenerator::new();
        // Default list_item_nullable is true, so [String] (without !) → Option<Vec<Option<String>>>
        assert_eq!(generator.map_type("String", true, true), "Option<Vec<Option<String>>>");
    }

    #[test]
    fn test_map_type_with_list_item_nullability_nullable_items() {
        let generator = RustGenerator::new();
        // [String] → Option<Vec<Option<String>>>
        assert_eq!(
            generator.map_type_with_list_item_nullability("String", true, true, true),
            "Option<Vec<Option<String>>>"
        );
    }

    #[test]
    fn test_map_type_with_list_item_nullability_non_nullable_items() {
        let generator = RustGenerator::new();
        // [String!] → Option<Vec<String>>
        assert_eq!(
            generator.map_type_with_list_item_nullability("String", true, true, false),
            "Option<Vec<String>>"
        );
    }

    #[test]
    fn test_map_type_with_list_item_nullability_non_null_list_nullable_items() {
        let generator = RustGenerator::new();
        // [String]! → Vec<Option<String>>
        assert_eq!(
            generator.map_type_with_list_item_nullability("String", false, true, true),
            "Vec<Option<String>>"
        );
    }

    #[test]
    fn test_map_type_with_list_item_nullability_non_null_list_non_null_items() {
        let generator = RustGenerator::new();
        // [String!]! → Vec<String>
        assert_eq!(
            generator.map_type_with_list_item_nullability("String", false, true, false),
            "Vec<String>"
        );
    }

    #[test]
    fn test_generator_has_default() {
        let _ = RustGenerator::default();
        let _ = RustGenerator::new();
    }

    #[test]
    fn test_generator_builder_methods() {
        let generator = RustGenerator::new().without_serde().without_debug().with_bare_types();
        assert!(!generator.include_serde);
        assert!(!generator.include_debug);
        assert!(!generator.use_result_types);
    }
}