unistructgen 0.2.2

A powerful Rust code generator
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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
# 🧩 Modules Guide

Complete guide to UniStructGen's modular architecture - understand each module, when to use it, and how to leverage its full power.

## 📋 Table of Contents

1. [Architecture Overview]#-architecture-overview
2. [Core Module]#-core-module
3. [JSON Parser Module]#-json-parser-module
4. [Codegen Module]#-codegen-module
5. [Proc-Macro Module]#-proc-macro-module
6. [CLI Module]#-cli-module
6. [Advanced Patterns]#-advanced-patterns
7. [Creating Custom Parsers]#-creating-custom-parsers

---

## 🏗️ Architecture Overview

UniStructGen follows a **pipeline architecture** with clear separation of concerns:

```
┌─────────────────────────────────────────────────────────┐
│                    Input Layer                          │
│  ┌──────────┐  ┌───────────┐  ┌─────────┐  ┌────────┐  │
│  │   JSON   │  │ Markdown  │  │   SQL   │  │  API   │  │
│  └────┬─────┘  └─────┬─────┘  └────┬────┘  └───┬────┘  │
└───────┼──────────────┼──────────────┼───────────┼───────┘
        │              │              │           │
        └──────────────┴──────────────┴───────────┘
┌───────────────────────────▼─────────────────────────────┐
│                    Parser Layer                         │
│  ┌──────────────┐  ┌──────────────┐  ┌──────────────┐  │
│  │ JSON Parser  │  │   MD Parser  │  │  SQL Parser  │  │
│  └──────┬───────┘  └──────┬───────┘  └──────┬───────┘  │
└─────────┼──────────────────┼──────────────────┼─────────┘
          │                  │                  │
          └──────────────────┴──────────────────┘
┌───────────────────────────▼─────────────────────────────┐
│                  Intermediate Layer                     │
│              ┌──────────────────────┐                   │
│              │  IR (Core Module)    │                   │
│              │  - IRModule          │                   │
│              │  - IRStruct          │                   │
│              │  - IRField           │                   │
│              │  - IRType            │                   │
│              └──────────┬───────────┘                   │
└─────────────────────────┼───────────────────────────────┘
┌─────────────────────────▼───────────────────────────────┐
│                  Code Generator Layer                   │
│  ┌──────────────┐  ┌──────────────┐  ┌──────────────┐  │
│  │ Rust Codegen │  │   TS Codegen │  │   Go Codegen │  │
│  └──────┬───────┘  └──────┬───────┘  └──────┬───────┘  │
└─────────┼──────────────────┼──────────────────┼─────────┘
          │                  │                  │
          └──────────────────┴──────────────────┘
┌───────────────────────────▼─────────────────────────────┐
│                    Output Layer                         │
│     ┌────────┐  ┌────────┐  ┌────────┐  ┌────────┐     │
│     │ Rust   │  │  TS    │  │   Go   │  │  More  │     │
│     └────────┘  └────────┘  └────────┘  └────────┘     │
└─────────────────────────────────────────────────────────┘
```

### Why This Architecture?

**Benefits:**
- **Modularity** - Each module has one responsibility
-**Extensibility** - Easy to add new parsers/generators
-**Testability** - Test each layer independently
-**Reusability** - Share IR across different use cases
-**Maintainability** - Clear boundaries between components

---

## 🎯 Core Module

**Package:** `unistructgen-core`

### What It Does

The **foundation** of UniStructGen. Defines the Intermediate Representation (IR) - a language-agnostic way to represent data structures.

### Why You Need It

**Use directly when:**
- Building custom parsers
- Creating custom code generators
- Programmatically constructing types
- Building tools on top of UniStructGen

### Key Components

#### 1. IRModule

The top-level container for all generated types.

```rust
use unistructgen_core::{IRModule, IRType, IRStruct};

// Create a module
let mut module = IRModule::new("MyModule".to_string());

// Add types
module.add_type(IRType::Struct(my_struct));

println!("Module has {} types", module.types.len());
```

#### 2. IRStruct

Represents a struct/class/type.

