facet-styx 3.0.0

Facet integration for the Styx configuration language
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
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
//! Schema generation from Facet types.
//!
//! This module provides utilities for generating Styx schemas from Rust types
//! that implement `Facet`.

use facet_core::{
    Def, DefaultSource, Facet, Field, NumericType, PrimitiveType, PtrConst, PtrMut, PtrUninit,
    Shape, ShapeLayout, Type, UserType,
};
use facet_reflect::Peek;
use std::collections::{HashMap, HashSet};
use std::marker::PhantomData;
use std::path::Path;
use std::ptr::NonNull;

use crate::peek_to_string_expr;
use crate::schema_types::{
    DefaultSchema, Documented, EnumSchema, LspExtensionConfig, MapSchema, Meta, ObjectKey,
    ObjectSchema, OptionalSchema, RawStyx, Schema, SchemaFile, SeqSchema, TupleSchema,
};

/// Strip exactly one leading space from a doc line if present.
///
/// Rust doc comments like `/// text` produce doc strings with a leading space: `" text"`.
/// This function normalizes them to `"text"`.
fn strip_doc_leading_space(s: &str) -> String {
    s.strip_prefix(' ').unwrap_or(s).to_string()
}

/// Try to get the default value for a field as a styx expression string.
/// Returns None if the field has no default or if serialization fails.
fn field_default_value(field: &Field) -> Option<String> {
    let default_source = field.default?;
    let shape = field.shape();

    // Get layout
    let layout = match shape.layout {
        ShapeLayout::Sized(l) => l,
        ShapeLayout::Unsized => return None,
    };

    if layout.size() == 0 {
        // Zero-sized type - return unit
        return Some("@".to_string());
    }

    // Allocate memory for the value
    let ptr = unsafe { std::alloc::alloc(layout) };
    if ptr.is_null() {
        return None;
    }
    let ptr = unsafe { NonNull::new_unchecked(ptr) };

    // Initialize with the default value
    let ptr_uninit = PtrUninit::new(ptr.as_ptr());
    match default_source {
        DefaultSource::Custom(default_fn) => {
            unsafe { default_fn(ptr_uninit) };
        }
        DefaultSource::FromTrait => {
            if unsafe { shape.call_default_in_place(ptr_uninit) }.is_none() {
                unsafe { std::alloc::dealloc(ptr.as_ptr(), layout) };
                return None;
            }
        }
    }

    // Create a Peek to serialize
    let ptr_const = PtrConst::new(ptr.as_ptr());
    let peek = unsafe { Peek::unchecked_new(ptr_const, shape) };

    // Serialize to styx expression string (with braces for objects)
    let styx_str = peek_to_string_expr(peek).ok()?;

    // Drop the value and free memory
    unsafe {
        shape.call_drop_in_place(PtrMut::new(ptr.as_ptr()));
        std::alloc::dealloc(ptr.as_ptr(), layout);
    }

    Some(styx_str)
}

/// Builder for generating Styx schemas from Facet types.
///
/// Use in build scripts to generate schema files:
///
/// ```rust,ignore
/// // build.rs
/// fn main() {
///     facet_styx::GenerateSchema::<MyConfig>::new()
///         .crate_name("myapp-config")
///         .version("1")
///         .cli("myapp")
///         .write("schema.styx");
/// }
/// ```
///
/// The generated schema can then be embedded:
///
/// ```rust,ignore
/// // src/main.rs
/// styx_embed::embed_outdir_file!("schema.styx");
/// ```
pub struct GenerateSchema<T: facet_core::Facet<'static>> {
    crate_name: Option<String>,
    version: Option<String>,
    cli: Option<String>,
    lsp: Option<LspExtensionConfig>,
    _marker: PhantomData<T>,
}

impl<T: facet_core::Facet<'static>> GenerateSchema<T> {
    /// Create a new schema generator.
    pub fn new() -> Self {
        Self {
            crate_name: None,
            version: None,
            cli: None,
            lsp: None,
            _marker: PhantomData,
        }
    }

    /// Set the crate name for the schema ID.
    pub fn crate_name(mut self, name: impl Into<String>) -> Self {
        self.crate_name = Some(name.into());
        self
    }

    /// Set the version for the schema ID.
    pub fn version(mut self, version: impl Into<String>) -> Self {
        self.version = Some(version.into());
        self
    }

    /// Set the CLI binary name.
    pub fn cli(mut self, cli: impl Into<String>) -> Self {
        self.cli = Some(cli.into());
        self
    }

    /// Set the LSP extension configuration.
    ///
    /// When set, the generated schema will include this configuration in `meta.lsp`.
    /// The Styx LSP will spawn this extension process to provide domain-specific
    /// completions, hover info, and diagnostics.
    ///
    /// # Example
    ///
    /// ```ignore
    /// GenerateSchema::<Config>::new()
    ///     .crate_name("my-tool")
    ///     .version("1")
    ///     .cli("my-tool")
    ///     .lsp(LspExtensionConfig {
    ///         launch: vec!["my-tool".into(), "lsp-extension".into()],
    ///         capabilities: None,
    ///     })
    ///     .write("config.styx");
    /// ```
    pub fn lsp(mut self, lsp: LspExtensionConfig) -> Self {
        self.lsp = Some(lsp);
        self
    }

    /// Write the schema to `$OUT_DIR/{filename}`.
    pub fn write(self, filename: &str) {
        let out_dir =
            std::env::var("OUT_DIR").expect("OUT_DIR not set - are you in a build script?");
        let path = Path::new(&out_dir).join(filename);

        let schema = self.generate();
        std::fs::write(&path, schema).expect("failed to write schema");
    }

    /// Generate the schema as a string.
    pub fn generate(self) -> String {
        let schema_file = self.generate_schema_file();
        crate::to_string(&schema_file).expect("failed to serialize schema")
    }

    /// Generate the schema as a SchemaFile struct.
    ///
    /// This is more efficient than `generate()` when you need to inspect
    /// the schema programmatically, as it avoids the string serialization.
    pub fn generate_schema_file(self) -> SchemaFile {
        let crate_name = self
            .crate_name
            .expect("crate_name is required - call .crate_name(\"...\")");
        let version = self
            .version
            .expect("version is required - call .version(\"...\")");

        let id = format!("crate:{crate_name}@{version}");
        generate_schema_file_inner::<T>(id, self.cli, self.lsp)
    }
}

impl<T: facet_core::Facet<'static>> Default for GenerateSchema<T> {
    fn default() -> Self {
        Self::new()
    }
}

/// Generate a Styx schema string from a Facet type.
pub fn schema_from_type<T: facet_core::Facet<'static>>() -> String {
    let schema_file = schema_file_from_type::<T>();
    crate::to_string(&schema_file).expect("failed to serialize schema")
}

/// Generate a SchemaFile directly from a Facet type.
///
/// This is more efficient than `schema_from_type` when you need to inspect
/// the schema programmatically, as it avoids the string serialization and
/// parsing round-trip.
pub fn schema_file_from_type<T: facet_core::Facet<'static>>() -> SchemaFile {
    let shape = T::SHAPE;
    let id = shape.type_identifier.to_string();
    generate_schema_file_inner::<T>(id, None, None)
}

/// Internal function that generates a SchemaFile with the given id and optional cli/lsp.
fn generate_schema_file_inner<T: facet_core::Facet<'static>>(
    id: String,
    cli: Option<String>,
    lsp: Option<LspExtensionConfig>,
) -> SchemaFile {
    let shape = T::SHAPE;

    let mut generator = SchemaGenerator::new();

    // Generate the root schema - inline it directly at @ (None key)
    // Use generate_type_definition to inline the root, while nested types get references
    let root_schema = generator
        .generate_type_definition(shape)
        .unwrap_or_else(|| generator.shape_to_schema(shape));

    // Build the schema map with root and all named type definitions
    let mut schema_map: HashMap<Option<String>, Schema> = HashMap::new();
    schema_map.insert(None, root_schema);

    // Process all pending types (types that were referenced but need definitions)
    while let Some(pending_shape) = generator.take_pending() {
        // Schema is a well-known built-in type - don't generate its definition
        if pending_shape == Schema::SHAPE {
            continue;
        }

        let type_name = pending_shape.type_identifier.to_string();
        // Only add if not already defined
        if !schema_map.contains_key(&Some(type_name.clone()))
            && let Some(type_schema) = generator.generate_type_definition(pending_shape)
        {
            schema_map.insert(Some(type_name), type_schema);
        }
    }

    let description = if shape.doc.is_empty() {
        None
    } else {
        Some(
            shape
                .doc
                .iter()
                .map(|s| s.trim())
                .collect::<Vec<_>>()
                .join(" "),
        )
    };

    SchemaFile {
        meta: Meta {
            id,
            version: None,
            cli,
            description,
            lsp,
        },
        imports: None,
        schema: schema_map,
    }
}

/// Convert a Schema to a tag name for use in ObjectKey.
///
/// Maps built-in types to their tag names:
/// - Schema::String(_) → "string"
/// - Schema::Int(_) → "int"
/// - Schema::Float(_) → "float"
/// - Schema::Bool → "bool"
/// - Schema::Unit → "unit"
/// - Schema::Any → "any"
/// - Schema::Type { name } → the type name
/// - Complex types → serialized form (for future extensibility)
fn schema_to_tag_name(schema: &Schema) -> String {
    match schema {
        Schema::String(_) => "string".to_string(),
        Schema::Int(_) => "int".to_string(),
        Schema::Float(_) => "float".to_string(),
        Schema::Bool => "bool".to_string(),
        Schema::Unit => "unit".to_string(),
        Schema::Any => "any".to_string(),
        Schema::Type { name } => name.clone().unwrap_or_default(),
        // For complex types, we could serialize them, but for now use "any"
        // This handles cases like @object, @seq, @map, etc. being used as keys
        _ => "any".to_string(),
    }
}