```rust
use unistructgen_core::{IRStruct, IRField, IRTypeRef, PrimitiveKind};

// Create a struct
let mut user_struct = IRStruct::new("User".to_string());

// Add derives
user_struct.add_derive("Debug".to_string());
user_struct.add_derive("Clone".to_string());

// Add fields
let id_field = IRField::new(
    "id".to_string(),
    IRTypeRef::Primitive(PrimitiveKind::I64)
);
user_struct.add_field(id_field);

let name_field = IRField::new(
    "name".to_string(),
    IRTypeRef::Primitive(PrimitiveKind::String)
);
user_struct.add_field(name_field);
```

#### 3. IRField

Represents a field in a struct.

```rust
use unistructgen_core::{IRField, IRTypeRef, PrimitiveKind};

// Simple field
let mut field = IRField::new(
    "email".to_string(),
    IRTypeRef::Primitive(PrimitiveKind::String)
);

// Add attributes
field.attributes.push("serde(rename = \"emailAddress\")".to_string());

// Make optional
field.optional = true;
field.ty = field.ty.make_optional();

// Documentation
field.documentation = Some("User's email address".to_string());
```

#### 4. IRTypeRef

Represents type references.

```rust
use unistructgen_core::{IRTypeRef, PrimitiveKind};

// Primitive types
let int_type = IRTypeRef::Primitive(PrimitiveKind::I64);
let string_type = IRTypeRef::Primitive(PrimitiveKind::String);
let bool_type = IRTypeRef::Primitive(PrimitiveKind::Bool);

// Complex types
let uuid_type = IRTypeRef::Primitive(PrimitiveKind::Uuid);
let datetime_type = IRTypeRef::Primitive(PrimitiveKind::DateTime);

// Collections
let string_vec = IRTypeRef::Vec(Box::new(
    IRTypeRef::Primitive(PrimitiveKind::String)
));

// Optional
let optional_string = IRTypeRef::Option(Box::new(
    IRTypeRef::Primitive(PrimitiveKind::String)
));

// Named types (references to other structs)
let user_type = IRTypeRef::Named("User".to_string());
```

### Real-World Example: Building Types Programmatically

```rust
use unistructgen_core::{
    IRModule, IRType, IRStruct, IRField, IRTypeRef, PrimitiveKind
};

fn create_user_module() -> IRModule {
    let mut module = IRModule::new("UserModule".to_string());

    // Create User struct
    let mut user = IRStruct::new("User".to_string());
    user.add_derive("Debug".to_string());
    user.add_derive("Clone".to_string());
    user.add_derive("serde::Serialize".to_string());
    user.add_derive("serde::Deserialize".to_string());

    // Add ID field
    let mut id_field = IRField::new(
        "id".to_string(),
        IRTypeRef::Primitive(PrimitiveKind::Uuid)
    );
    id_field.documentation = Some("Unique user identifier".to_string());
    user.add_field(id_field);

    // Add name field
    let mut name_field = IRField::new(
        "name".to_string(),
        IRTypeRef::Primitive(PrimitiveKind::String)
    );
    name_field.documentation = Some("User's full name".to_string());
    user.add_field(name_field);

    // Add optional email
    let mut email_field = IRField::new(
        "email".to_string(),
        IRTypeRef::Option(Box::new(
            IRTypeRef::Primitive(PrimitiveKind::String)
        ))
    );
    email_field.optional = true;
    user.add_field(email_field);

    // Add created_at
    let created_field = IRField::new(
        "created_at".to_string(),
        IRTypeRef::Primitive(PrimitiveKind::DateTime)
    );
    user.add_field(created_field);

    // Add tags array
    let tags_field = IRField::new(
        "tags".to_string(),
        IRTypeRef::Vec(Box::new(
            IRTypeRef::Primitive(PrimitiveKind::String)
        ))
    );
    user.add_field(tags_field);

    module.add_type(IRType::Struct(user));
    module
}

fn main() {
    let module = create_user_module();
    println!("Created module with {} types", module.types.len());

    // Now you can pass this to a code generator!
}
```

---

## 📦 JSON Parser Module

**Package:** `unistructgen-json-parser`

### What It Does

Parses JSON into IR with **smart type inference** and field name sanitization.

### Why You Need It

**Use directly when:**
- Building custom CLI tools
- Integrating into build systems
- Processing JSON at runtime
- Need fine-grained control over parsing

### Key Components

#### 1. JsonParser

The main parser implementation.