/// Internal schema generator that builds typed Schema structs.
struct SchemaGenerator {
    /// Types currently being generated (for cycle detection)
    generating: HashSet<&'static str>,
    /// Types that have been queued for definition
    queued_types: HashSet<&'static str>,
    /// Types pending generation (shapes to process)
    pending_types: Vec<&'static Shape>,
}

impl SchemaGenerator {
    fn new() -> Self {
        Self {
            generating: HashSet::new(),
            queued_types: HashSet::new(),
            pending_types: Vec::new(),
        }
    }

    /// Queue a type for definition if not already queued.
    fn queue_type(&mut self, shape: &'static Shape) {
        // Schema is a well-known built-in type - don't queue it or its dependencies
        if shape == Schema::SHAPE {
            return;
        }

        let type_id = shape.type_identifier;
        if !self.queued_types.contains(type_id) {
            self.queued_types.insert(type_id);
            self.pending_types.push(shape);
        }
    }

    /// Take the next pending type to generate, if any.
    fn take_pending(&mut self) -> Option<&'static Shape> {
        self.pending_types.pop()
    }

    /// Generate the schema definition for a user type (struct or enum).
    /// This is used when generating named type definitions.
    fn generate_type_definition(&mut self, shape: &'static Shape) -> Option<Schema> {
        match &shape.ty {
            Type::User(user) => {
                let type_id = shape.type_identifier;
                self.generating.insert(type_id);
                let result = match user {
                    UserType::Struct(struct_type) => Some(self.struct_to_schema(struct_type)),
                    UserType::Enum(enum_type) => Some(self.enum_to_schema(enum_type)),
                    _ => None,
                };
                self.generating.remove(type_id);
                result
            }
            _ => None,
        }
    }

    /// Convert a shape to a Schema.
    fn shape_to_schema(&mut self, shape: &'static Shape) -> Schema {
        // Handle metadata containers (like Documented<T>) - unwrap to inner value type
        if let Some(inner_shape) = facet_reflect::get_metadata_container_value_shape(shape) {
            return self.shape_to_schema(inner_shape);
        }

        match &shape.def {
            Def::Scalar => self.scalar_to_schema(shape),
            Def::Option(opt_def) => {
                let inner = self.shape_to_schema(opt_def.t);
                Schema::Optional(OptionalSchema((Documented::new(Box::new(inner)),)))
            }
            Def::List(list_def) => {
                let inner = self.shape_to_schema(list_def.t);
                Schema::Seq(SeqSchema((Documented::new(Box::new(inner)),)))
            }
            Def::Array(array_def) => {
                let inner = self.shape_to_schema(array_def.t);
                Schema::Seq(SeqSchema((Documented::new(Box::new(inner)),)))
            }
            Def::Map(map_def) => {
                let key = self.shape_to_schema(map_def.k);
                let value = self.shape_to_schema(map_def.v);
                Schema::Map(MapSchema(vec![
                    Documented::new(key),
                    Documented::new(value),
                ]))
            }
            Def::Set(set_def) => {
                let inner = self.shape_to_schema(set_def.t);
                Schema::Seq(SeqSchema((Documented::new(Box::new(inner)),)))
            }
            Def::Result(result_def) => {
                let ok = self.shape_to_schema(result_def.t);
                let err = self.shape_to_schema(result_def.e);
                let mut variants = HashMap::new();
                variants.insert(Documented::new("ok".to_string()), ok);
                variants.insert(Documented::new("err".to_string()), err);
                Schema::Enum(EnumSchema(variants))
            }
            Def::Pointer(ptr_def) => {
                if let Some(pointee) = ptr_def.pointee {
                    self.shape_to_schema(pointee)
                } else {
                    Schema::Any
                }
            }
            Def::Slice(slice_def) => {
                let inner = self.shape_to_schema(slice_def.t);
                Schema::Seq(SeqSchema((Documented::new(Box::new(inner)),)))
            }
            Def::Undefined | Def::NdArray(_) | Def::DynamicValue(_) => self.type_to_schema(shape),
            _ => self.type_to_schema(shape),
        }
    }

    fn type_to_schema(&mut self, shape: &'static Shape) -> Schema {
        match &shape.ty {
            Type::Primitive(prim) => self.primitive_to_schema(prim),
            Type::User(user) => self.user_type_to_schema(user, shape),
            Type::Sequence(seq) => {
                use facet_core::SequenceType;
                match seq {
                    SequenceType::Array(arr) => {
                        let inner = self.shape_to_schema(arr.t);
                        Schema::Seq(SeqSchema((Documented::new(Box::new(inner)),)))
                    }
                    SequenceType::Slice(slice) => {
                        let inner = self.shape_to_schema(slice.t);
                        Schema::Seq(SeqSchema((Documented::new(Box::new(inner)),)))
                    }
                }
            }
            Type::Pointer(_) | Type::Undefined => Schema::Any,
        }
    }

    fn scalar_to_schema(&self, shape: &'static Shape) -> Schema {
        match &shape.ty {
            Type::Primitive(prim) => self.primitive_to_schema(prim),
            Type::User(UserType::Opaque) => {
                let type_id = shape.type_identifier;
                match type_id {
                    "String" | "str" | "Cow" | "PathBuf" | "Path" | "OsString" | "OsStr"
                    | "Url" | "Uri" | "Uuid" | "Duration" | "SystemTime" | "Instant" | "IpAddr"
                    | "Ipv4Addr" | "Ipv6Addr" | "SocketAddr" | "SocketAddrV4" | "SocketAddrV6" => {
                        Schema::String(None)
                    }
                    _ => Schema::Type {
                        name: Some(type_id.to_string()),
                    },
                }
            }
            _ => Schema::Any,
        }
    }

    fn primitive_to_schema(&self, prim: &PrimitiveType) -> Schema {
        match prim {
            PrimitiveType::Boolean => Schema::Bool,
            PrimitiveType::Numeric(num) => match num {
                NumericType::Integer { .. } => Schema::Int(None),
                NumericType::Float => Schema::Float(None),
            },
            PrimitiveType::Textual(_) => Schema::String(None),
            PrimitiveType::Never => Schema::Unit,
        }
    }

    fn user_type_to_schema(&mut self, user: &UserType, shape: &'static Shape) -> Schema {
        let type_id = shape.type_identifier;

        // Schema is a well-known built-in type - emit a reference without generating its definition.
        // This avoids inlining the entire Schema enum (which describes types) into generated schemas.
        if shape == Schema::SHAPE {
            return Schema::Type {
                name: Some("Schema".to_string()),
            };
        }

        // Cycle detection - if we're already generating this type, return a reference
        if self.generating.contains(type_id) {
            self.queue_type(shape);
            return Schema::Type {
                name: Some(type_id.to_string()),
            };
        }

        match user {
            // For structs and enums, always emit a type reference and queue for definition
            // This gives all complex types their own named definitions
            UserType::Struct(_) | UserType::Enum(_) => {
                self.queue_type(shape);
                Schema::Type {
                    name: Some(type_id.to_string()),
                }
            }
            UserType::Union(_) => Schema::Any,
            UserType::Opaque => match type_id {
                "String" | "str" | "&str" | "Cow" | "PathBuf" | "Path" => Schema::String(None),
                _ => Schema::Type {
                    name: Some(type_id.to_string()),
                },
            },
        }
    }

    fn struct_to_schema(&mut self, struct_type: &facet_core::StructType) -> Schema {
        use facet_core::StructKind;

        match struct_type.kind {
            StructKind::Unit => Schema::Unit,
            StructKind::Tuple | StructKind::TupleStruct => {
                if struct_type.fields.len() == 1 {
                    // Newtype - unwrap
                    self.shape_to_schema(struct_type.fields[0].shape())
                } else {
                    // Tuple with multiple fields - each position has a distinct type
                    let elements: Vec<Documented<Schema>> = struct_type
                        .fields
                        .iter()
                        .map(|field| Documented::new(self.shape_to_schema(field.shape())))
                        .collect();
                    Schema::Tuple(TupleSchema(elements))
                }
            }
            StructKind::Struct => {
                let mut fields: HashMap<Documented<ObjectKey>, Schema> = HashMap::new();

                for field in struct_type.fields {
                    let field_name = field.effective_name();
                    let field_schema = self.shape_to_schema(field.shape());

                    // Extract doc comments from field
                    let doc = if field.doc.is_empty() {
                        None
                    } else {
                        Some(
                            field
                                .doc
                                .iter()
                                .map(|s| strip_doc_leading_space(s))
                                .collect(),
                        )
                    };

                    // Handle flattened fields - inline their contents
                    if field.is_flattened() {
                        match field_schema {
                            // Flattened map: add catch-all entry with the value type
                            Schema::Map(MapSchema(types)) => {
                                // @map(@V) has 1 type, @map(@K @V) has 2
                                // Get key type tag and value schema
                                let (key_tag, value_schema) = if types.len() == 1 {
                                    // Only value type, key defaults to @string
                                    (
                                        "string".to_string(),
                                        types.into_iter().next().unwrap().value,
                                    )
                                } else {
                                    // Key and value types
                                    let mut iter = types.into_iter();
                                    let key_schema = iter.next().unwrap().value;
                                    let value_schema = iter.next().unwrap().value;
                                    // Convert key schema to tag name
                                    let key_tag = schema_to_tag_name(&key_schema);
                                    (key_tag, value_schema)
                                };
                                let key = Documented {
                                    value: ObjectKey::typed(key_tag),
                                    doc,
                                };
                                fields.insert(key, value_schema);
                            }
                            // Flattened object: merge its fields into parent
                            Schema::Object(ObjectSchema(inner_fields)) => {
                                for (inner_key, inner_schema) in inner_fields {
                                    fields.insert(inner_key, inner_schema);
                                }
                            }
                            // Other flattened types: use unit catch-all
                            other => {
                                let key = Documented {
                                    value: ObjectKey::unit(),
                                    doc,
                                };
                                fields.insert(key, other);
                            }
                        }
                        continue;
                    }

                    let mut field_schema = field_schema;

                    // Wrap with @default if field has a default value
                    if let Some(default_value_str) = field_default_value(field) {
                        let default_value = RawStyx::new(default_value_str);
                        field_schema = Schema::Default(DefaultSchema((
                            default_value,
                            Documented::new(Box::new(field_schema)),
                        )));
                    }

                    // Handle catch-all field (empty name) vs regular named field
                    let key = if field_name.is_empty() {
                        Documented {
                            value: ObjectKey::unit(),
                            doc,
                        }
                    } else {
                        Documented {
                            value: ObjectKey::named(field_name),
                            doc,
                        }
                    };

                    fields.insert(key, field_schema);
                }

                Schema::Object(ObjectSchema(fields))
            }
        }
    }

    fn enum_to_schema(&mut self, enum_type: &facet_core::EnumType) -> Schema {
        use facet_core::StructKind;

        // If any variant has #[facet(other)], this enum accepts any tag,
        // so emit @any instead of trying to enumerate variants
        if enum_type.variants.iter().any(|v| v.is_other()) {
            return Schema::Any;
        }

        let mut variants: HashMap<Documented<String>, Schema> = HashMap::new();

        for variant in enum_type.variants {
            let variant_name = variant.effective_name().to_string();
            let variant_schema = match variant.data.kind {
                StructKind::Unit => Schema::Unit,
                StructKind::Tuple | StructKind::TupleStruct => {
                    if variant.data.fields.len() == 1 {
                        self.shape_to_schema(variant.data.fields[0].shape())
                    } else {
                        // Tuple variant with multiple fields
                        let elements: Vec<Documented<Schema>> = variant
                            .data
                            .fields
                            .iter()
                            .map(|field| Documented::new(self.shape_to_schema(field.shape())))
                            .collect();
                        Schema::Tuple(TupleSchema(elements))
                    }
                }
                StructKind::Struct => self.struct_to_schema(&variant.data),
            };

            // Extract doc comments from variant
            let doc = if variant.doc.is_empty() {
                None
            } else {
                Some(
                    variant
                        .doc
                        .iter()
                        .map(|s| strip_doc_leading_space(s))
                        .collect(),
                )
            };

            variants.insert(
                Documented {
                    value: variant_name,
                    doc,
                },
                variant_schema,
            );
        }

        Schema::Enum(EnumSchema(variants))
    }
}

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

    #[test]
    fn test_simple_struct() {
        #[derive(Facet)]
        #[allow(dead_code)]
        struct Config {
            name: String,
            port: u16,
        }

        let schema = schema_from_type::<Config>();
        tracing::debug!("Generated schema:\n{schema}");
        assert!(schema.contains("meta"));
        assert!(schema.contains("name"));
        assert!(schema.contains("port"));
    }

    #[test]
    fn test_with_option() {
        #[derive(Facet)]
        #[allow(dead_code)]
        struct Config {
            name: String,
            debug: Option<bool>,
        }

        let schema = schema_from_type::<Config>();
        tracing::debug!("Generated schema:\n{schema}");
        assert!(schema.contains("debug"));
        assert!(schema.contains("optional"));
    }

    #[test]
    fn test_with_vec() {
        #[derive(Facet)]
        #[allow(dead_code)]
        struct Config {
            items: Vec<String>,
        }

        let schema = schema_from_type::<Config>();
        tracing::debug!("Generated schema:\n{schema}");
        assert!(schema.contains("items"));
        assert!(schema.contains("seq"));
    }

    #[test]
    fn test_with_default() {
        #[derive(Facet)]
        #[allow(dead_code)]
        struct Config {
            name: String,
            #[facet(default = 8080)]
            port: u16,
        }

        let schema = schema_from_type::<Config>();
        tracing::debug!("Generated schema:\n{schema}");
        assert!(schema.contains("default"));
        assert!(schema.contains("8080"));
    }

    #[test]
    fn test_with_nested_default() {
        #[derive(Facet, Default)]
        #[allow(dead_code)]
        struct Inner {
            #[facet(default = true)]
            enabled: bool,
            #[facet(default = 8080)]
            port: u16,
        }

        #[derive(Facet)]
        #[allow(dead_code)]
        struct Config {
            #[facet(default)]
            inner: Inner,
        }

        let schema = schema_from_type::<Config>();
        tracing::debug!("Generated schema:\n{schema}");
        // The nested default should be serialized with braces
        assert!(schema.contains("default"));
        assert!(schema.contains("enabled"));
    }

    // =========================================================================
    // Roundtrip tests - parse generated schema back into SchemaFile
    // =========================================================================

    #[test]
    fn test_roundtrip_simple_struct() {
        #[derive(Facet)]
        #[allow(dead_code)]
        struct Config {
            name: String,
            port: u16,
        }

        let schema_str = schema_from_type::<Config>();
        tracing::debug!("Generated schema:\n{schema_str}");

        // Parse it back
        let parsed: SchemaFile =
            crate::from_str(&schema_str).expect("failed to parse generated schema");

        // Verify structure
        assert_eq!(parsed.meta.id, "Config");
        assert!(
            parsed.schema.contains_key(&None),
            "should have root schema with None key"
        );

        let root_schema = parsed.schema.get(&None).expect("missing root schema");
        if let Schema::Object(obj) = root_schema {
            assert!(
                obj.0
                    .contains_key(&Documented::new(ObjectKey::named("name")))
            );
            assert!(
                obj.0
                    .contains_key(&Documented::new(ObjectKey::named("port")))
            );
        } else {
            panic!("expected root schema to be Object, got {:?}", root_schema);
        }
    }

    #[test]
    fn test_roundtrip_with_option() {
        #[derive(Facet)]
        #[allow(dead_code)]
        struct Config {
            name: String,
            debug: Option<bool>,
        }

        let schema_str = schema_from_type::<Config>();
        tracing::debug!("Generated schema:\n{schema_str}");

        let parsed: SchemaFile =
            crate::from_str(&schema_str).expect("failed to parse generated schema");

        let root_schema = parsed.schema.get(&None).expect("missing root schema");
        if let Schema::Object(obj) = root_schema {
            let debug_schema = obj
                .0
                .get(&Documented::new(ObjectKey::named("debug")))
                .expect("missing debug field");
            assert!(
                matches!(debug_schema, Schema::Optional(_)),
                "debug should be Optional, got {:?}",
                debug_schema
            );
        } else {
            panic!("expected root schema to be Object");
        }
    }

    #[test]
    fn test_roundtrip_with_vec() {
        #[derive(Facet)]
        #[allow(dead_code)]
        struct Config {
            items: Vec<String>,
        }

        let schema_str = schema_from_type::<Config>();
        tracing::debug!("Generated schema:\n{schema_str}");

        let parsed: SchemaFile =
            crate::from_str(&schema_str).expect("failed to parse generated schema");

        let root_schema = parsed.schema.get(&None).expect("missing root schema");
        if let Schema::Object(obj) = root_schema {
            let items_schema = obj
                .0
                .get(&Documented::new(ObjectKey::named("items")))
                .expect("missing items field");
            assert!(
                matches!(items_schema, Schema::Seq(_)),
                "items should be Seq, got {:?}",
                items_schema
            );
        } else {
            panic!("expected root schema to be Object");
        }
    }

    #[test]
    fn test_roundtrip_with_default() {
        #[derive(Facet)]
        #[allow(dead_code)]
        struct Config {
            name: String,
            #[facet(default = 8080)]
            port: u16,
        }

        let schema_str = schema_from_type::<Config>();
        tracing::debug!("Generated schema:\n{schema_str}");

        let parsed: SchemaFile =
            crate::from_str(&schema_str).expect("failed to parse generated schema");

        let root_schema = parsed.schema.get(&None).expect("missing root schema");
        if let Schema::Object(obj) = root_schema {
            let port_schema = obj
                .0
                .get(&Documented::new(ObjectKey::named("port")))
                .expect("missing port field");
            if let Schema::Default(default_schema) = port_schema {
                assert_eq!(
                    default_schema.0.0.as_str(),
                    "8080",
                    "default value should be 8080"
                );
            } else {
                panic!("port should be Default, got {:?}", port_schema);
            }
        } else {
            panic!("expected root schema to be Object");
        }
    }

    #[test]
    fn test_roundtrip_with_nested_default() {
        #[derive(Facet, Default)]
        #[allow(dead_code)]
        struct Inner {
            #[facet(default = true)]
            enabled: bool,
            #[facet(default = 8080)]
            port: u16,
        }

        #[derive(Facet)]
        #[allow(dead_code)]
        struct Config {
            inner: Inner,
        }

        let schema_str = schema_from_type::<Config>();
        tracing::debug!("Generated schema:\n{schema_str}");

        let parsed: SchemaFile =
            crate::from_str(&schema_str).expect("failed to parse generated schema");

        // Root schema should reference @Inner
        let root_schema = parsed.schema.get(&None).expect("missing root schema");
        if let Schema::Object(obj) = root_schema {
            let inner_schema = obj
                .0
                .get(&Documented::new(ObjectKey::named("inner")))
                .expect("missing inner field");
            // Inner should be a type reference now
            assert!(
                matches!(inner_schema, Schema::Type { name: Some(n) } if n == "Inner"),
                "inner should be Type reference, got {:?}",
                inner_schema
            );
        } else {
            panic!("expected root schema to be Object");
        }

        // Inner type should have its own definition with defaults
        let inner_def = parsed
            .schema
            .get(&Some("Inner".to_string()))
            .expect("missing Inner type definition");
        if let Schema::Object(inner_obj) = inner_def {
            // Check that the inner object has fields with @default wrappers
            let enabled_schema = inner_obj
                .0
                .get(&Documented::new(ObjectKey::named("enabled")))
                .expect("missing enabled field");
            assert!(
                matches!(enabled_schema, Schema::Default(_)),
                "enabled should have @default wrapper"
            );
            let port_schema = inner_obj
                .0
                .get(&Documented::new(ObjectKey::named("port")))
                .expect("missing port field");
            assert!(
                matches!(port_schema, Schema::Default(_)),
                "port should have @default wrapper"
            );
        } else {
            panic!("Inner should be Object, got {:?}", inner_def);
        }
    }

    #[test]
    fn test_schema_uses_at_for_root_key() {
        #[derive(Facet)]
        #[allow(dead_code)]
        struct Config {
            name: String,
        }

        let schema_str = schema_from_type::<Config>();
        tracing::debug!("Generated schema:\n{schema_str}");

        // The schema should use @ as the key for the root type, not "None"
        assert!(
            schema_str.contains("@ @object")
                || schema_str.contains("@\n")
                || schema_str.contains("@ {"),
            "schema should use @ for root key, not None. Got:\n{}",
            schema_str
        );
        assert!(
            !schema_str.contains("None @object"),
            "schema should not contain 'None @object'. Got:\n{}",
            schema_str
        );
    }

    #[test]
    fn test_debug_nested_default_serialization() {
        use crate::to_string_compact;

        #[derive(Facet, Default)]
        #[allow(dead_code)]
        struct Inner {
            #[facet(default = true)]
            enabled: bool,
            #[facet(default = 8080)]
            port: u16,
        }

        // Check what Inner::default() serializes to
        let inner_default = Inner::default();
        let serialized = to_string_compact(&inner_default).expect("serialization should work");
        tracing::debug!("Inner::default() serializes to: {}", serialized);

        // Now check what the schema looks like
        let schema_str = schema_from_type::<Inner>();
        tracing::debug!("Inner schema:\n{}", schema_str);

        // Check if the schema contains @default wrappers at field level
        assert!(
            schema_str.contains("@default"),
            "Inner schema should contain @default for fields with defaults"
        );
    }

    #[test]
    fn test_doc_comment_leading_space_trimmed() {
        /// Configuration with documented fields.
        #[derive(Facet)]
        #[allow(dead_code)]
        struct Config {
            /// The hostname to connect to.
            host: String,
            /// The port number.
            /// Can be any valid port.
            port: u16,
        }

        let schema = schema_from_type::<Config>();
        tracing::debug!("Generated schema:\n{schema}");

        // Doc comments should have exactly one space after ///
        // NOT "///  The hostname" (double space)
        assert!(
            schema.contains("/// The hostname"),
            "Doc comment should have single space after ///"
        );
        assert!(
            !schema.contains("///  The hostname"),
            "Doc comment should NOT have double space after ///"
        );
        assert!(
            schema.contains("/// The port number"),
            "First line of multi-line doc should have single space"
        );
        assert!(
            schema.contains("/// Can be any valid port"),
            "Second line of multi-line doc should have single space"
        );
    }

    #[test]
    fn test_flatten_hashmap() {
        use std::collections::HashMap;

        #[derive(Facet)]
        #[allow(dead_code)]
        struct QueryFile {
            /// All declarations
            #[facet(flatten)]
            decls: HashMap<String, Decl>,
        }

        /// A declaration
        #[derive(Facet)]
        #[allow(dead_code)]
        struct Decl {
            value: String,
        }

        let schema = schema_from_type::<QueryFile>();
        eprintln!("Generated schema:\n{schema}");

        // Flattened HashMap should NOT have a "decls" key
        assert!(
            !schema.contains("decls"),
            "schema should NOT contain 'decls' key for flattened HashMap. Got:\n{}",
            schema
        );
        // Should have typed catch-all @string with the value type (now a @Decl reference)
        assert!(
            schema.contains("@string @Decl"),
            "schema should have typed catch-all @string entry with @Decl reference. Got:\n{}",
            schema
        );
        // Decl should have its own definition
        assert!(
            schema.contains("Decl @object"),
            "schema should contain Decl type definition. Got:\n{}",
            schema
        );
        // Decl should contain the 'value' field
        assert!(
            schema.contains("value @string"),
            "Decl should contain 'value' field. Got:\n{}",
            schema
        );
    }

    #[test]
    fn test_flatten_struct() {
        #[derive(Facet)]
        #[allow(dead_code)]
        struct Common {
            name: String,
            id: u64,
        }

        #[derive(Facet)]
        #[allow(dead_code)]
        struct Extended {
            #[facet(flatten)]
            common: Common,
            extra: String,
        }

        let schema = schema_from_type::<Extended>();
        tracing::debug!("Generated schema:\n{schema}");

        // Flattened struct should NOT have a "common" key
        assert!(
            !schema.contains("common"),
            "schema should NOT contain 'common' key for flattened struct. Got:\n{}",
            schema
        );
        // Should have the flattened struct's fields directly
        assert!(
            schema.contains("name @string"),
            "schema should contain 'name' field from Common. Got:\n{}",
            schema
        );
        assert!(
            schema.contains("id @int"),
            "schema should contain 'id' field from Common. Got:\n{}",
            schema
        );
        // Should still have the regular field
        assert!(
            schema.contains("extra @string"),
            "schema should contain 'extra' field. Got:\n{}",
            schema
        );
    }

    #[test]
    fn test_flatten_hashmap_with_optional_value() {
        use std::collections::HashMap;

        /// ORDER BY clause.
        #[derive(Facet)]
        #[allow(dead_code)]
        struct OrderBy {
            /// Column name -> direction ("asc" or "desc", None means asc)
            #[facet(flatten)]
            pub columns: HashMap<String, Option<String>>,
        }

        let schema = schema_from_type::<OrderBy>();
        tracing::debug!("Generated schema:\n{schema}");

        // Flattened HashMap<String, Option<String>> should produce @string @optional(@string)
        // NOT "@" @optional(@string) or "\"@\"" @optional(@string)
        assert!(
            schema.contains("@string @optional"),
            "schema should have typed catch-all @string with @optional value. Got:\n{}",
            schema
        );
        assert!(
            !schema.contains("\"@\""),
            "schema should NOT contain quoted @ key. Got:\n{}",
            schema
        );
    }

    #[test]
    fn test_recursive_types_get_definitions() {
        /// A node in a tree structure.
        #[derive(Facet)]
        #[allow(dead_code)]
        struct TreeNode {
            value: String,
            children: Vec<TreeNode>,
        }

        let schema = schema_from_type::<TreeNode>();
        tracing::debug!("Generated schema:\n{schema}");

        // The schema should contain a named definition for TreeNode
        // because it references itself recursively
        assert!(
            schema.contains("TreeNode @object"),
            "schema should have named TreeNode definition. Got:\n{}",
            schema
        );

        // The children field should reference @TreeNode
        assert!(
            schema.contains("@TreeNode"),
            "schema should reference @TreeNode. Got:\n{}",
            schema
        );

        // Parse and verify structure
        let parsed: SchemaFile =
            crate::from_str(&schema).expect("failed to parse generated schema");

        // Should have root definition and TreeNode definition
        assert!(
            parsed.schema.contains_key(&Some("TreeNode".to_string())),
            "schema should have TreeNode type definition"
        );
    }

    #[test]
    fn test_mutually_recursive_types_get_definitions() {
        /// A container that can hold items.
        #[derive(Facet)]
        #[allow(dead_code)]
        struct Container {
            name: String,
            items: Vec<Item>,
        }

        /// An item that can contain other containers.
        #[derive(Facet)]
        #[allow(dead_code)]
        struct Item {
            id: u32,
            nested: Option<Container>,
        }

        let schema = schema_from_type::<Container>();
        tracing::debug!("Generated schema:\n{schema}");

        // Parse and verify both types have definitions
        let parsed: SchemaFile =
            crate::from_str(&schema).expect("failed to parse generated schema");

        // Should have definitions for both Container and Item
        // Note: Container is the root so it's at None, Item should be at Some("Item")
        assert!(
            parsed.schema.contains_key(&Some("Item".to_string())),
            "schema should have Item type definition. Keys: {:?}",
            parsed.schema.keys().collect::<Vec<_>>()
        );
    }

    #[test]
    fn test_enum_with_facet_other_becomes_any() {
        /// An enum that accepts any tag via #[facet(other)].
        #[derive(Facet)]
        #[facet(rename_all = "lowercase")]
        #[repr(u8)]
        #[allow(dead_code)]
        enum ValueExpr {
            /// Default value (@default).
            Default,
            /// Everything else: functions and bare scalars.
            #[facet(other)]
            Other {
                #[facet(tag)]
                tag: Option<String>,
                #[facet(content)]
                content: Option<String>,
            },
        }

        #[derive(Facet)]
        #[allow(dead_code)]
        struct Config {
            value: ValueExpr,
        }

        let schema = schema_from_type::<Config>();
        tracing::debug!("Generated schema:\n{schema}");

        // The ValueExpr type should be @any because of #[facet(other)]
        assert!(
            schema.contains("ValueExpr @any"),
            "enum with #[facet(other)] should become @any. Got:\n{}",
            schema
        );
        // Should NOT have an enum definition with "other" as a variant
        assert!(
            !schema.contains("@enum"),
            "should not emit @enum for type with #[facet(other)]. Got:\n{}",
            schema
        );
        assert!(
            !schema.contains("other"),
            "should not contain 'other' as a variant name. Got:\n{}",
            schema
        );
    }
}