```rust
use unistructgen_json_parser::{JsonParser, ParserOptions};
use unistructgen_core::Parser;

fn main() {
    // Create parser with options
    let options = ParserOptions {
        struct_name: "User".to_string(),
        derive_serde: true,
        derive_default: false,
        make_fields_optional: false,
    };

    let mut parser = JsonParser::new(options);

    // Parse JSON
    let json = r#"{
        "id": 1,
        "name": "Alice",
        "email": "alice@example.com"
    }"#;

    let module = parser.parse(json).unwrap();

    println!("Parsed {} types", module.types.len());
}
```

#### 2. JsonParserBuilder

Fluent API for configuring parsers.

```rust
use unistructgen_json_parser::JsonParserBuilder;
use unistructgen_core::Parser;

fn main() {
    // Build parser with fluent API
    let mut parser = JsonParserBuilder::new()
        .struct_name("Product")
        .derive_serde(true)
        .derive_default(true)
        .make_optional(false)
        .build();

    let json = r#"{
        "id": "550e8400-e29b-41d4-a716-446655440000",
        "name": "Laptop",
        "price": 999.99
    }"#;

    let module = parser.parse(json).unwrap();

    // Module contains IR ready for code generation
}
```

#### 3. Smart Type Inference

Automatic detection of special types.

```rust
use unistructgen_json_parser::{
    JsonParser, ParserOptions,
    SmartTypeInference, TypeInferenceStrategy,
    UuidDetector, DateTimeDetector
};

fn main() {
    // Configure type inference
    let mut inference = SmartTypeInference::new();

    // Add custom detectors
    inference.add_detector(Box::new(UuidDetector));
    inference.add_detector(Box::new(DateTimeDetector));

    // Use with parser
    let options = ParserOptions {
        struct_name: "Event".to_string(),
        derive_serde: true,
        derive_default: false,
        make_fields_optional: false,
    };

    let mut parser = JsonParser::new(options);

    let json = r#"{
        "id": "550e8400-e29b-41d4-a716-446655440000",
        "timestamp": "2024-01-15T10:30:00Z",
        "message": "Event logged"
    }"#;

    let module = parser.parse(json).unwrap();

    // id -> uuid::Uuid
    // timestamp -> chrono::DateTime<Utc>
    // message -> String
}
```

### Real-World Example: Custom JSON Processor

```rust
use unistructgen_json_parser::{JsonParser, ParserOptions};
use unistructgen_core::Parser;
use std::fs;

/// Processes JSON files from a directory
fn process_json_directory(dir: &str, output_dir: &str) -> Result<(), Box<dyn std::error::Error>> {
    let entries = fs::read_dir(dir)?;

    for entry in entries {
        let entry = entry?;
        let path = entry.path();

        if path.extension().and_then(|s| s.to_str()) == Some("json") {
            // Read JSON file
            let json_content = fs::read_to_string(&path)?;

            // Create parser
            let struct_name = path
                .file_stem()
                .unwrap()
                .to_str()
                .unwrap()
                .to_string();

            let options = ParserOptions {
                struct_name: struct_name.clone(),
                derive_serde: true,
                derive_default: true,
                make_fields_optional: false,
            };

            let mut parser = JsonParser::new(options);

            // Parse
            match parser.parse(&json_content) {
                Ok(module) => {
                    println!("✓ Parsed {}: {} types", struct_name, module.types.len());

                    // Now you can pass module to code generator
                    // (see Codegen Module section)
                }
                Err(e) => {
                    eprintln!("✗ Failed to parse {}: {}", struct_name, e);
                }
            }
        }
    }

    Ok(())
}

fn main() {
    process_json_directory("./schemas", "./src/models").unwrap();
}
```

### Advanced: Custom Type Inference

```rust
use unistructgen_json_parser::{
    CustomTypeDetector, TypeInferenceStrategy, SmartTypeInference
};
use unistructgen_core::{IRTypeRef, PrimitiveKind};

/// Custom detector for email addresses
struct EmailDetector;

impl CustomTypeDetector for EmailDetector {
    fn detect(&self, value: &str) -> Option<IRTypeRef> {
        if value.contains('@') && value.contains('.') {
            // Could return a custom Email type
            // For now, just return String with validation hint
            Some(IRTypeRef::Primitive(PrimitiveKind::String))
        } else {
            None
        }
    }

    fn name(&self) -> &'static str {
        "EmailDetector"
    }
}

fn main() {
    let mut inference = SmartTypeInference::new();
    inference.add_detector(Box::new(EmailDetector));

    // Now emails will be detected
}
```

---

## 🎨 Codegen Module

**Package:** `unistructgen-codegen`

### What It Does

Transforms IR into beautiful, idiomatic Rust code.

### Why You Need It

**Use directly when:**
- Building custom code generation tools
- Generating code from IR programmatically
- Customizing output formatting
- Integrating with other tools

### Key Components

#### 1. RustRenderer

The main code generator for Rust.

```rust
use unistructgen_codegen::{RustRenderer, RenderOptions};
use unistructgen_core::{IRModule, IRType, IRStruct, IRField, IRTypeRef, PrimitiveKind};

fn main() {
    // Create IR module (from parser or manually)
    let mut module = IRModule::new("User".to_string());

    let mut user = IRStruct::new("User".to_string());
    user.add_derive("Debug".to_string());

    let id_field = IRField::new(
        "id".to_string(),
        IRTypeRef::Primitive(PrimitiveKind::I64)
    );
    user.add_field(id_field);

    module.add_type(IRType::Struct(user));

    // Create renderer
    let options = RenderOptions {
        add_header: true,
        add_clippy_allows: true,
    };

    let renderer = RustRenderer::new(options);

    // Generate code
    let rust_code = renderer.render(&module).unwrap();

    println!("{}", rust_code);
}
```

**Output:**
```rust
// Generated by unistructgen v0.1.0
// Do not edit this file manually

#![allow(dead_code)]
#![allow(unused_imports)]

#[derive(Debug)]
pub struct User {
    pub id: i64,
}
```

#### 2. RenderOptions

Customize code generation.

```rust
use unistructgen_codegen::{RustRenderer, RenderOptions};

// Minimal output (no header, no clippy allows)
let minimal = RustRenderer::new(RenderOptions {
    add_header: false,
    add_clippy_allows: false,
});

// Full output (with header and allows)
let full = RustRenderer::new(RenderOptions {
    add_header: true,
    add_clippy_allows: true,
});
```

### Real-World Example: Code Generation Pipeline

```rust
use unistructgen_json_parser::{JsonParser, ParserOptions};
use unistructgen_codegen::{RustRenderer, RenderOptions};
use unistructgen_core::Parser;
use std::fs;

/// Complete pipeline: JSON -> IR -> Rust code
fn generate_rust_from_json(
    json: &str,
    struct_name: &str,
    output_path: &str
) -> Result<(), Box<dyn std::error::Error>> {
    // Step 1: Parse JSON to IR
    let options = ParserOptions {
        struct_name: struct_name.to_string(),
        derive_serde: true,
        derive_default: true,
        make_fields_optional: false,
    };

    let mut parser = JsonParser::new(options);
    let module = parser.parse(json)?;

    println!("✓ Parsed {} types", module.types.len());

    // Step 2: Generate Rust code from IR
    let render_options = RenderOptions {
        add_header: true,
        add_clippy_allows: true,
    };

    let renderer = RustRenderer::new(render_options);
    let rust_code = renderer.render(&module)?;

    println!("✓ Generated {} lines of code", rust_code.lines().count());

    // Step 3: Write to file
    fs::write(output_path, rust_code)?;

    println!("✓ Written to {}", output_path);

    Ok(())
}

fn main() {
    let json = r#"{
        "id": "550e8400-e29b-41d4-a716-446655440000",
        "name": "John Doe",
        "email": "john@example.com",
        "createdAt": "2024-01-15T10:30:00Z"
    }"#;

    generate_rust_from_json(
        json,
        "User",
        "src/generated/user.rs"
    ).unwrap();
}
```

### Advanced: Batch Processing

```rust
use unistructgen_json_parser::JsonParser;
use unistructgen_codegen::RustRenderer;
use std::fs;
use std::path::Path;

/// Process multiple JSON schemas in parallel
fn batch_generate(
    input_dir: &str,
    output_dir: &str
) -> Result<(), Box<dyn std::error::Error>> {
    use rayon::prelude::*;

    let entries: Vec<_> = fs::read_dir(input_dir)?
        .filter_map(|e| e.ok())
        .filter(|e| {
            e.path().extension().and_then(|s| s.to_str()) == Some("json")
        })
        .collect();

    // Process in parallel
    entries.par_iter().for_each(|entry| {
        let path = entry.path();
        let struct_name = path.file_stem()
            .unwrap()
            .to_str()
            .unwrap();

        let json = fs::read_to_string(&path).unwrap();

        // Parse
        let options = unistructgen_json_parser::ParserOptions {
            struct_name: struct_name.to_string(),
            derive_serde: true,
            derive_default: false,
            make_fields_optional: false,
        };

        let mut parser = JsonParser::new(options);
        let module = parser.parse(&json).unwrap();

        // Generate
        let renderer = RustRenderer::new(
            unistructgen_codegen::RenderOptions {
                add_header: true,
                add_clippy_allows: true,
            }
        );

        let code = renderer.render(&module).unwrap();

        // Write
        let output_path = Path::new(output_dir)
            .join(format!("{}.rs", struct_name.to_lowercase()));

        fs::write(output_path, code).unwrap();

        println!("✓ Generated {}.rs", struct_name);
    });

    Ok(())
}

fn main() {
    batch_generate("./schemas", "./src/models").unwrap();
}
```

---

## ⚡ Proc-Macro Module

**Package:** `unistructgen-macro`

### What It Does

Provides **compile-time code generation** through procedural macros.

### Why You Need It

**Use when:**
- You want zero runtime overhead
- Schemas are known at compile time
- You want IDE autocomplete
- You're building Rust applications

### Key Macros

#### 1. generate_struct_from_json!

Function-like macro for inline JSON.

```rust
use unistructgen_macro::generate_struct_from_json;

// Basic usage
generate_struct_from_json! {
    name = "User",
    json = r#"{"id": 1, "name": "Alice"}"#
}

// With options
generate_struct_from_json! {
    name = "Config",
    json = r#"{"debug": true, "port": 8080}"#,
    serde = true,
    default = true,
    optional = false
}

fn main() {
    let user = User {
        id: 42,
        name: "Bob".to_string(),
    };

    let config = Config::default();
}
```

#### 2. struct_from_external_api!

Fetch from external APIs at compile time.

```rust
use unistructgen_macro::struct_from_external_api;

// Basic API call
struct_from_external_api! {
    struct_name = "User",
    url_api = "https://jsonplaceholder.typicode.com/users/1"
}

// With all options
struct_from_external_api! {
    struct_name = "Todo",
    url_api = "https://jsonplaceholder.typicode.com/todos",
    timeout = 10000,
    max_depth = 3,
    optional = false,
    default = true,
    serde = true
}

fn main() {
    let user = User { /* ... */ };
    let todo = Todo::default();
}
```

#### 3. #[json_struct]

Attribute macro for constants.

```rust
use unistructgen_macro::json_struct;

#[json_struct(name = "Config")]
const APP_CONFIG: &str = r#"{
    "database": {
        "host": "localhost",
        "port": 5432
    }
}"#;

fn main() {
    let config = Config {
        database: Database {
            host: "prod.example.com".to_string(),
            port: 5432,
        },
    };
}
```

### Real-World Example: Multi-API Integration

```rust
use unistructgen_macro::struct_from_external_api;

// GitHub API
struct_from_external_api! {
    struct_name = "GithubRepo",
    url_api = "https://api.github.com/repos/rust-lang/rust"
}

// JSONPlaceholder
struct_from_external_api! {
    struct_name = "Post",
    url_api = "https://jsonplaceholder.typicode.com/posts/1"
}

struct_from_external_api! {
    struct_name = "Comment",
    url_api = "https://jsonplaceholder.typicode.com/comments/1"
}

// Your API
#[cfg(not(debug_assertions))]
struct_from_external_api! {
    struct_name = "CustomData",
    url_api = "https://your-api.com/data"
}

#[cfg(debug_assertions)]
use unistructgen_macro::generate_struct_from_json;

#[cfg(debug_assertions)]
generate_struct_from_json! {
    name = "CustomData",
    json = r#"{"id": 1, "value": "dev"}"#
}

async fn fetch_all_data() {
    // All types are available and fully typed!
    let repo: GithubRepo = fetch_github_repo().await;
    let post: Post = fetch_post().await;
    let comment: Comment = fetch_comment().await;
    let data: CustomData = fetch_custom_data().await;
}
```

---

## 💻 CLI Module

**Package:** `unistructgen` (binary)

### What It Does

Command-line interface for generating code from schemas.

### Why You Need It

**Use when:**
- Building in CI/CD pipelines
- Pre-generating code
- Committing generated files
- Batch processing schemas

### Commands

#### Generate Command

```bash
# Basic usage
unistructgen generate \
    --input schema.json \
    --name User \
    --output user.rs

# With options
unistructgen generate \
    -i schema.json \
    -n User \
    -o user.rs \
    --serde true \
    --default true \
    --optional false

# From stdin
cat schema.json | unistructgen generate -n User

# To stdout
unistructgen generate -i schema.json -n User > user.rs
```

### Real-World Example: Build Script Integration

```rust
// build.rs
use std::process::Command;
use std::fs;

fn main() {
    println!("cargo:rerun-if-changed=schemas/");

    // Find all JSON schemas
    let entries = fs::read_dir("schemas").unwrap();

    for entry in entries {
        let entry = entry.unwrap();
        let path = entry.path();

        if path.extension().and_then(|s| s.to_str()) == Some("json") {
            let struct_name = path
                .file_stem()
                .unwrap()
                .to_str()
                .unwrap();

            let output_path = format!(
                "src/models/{}.rs",
                struct_name.to_lowercase()
            );

            // Generate code
            let status = Command::new("unistructgen")
                .args(&[
                    "generate",
                    "-i", path.to_str().unwrap(),
                    "-n", struct_name,
                    "-o", &output_path,
                    "--serde", "true",
                ])
                .status()
                .expect("Failed to run unistructgen");

            if !status.success() {
                panic!("Failed to generate {}", struct_name);
            }

            println!("✓ Generated {}", struct_name);
        }
    }
}
```

---

## 🚀 Advanced Patterns

### Pattern 1: Complete Custom Pipeline

Build your own tool using all modules:

```rust
use unistructgen_core::{Parser, IRModule};
use unistructgen_json_parser::{JsonParser, ParserOptions};
use unistructgen_codegen::{RustRenderer, RenderOptions};
use std::fs;

struct CodeGenerator {
    parser: JsonParser,
    renderer: RustRenderer,
}

impl CodeGenerator {
    fn new(struct_name: &str) -> Self {
        let parser_options = ParserOptions {
            struct_name: struct_name.to_string(),
            derive_serde: true,
            derive_default: true,
            make_fields_optional: false,
        };

        let render_options = RenderOptions {
            add_header: true,
            add_clippy_allows: true,
        };

        Self {
            parser: JsonParser::new(parser_options),
            renderer: RustRenderer::new(render_options),
        }
    }

    fn generate_from_json(&mut self, json: &str) -> Result<String, Box<dyn std::error::Error>> {
        // Parse
        let module = self.parser.parse(json)?;

        // Generate
        let code = self.renderer.render(&module)?;

        Ok(code)
    }

    fn generate_from_file(
        &mut self,
        input_path: &str,
        output_path: &str
    ) -> Result<(), Box<dyn std::error::Error>> {
        let json = fs::read_to_string(input_path)?;
        let code = self.generate_from_json(&json)?;
        fs::write(output_path, code)?;
        Ok(())
    }
}

fn main() {
    let mut generator = CodeGenerator::new("User");

    generator.generate_from_file(
        "schemas/user.json",
        "src/models/user.rs"
    ).unwrap();
}
```

### Pattern 2: Watch Mode

Auto-regenerate on file changes:

```rust
use notify::{Watcher, RecursiveMode, watcher};
use std::sync::mpsc::channel;
use std::time::Duration;

fn watch_and_generate(schema_dir: &str, output_dir: &str) {
    let (tx, rx) = channel();

    let mut watcher = watcher(tx, Duration::from_secs(2)).unwrap();
    watcher.watch(schema_dir, RecursiveMode::Recursive).unwrap();

    println!("👀 Watching {} for changes...", schema_dir);

    loop {
        match rx.recv() {
            Ok(event) => {
                println!("🔄 Change detected, regenerating...");

                // Regenerate all schemas
                // (use batch_generate from earlier example)

                println!("✓ Regeneration complete");
            }
            Err(e) => println!("Watch error: {:?}", e),
        }
    }
}
```

### Pattern 3: Custom Transformations

Transform IR before code generation:

```rust
use unistructgen_core::{IRModule, IRType, IRField};

fn add_custom_derives(mut module: IRModule) -> IRModule {
    for ty in &mut module.types {
        if let IRType::Struct(ref mut struct_def) = ty {
            // Add custom derives
            struct_def.add_derive("PartialEq".to_string());
            struct_def.add_derive("Eq".to_string());
            struct_def.add_derive("Hash".to_string());
        }
    }
    module
}

fn add_id_fields(mut module: IRModule) -> IRModule {
    use unistructgen_core::{IRTypeRef, PrimitiveKind};

    for ty in &mut module.types {
        if let IRType::Struct(ref mut struct_def) = ty {
            // Check if struct has ID field
            let has_id = struct_def.fields.iter()
                .any(|f| f.name == "id");

            if !has_id {
                // Add UUID id field
                let id_field = IRField::new(
                    "id".to_string(),
                    IRTypeRef::Primitive(PrimitiveKind::Uuid)
                );
                struct_def.fields.insert(0, id_field);
            }
        }
    }
    module
}

fn main() {
    // Parse JSON
    let mut parser = JsonParser::new(/* ... */);
    let module = parser.parse(json).unwrap();

    // Transform IR
    let module = add_custom_derives(module);
    let module = add_id_fields(module);

    // Generate code
    let renderer = RustRenderer::new(/* ... */);
    let code = renderer.render(&module).unwrap();
}
```

---

## 🛠️ Creating Custom Parsers

Want to parse other formats? Here's how:

### Step 1: Implement Parser Trait

```rust
use unistructgen_core::{Parser, ParserMetadata, IRModule};
use thiserror::Error;

#[derive(Error, Debug)]
pub enum YamlParserError {
    #[error("YAML syntax error: {0}")]
    SyntaxError(String),
}

pub struct YamlParser {
    struct_name: String,
}

impl Parser for YamlParser {
    type Error = YamlParserError;

    fn parse(&mut self, input: &str) -> Result<IRModule, Self::Error> {
        // Parse YAML to IR
        // Similar to JsonParser implementation
        todo!("Implement YAML parsing")
    }

    fn name(&self) -> &'static str {
        "YAML"
    }

    fn extensions(&self) -> &[&'static str] {
        &["yaml", "yml"]
    }

    fn validate(&self, input: &str) -> Result<(), Self::Error> {
        // Validate YAML syntax
        todo!()
    }

    fn metadata(&self) -> ParserMetadata {
        ParserMetadata::new()
            .with_version("0.1.0")
            .with_description("YAML parser")
    }
}
```

### Step 2: Use Your Parser

```rust
fn main() {
    let yaml = r#"
user:
  id: 1
  name: Alice
  email: alice@example.com
"#;

    let mut parser = YamlParser {
        struct_name: "User".to_string(),
    };

    let module = parser.parse(yaml).unwrap();

    // Use with existing code generator!
    let renderer = RustRenderer::new(RenderOptions::default());
    let code = renderer.render(&module).unwrap();
}
```

---

## 📚 Summary

### Module Dependencies

```
proc-macro ──────┐
                 ├──> json_parser ──> core
cli ─────────────┤                      │
                 └──> codegen ──────────┘
```

### When to Use Each Module

| Module | Use When | Don't Use When |
|--------|----------|----------------|
| **proc-macro** | Compile-time gen, known schemas | Runtime gen, unknown schemas |
| **json_parser** | Custom tools, runtime parsing | Just need macros |
| **codegen** | Custom output, multiple languages | Just generating Rust |
| **core** | Building extensions, custom parsers | Just using the library |
| **CLI** | Build scripts, CI/CD | Compile-time generation |

### Quick Decision Tree

```
Need to generate code?
├─ At compile time?
│  └─ Yes → Use proc-macro module
│      └─ generate_struct_from_json! or struct_from_external_api!
│
├─ At build time?
│  └─ Yes → Use CLI module
│      └─ unistructgen generate
│
└─ Programmatically/Custom tool?
   └─ Yes → Use json_parser + codegen modules
       └─ Build custom pipeline
```

---

<div align="center">

**[⬅️ Back to README](README.md)** • **[📖 Quick Start](QUICKSTART.md)** • **[✨ Features](FEATURES.md)**

Master every module of UniStructGen

Made with 🦀 for extensibility and power

</div>