faucet-cli 1.7.0

Config-driven CLI runner for faucet-stream pipelines (YAML / JSON, Meltano-style)
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
//! Schema-driven YAML template emitter for `faucet init`.
//!
//! Given a [`schemars`]-emitted JSON Schema for a connector config struct,
//! [`schema_to_yaml_template`] walks the schema's `properties` and produces a
//! comment-annotated YAML block suitable for pasting under `config:` in a
//! generated pipeline file. Required fields are surfaced with placeholder
//! values and a `# REQUIRED` comment; optional fields are commented out so
//! users can opt in by uncommenting and editing.
//!
//! The emitter is intentionally a string builder rather than a `serde_yaml`
//! round-trip: comments and required/optional distinctions matter to the
//! reader and would be lost by any round-trip through a generic YAML value.

use std::collections::HashMap;

use serde_json::Value;

/// A top-level property whose schema is a tagged enum (`oneOf` with a `const`
/// discriminator field). Returned by [`discover_tagged_enum_fields`] so the
/// CLI's interactive mode can prompt the user for the variant before emitting
/// the scaffold.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TaggedEnumField {
    /// Property name on the root object — e.g. `"auth"`, `"pagination"`,
    /// `"credentials"`.
    pub path: String,
    /// Variant tag values in the order they appear in `oneOf` — e.g.
    /// `["None", "Bearer", "Basic", "ApiKey"]`.
    pub variants: Vec<String>,
}

/// Render the `properties` of `schema` as a YAML block, indented by
/// `indent_spaces` columns. Returns a string that always ends with `\n`.
///
/// Tagged-enum fields get their first variant inlined and the remaining
/// variants emitted as commented-out "alternative" blocks the user can
/// uncomment to switch. Use [`schema_to_yaml_template_with_choices`] to
/// override which variant is inlined.
pub fn schema_to_yaml_template(schema: &Value, indent_spaces: usize) -> String {
    schema_to_yaml_template_with_choices(schema, indent_spaces, &HashMap::new())
}

/// Like [`schema_to_yaml_template`], but each entry in `choices` overrides
/// the inlined variant for a tagged-enum field whose property name matches
/// the key. (e.g. `choices.insert("auth", "Bearer")` makes the Bearer variant
/// the inlined default.) Unknown variant tags fall back to the first variant.
pub fn schema_to_yaml_template_with_choices(
    schema: &Value,
    indent_spaces: usize,
    choices: &HashMap<String, String>,
) -> String {
    let defs = schema.get("$defs");
    let mut out = String::new();
    emit_object_properties(schema, defs, indent_spaces, choices, &mut out);
    if out.is_empty() {
        let pad = " ".repeat(indent_spaces);
        out.push_str(&format!("{pad}{{}}\n"));
    }
    out
}

/// Walk the top-level properties of `schema` and return every property whose
/// schema is a tagged enum. `$ref`s are resolved against `$defs`.
pub fn discover_tagged_enum_fields(schema: &Value) -> Vec<TaggedEnumField> {
    let defs = schema.get("$defs");
    let resolved = resolve_ref_borrowed(schema, defs);
    let Some(props) = resolved.get("properties").and_then(|v| v.as_object()) else {
        return Vec::new();
    };
    let mut out = Vec::new();
    for (key, prop_schema) in props {
        let prop_resolved = resolve_ref_borrowed(prop_schema, defs);
        if let Some(variants) = tagged_enum_variants(prop_resolved, defs) {
            out.push(TaggedEnumField {
                path: key.clone(),
                variants: variants.iter().map(|v| v.tag.to_string()).collect(),
            });
        }
    }
    out
}

fn emit_object_properties(
    schema: &Value,
    defs: Option<&Value>,
    indent: usize,
    choices: &HashMap<String, String>,
    out: &mut String,
) {
    let schema = resolve_ref(schema, defs);
    let Some(props) = schema.get("properties").and_then(|v| v.as_object()) else {
        return;
    };
    let required: Vec<&str> = schema
        .get("required")
        .and_then(|v| v.as_array())
        .map(|a| a.iter().filter_map(|v| v.as_str()).collect())
        .unwrap_or_default();

    for (key, prop_schema) in props {
        let is_required = required.contains(&key.as_str());
        emit_property(key, prop_schema, is_required, defs, indent, choices, out);
    }
}

#[allow(clippy::too_many_arguments)] // schema-walking helpers thread several context refs.
fn emit_property(
    key: &str,
    schema: &Value,
    required: bool,
    defs: Option<&Value>,
    indent: usize,
    choices: &HashMap<String, String>,
    out: &mut String,
) {
    let pad = " ".repeat(indent);
    let resolved = resolve_ref(schema, defs);
    let description = resolved
        .get("description")
        .and_then(|v| v.as_str())
        .map(collapse_whitespace);

    // Tagged-enum (oneOf with `type` discriminator) — emit the chosen variant
    // inline and append the remaining variants as commented-out alternatives
    // so users see every option without having to leave the file.
    if let Some(variants) = tagged_enum_variants(&resolved, defs) {
        emit_tagged_enum(
            key,
            &variants,
            required,
            &description,
            defs,
            indent,
            choices,
            out,
        );
        return;
    }

    // Nested object with its own `properties` — recurse when required, flatten
    // to a comment when optional.
    if is_object_with_properties(&resolved) {
        if required {
            out.push_str(&format!("{pad}{key}:\n"));
            emit_object_properties(&resolved, defs, indent + 2, choices, out);
        } else {
            let line_comment = describe(&description, &resolved);
            let suffix = if line_comment.is_empty() {
                String::new()
            } else {
                format!("    # {line_comment}")
            };
            out.push_str(&format!("{pad}# {key}: {{ ... }}{suffix}\n"));
        }
        return;
    }

    let placeholder = type_placeholder(&resolved);
    let value = resolved
        .get("default")
        .map(render_default)
        .unwrap_or(placeholder);

    let mut comment_parts: Vec<String> = Vec::new();
    if required {
        comment_parts.push("REQUIRED".to_string());
    }
    if let Some(d) = description.as_ref()
        && !d.is_empty()
    {
        comment_parts.push(d.clone());
    }
    if let Some(values) = enum_string_values(&resolved) {
        comment_parts.push(format!("one of: {}", values.join(", ")));
    }
    let comment = if comment_parts.is_empty() {
        String::new()
    } else {
        format!("    # {}", comment_parts.join(""))
    };

    if required {
        out.push_str(&format!("{pad}{key}: {value}{comment}\n"));
    } else {
        out.push_str(&format!("{pad}# {key}: {value}{comment}\n"));
    }
}

#[allow(clippy::too_many_arguments)] // schema-walking helpers thread several context refs.
fn emit_tagged_enum(
    key: &str,
    variants: &[TaggedVariant<'_>],
    required: bool,
    _description: &Option<String>,
    defs: Option<&Value>,
    indent: usize,
    choices: &HashMap<String, String>,
    out: &mut String,
) {
    let pad = " ".repeat(indent);
    let inner_pad = " ".repeat(indent + 2);
    let all_tags: Vec<&str> = variants.iter().map(|v| v.tag).collect();
    let chosen_idx = choices
        .get(key)
        .and_then(|tag| variants.iter().position(|v| v.tag == tag))
        .unwrap_or(0);
    let chosen = &variants[chosen_idx];

    if required {
        out.push_str(&format!("{pad}{key}:\n"));
        out.push_str(&format!(
            "{inner_pad}type: {tag}    # one of: {tags}\n",
            tag = chosen.tag,
            tags = all_tags.join(", "),
        ));
        for (field_key, field_schema, field_required) in &chosen.fields {
            if *field_key == chosen.discriminator {
                continue;
            }
            emit_property(
                field_key,
                field_schema,
                *field_required,
                defs,
                indent + 2,
                choices,
                out,
            );
        }
        emit_alternative_variants(variants, chosen_idx, defs, indent + 2, out);
    } else {
        // Optional tagged enum: flatten the chosen variant to a single
        // commented line. Also emit the alternatives block so the user can
        // see every option.
        out.push_str(&format!(
            "{pad}# {key}: {{ type: {tag} }}    # one of: {tags}\n",
            tag = chosen.tag,
            tags = all_tags.join(", "),
        ));
        emit_alternative_variants(variants, chosen_idx, defs, indent + 2, out);
    }
}

/// Emit the non-chosen variants as a commented-out alternatives block.
/// Lines are indented to the same column as the chosen variant's fields so
/// removing the leading `# ` from a block produces valid YAML.
fn emit_alternative_variants(
    variants: &[TaggedVariant<'_>],
    chosen_idx: usize,
    defs: Option<&Value>,
    indent: usize,
    out: &mut String,
) {
    let alternatives: Vec<&TaggedVariant<'_>> = variants
        .iter()
        .enumerate()
        .filter_map(|(i, v)| if i == chosen_idx { None } else { Some(v) })
        .collect();
    if alternatives.is_empty() {
        return;
    }
    let pad = " ".repeat(indent);
    out.push_str(&format!(
        "{pad}# --- Alternative variants — replace the block above with one of these ---\n"
    ));
    for (i, alt) in alternatives.iter().enumerate() {
        if i > 0 {
            out.push_str(&format!("{pad}#\n"));
        }
        out.push_str(&format!("{pad}# type: {tag}\n", tag = alt.tag));
        for (field_key, field_schema, field_required) in &alt.fields {
            if *field_key == alt.discriminator {
                continue;
            }
            let resolved = resolve_ref(field_schema, defs);
            // Adjacent tagging nests a variant's real fields under a `config`
            // object — drill into it so the commented alternative shows the
            // actual fields, not an opaque `{ ... }`.
            if is_object_with_properties(&resolved) {
                out.push_str(&format!("{pad}# {field_key}:\n"));
                emit_commented_object_props(&resolved, defs, indent + 2, out);
            } else {
                let placeholder = resolved
                    .get("default")
                    .map(render_default)
                    .unwrap_or_else(|| type_placeholder(&resolved));
                let marker = if *field_required {
                    "    # REQUIRED"
                } else {
                    ""
                };
                out.push_str(&format!("{pad}# {field_key}: {placeholder}{marker}\n"));
            }
        }
    }
}

/// Emit every property of an object schema as commented-out YAML lines at
/// `indent`, recursing into nested objects. Used for the commented "alternative
/// variant" blocks where the whole block is already prefixed with `# `.
fn emit_commented_object_props(
    schema: &Value,
    defs: Option<&Value>,
    indent: usize,
    out: &mut String,
) {
    let pad = " ".repeat(indent);
    let Some(props) = schema.get("properties").and_then(|v| v.as_object()) else {
        return;
    };
    let required: Vec<&str> = schema
        .get("required")
        .and_then(|v| v.as_array())
        .map(|a| a.iter().filter_map(|v| v.as_str()).collect())
        .unwrap_or_default();
    for (k, s) in props {
        let resolved = resolve_ref(s, defs);
        let req = required.contains(&k.as_str());
        if is_object_with_properties(&resolved) {
            out.push_str(&format!("{pad}# {k}:\n"));
            emit_commented_object_props(&resolved, defs, indent + 2, out);
        } else {
            let placeholder = resolved
                .get("default")
                .map(render_default)
                .unwrap_or_else(|| type_placeholder(&resolved));
            let marker = if req { "    # REQUIRED" } else { "" };
            out.push_str(&format!("{pad}# {k}: {placeholder}{marker}\n"));
        }
    }
}

struct TaggedVariant<'a> {
    tag: &'a str,
    discriminator: &'a str,
    fields: Vec<(&'a str, &'a Value, bool)>,
}

fn tagged_enum_variants<'a>(
    schema: &'a Value,
    defs: Option<&'a Value>,
) -> Option<Vec<TaggedVariant<'a>>> {
    // A directly tagged enum exposes `oneOf`. An `AuthSpec`-style wrapper
    // (`#[serde(untagged)]` over the inline auth enum plus a `{ ref }` struct)
    // exposes `anyOf`; unwrap to the inline member that is itself a tagged enum
    // so `auth: { ref }` fields still render their inline-auth variants.
    let arr = match schema.get("oneOf").and_then(|v| v.as_array()) {
        Some(a) => a,
        None => {
            let any = schema.get("anyOf")?.as_array()?;
            let inner = any
                .iter()
                .map(|m| resolve_ref_borrowed(m, defs))
                .find(|r| r.get("oneOf").is_some())?;
            inner.get("oneOf")?.as_array()?
        }
    };
    if arr.is_empty() {
        return None;
    }
    // Discover the discriminator from the first variant.
    let first = resolve_ref_borrowed(&arr[0], defs);
    let props = first.get("properties")?.as_object()?;
    let (disc, _) = props
        .iter()
        .find(|(_, v)| v.get("const").and_then(|c| c.as_str()).is_some())?;
    let mut variants = Vec::new();
    for v in arr {
        let resolved = resolve_ref_borrowed(v, defs);
        let v_props = resolved.get("properties").and_then(|p| p.as_object())?;
        let tag = v_props
            .get(disc)
            .and_then(|t| t.get("const"))
            .and_then(|c| c.as_str())?;
        let required: Vec<&str> = resolved
            .get("required")
            .and_then(|r| r.as_array())
            .map(|a| a.iter().filter_map(|v| v.as_str()).collect())
            .unwrap_or_default();
        let fields = v_props
            .iter()
            .map(|(k, s)| (k.as_str(), s, required.contains(&k.as_str())))
            .collect();
        variants.push(TaggedVariant {
            tag,
            discriminator: disc,
            fields,
        });
    }
    Some(variants)
}

fn is_object_with_properties(schema: &Value) -> bool {
    schema_type(schema) == Some("object") && schema.get("properties").is_some()
}

fn schema_type(schema: &Value) -> Option<&str> {
    match schema.get("type") {
        Some(Value::String(s)) => Some(s.as_str()),
        Some(Value::Array(arr)) => arr.iter().filter_map(|v| v.as_str()).find(|s| *s != "null"),
        _ => None,
    }
}

fn type_placeholder(schema: &Value) -> String {
    match schema_type(schema) {
        Some("string") => "\"\"".to_string(),
        Some("integer") | Some("number") => "0".to_string(),
        Some("boolean") => "false".to_string(),
        Some("array") => "[]".to_string(),
        Some("object") => "{}".to_string(),
        _ => "null".to_string(),
    }
}

fn render_default(v: &Value) -> String {
    match v {
        Value::Null => "null".to_string(),
        Value::Bool(b) => b.to_string(),
        Value::Number(n) => n.to_string(),
        Value::String(s) => format!("\"{}\"", s.replace('"', "\\\"")),
        Value::Array(a) if a.is_empty() => "[]".to_string(),
        Value::Object(o) if o.is_empty() => "{}".to_string(),
        // Non-trivial composite defaults are rendered as compact JSON, which
        // happens to be a valid YAML flow-style literal.
        other => other.to_string(),
    }
}

fn enum_string_values(schema: &Value) -> Option<Vec<&str>> {
    let arr = schema.get("enum")?.as_array()?;
    let values: Vec<&str> = arr.iter().filter_map(|v| v.as_str()).collect();
    if values.is_empty() {
        None
    } else {
        Some(values)
    }
}

fn describe(description: &Option<String>, schema: &Value) -> String {
    let mut parts: Vec<String> = Vec::new();
    if let Some(d) = description.as_ref()
        && !d.is_empty()
    {
        parts.push(d.clone());
    }
    if let Some(values) = enum_string_values(schema) {
        parts.push(format!("one of: {}", values.join(", ")));
    }
    parts.join("")
}

/// Collapse runs of whitespace into single spaces and truncate to a
/// reader-friendly preview: the first sentence, or 120 chars, whichever comes
/// first. Long rustdoc paragraphs become illegible when inlined as a YAML
/// comment, and the full text is still available via `faucet schema`.
fn collapse_whitespace(s: &str) -> String {
    let collapsed: String = s.split_whitespace().collect::<Vec<_>>().join(" ");
    const MAX: usize = 120;
    if let Some(idx) = collapsed.find(". ") {
        let head = &collapsed[..idx + 1];
        return head.to_string();
    }
    if collapsed.chars().count() > MAX {
        let mut truncated: String = collapsed.chars().take(MAX).collect();
        truncated.push('');
        return truncated;
    }
    collapsed
}

fn resolve_ref(schema: &Value, defs: Option<&Value>) -> Value {
    resolve_ref_borrowed(schema, defs).clone()
}

fn resolve_ref_borrowed<'a>(schema: &'a Value, defs: Option<&'a Value>) -> &'a Value {
    let Some(reference) = schema.get("$ref").and_then(|v| v.as_str()) else {
        return schema;
    };
    let Some(name) = reference.strip_prefix("#/$defs/") else {
        return schema;
    };
    defs.and_then(|d| d.get(name)).unwrap_or(schema)
}

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

    #[test]
    fn empty_object_schema_emits_brace_placeholder() {
        let schema = json!({ "type": "object" });
        let yaml = schema_to_yaml_template(&schema, 6);
        assert_eq!(yaml, "      {}\n");
    }

    #[test]
    fn required_string_field_gets_quoted_placeholder_and_required_marker() {
        let schema = json!({
            "type": "object",
            "properties": {
                "path": { "type": "string", "description": "Path to the output file." }
            },
            "required": ["path"]
        });
        let yaml = schema_to_yaml_template(&schema, 6);
        assert!(yaml.contains("path: \"\""), "missing path key: {yaml}");
        assert!(
            yaml.contains("# REQUIRED"),
            "missing REQUIRED comment: {yaml}"
        );
        assert!(yaml.contains("Path to the output file."));
    }

    #[test]
    fn required_integer_field_gets_zero_placeholder() {
        let schema = json!({
            "type": "object",
            "properties": { "port": { "type": "integer" } },
            "required": ["port"]
        });
        let yaml = schema_to_yaml_template(&schema, 0);
        assert!(yaml.contains("port: 0"));
        assert!(yaml.contains("# REQUIRED"));
    }

    #[test]
    fn required_boolean_field_gets_false_placeholder() {
        let schema = json!({
            "type": "object",
            "properties": { "ssl": { "type": "boolean" } },
            "required": ["ssl"]
        });
        let yaml = schema_to_yaml_template(&schema, 0);
        assert!(yaml.contains("ssl: false"));
    }

    #[test]
    fn optional_field_with_default_is_commented_out_with_default_value() {
        let schema = json!({
            "type": "object",
            "properties": {
                "batch_size": { "type": "integer", "default": 1000, "description": "Batch size." }
            }
        });
        let yaml = schema_to_yaml_template(&schema, 0);
        assert!(yaml.contains("# batch_size: 1000"));
        assert!(yaml.contains("Batch size."));
    }

    #[test]
    fn optional_field_without_default_is_commented_out_with_placeholder() {
        let schema = json!({
            "type": "object",
            "properties": {
                "label": { "type": "string", "description": "Friendly label." }
            }
        });
        let yaml = schema_to_yaml_template(&schema, 0);
        assert!(yaml.contains("# label: \"\""));
        assert!(yaml.contains("Friendly label."));
    }

    #[test]
    fn enum_values_appear_in_comment() {
        let schema = json!({
            "type": "object",
            "properties": {
                "method": {
                    "type": "string",
                    "enum": ["GET", "POST", "PUT", "PATCH", "DELETE"],
                    "default": "GET",
                    "description": "HTTP method."
                }
            }
        });
        let yaml = schema_to_yaml_template(&schema, 0);
        assert!(yaml.contains("# method: \"GET\""), "yaml: {yaml}");
        assert!(
            yaml.contains("one of: GET, POST, PUT, PATCH, DELETE"),
            "yaml: {yaml}"
        );
    }

    #[test]
    fn required_nested_object_recurses() {
        let schema = json!({
            "type": "object",
            "properties": {
                "address": {
                    "type": "object",
                    "properties": {
                        "city": { "type": "string" }
                    },
                    "required": ["city"]
                }
            },
            "required": ["address"]
        });
        let yaml = schema_to_yaml_template(&schema, 0);
        assert!(yaml.contains("address:\n"), "yaml: {yaml}");
        assert!(yaml.contains("  city: \"\""), "yaml: {yaml}");
        assert!(yaml.contains("# REQUIRED"));
    }

    #[test]
    fn optional_nested_object_flattens_to_comment() {
        let schema = json!({
            "type": "object",
            "properties": {
                "tls": {
                    "type": "object",
                    "properties": { "ca_path": { "type": "string" } },
                    "description": "TLS settings."
                }
            }
        });
        let yaml = schema_to_yaml_template(&schema, 0);
        assert!(yaml.contains("# tls: { ... }"), "yaml: {yaml}");
        assert!(yaml.contains("TLS settings."));
    }

    #[test]
    fn tagged_enum_required_expands_first_variant_inline() {
        let schema = json!({
            "type": "object",
            "properties": {
                "auth": {
                    "oneOf": [
                        {
                            "type": "object",
                            "properties": { "type": { "const": "none" } },
                            "required": ["type"]
                        },
                        {
                            "type": "object",
                            "properties": {
                                "type": { "const": "bearer" },
                                "token": { "type": "string" }
                            },
                            "required": ["type", "token"]
                        }
                    ]
                }
            },
            "required": ["auth"]
        });
        let yaml = schema_to_yaml_template(&schema, 0);
        assert!(yaml.contains("auth:\n"), "yaml: {yaml}");
        assert!(yaml.contains("type: none"), "yaml: {yaml}");
        assert!(yaml.contains("one of: none, bearer"), "yaml: {yaml}");
    }

    #[test]
    fn tagged_enum_optional_flattens_to_first_variant_comment() {
        let schema = json!({
            "type": "object",
            "properties": {
                "auth": {
                    "oneOf": [
                        {
                            "type": "object",
                            "properties": { "type": { "const": "none" } },
                            "required": ["type"]
                        },
                        {
                            "type": "object",
                            "properties": {
                                "type": { "const": "bearer" },
                                "token": { "type": "string" }
                            },
                            "required": ["type", "token"]
                        }
                    ]
                }
            }
        });
        let yaml = schema_to_yaml_template(&schema, 0);
        assert!(yaml.contains("# auth: { type: none }"), "yaml: {yaml}");
        assert!(yaml.contains("one of: none, bearer"), "yaml: {yaml}");
    }

    #[test]
    fn adjacent_tagged_enum_nests_config_block() {
        // schemars emits adjacent-tagged enums (`#[serde(tag="type", content="config")]`)
        // with the variant's real fields nested under a `config` object property.
        let schema = json!({
            "type": "object",
            "properties": {
                "auth": {
                    "oneOf": [
                        {
                            "type": "object",
                            "properties": { "type": { "const": "none" } },
                            "required": ["type"]
                        },
                        {
                            "type": "object",
                            "properties": {
                                "type": { "const": "bearer" },
                                "config": {
                                    "type": "object",
                                    "properties": { "token": { "type": "string" } },
                                    "required": ["token"]
                                }
                            },
                            "required": ["type", "config"]
                        }
                    ]
                }
            },
            "required": ["auth"]
        });
        let yaml = schema_to_yaml_template(&schema, 0);
        // Inline (chosen = none) + bearer in the alternatives with nested config.
        assert!(yaml.contains("type: none"), "yaml: {yaml}");
        assert!(yaml.contains("# type: bearer"), "yaml: {yaml}");
        assert!(yaml.contains("# config:"), "yaml: {yaml}");
        assert!(yaml.contains("# token: \"\""), "yaml: {yaml}");
    }

    #[test]
    fn ref_to_defs_is_resolved() {
        let schema = json!({
            "type": "object",
            "properties": {
                "creds": { "$ref": "#/$defs/Creds" }
            },
            "required": ["creds"],
            "$defs": {
                "Creds": {
                    "type": "object",
                    "properties": { "token": { "type": "string" } },
                    "required": ["token"]
                }
            }
        });
        let yaml = schema_to_yaml_template(&schema, 0);
        assert!(yaml.contains("creds:\n"), "yaml: {yaml}");
        assert!(yaml.contains("  token: \"\""), "yaml: {yaml}");
    }

    #[test]
    fn tagged_enum_required_lists_other_variants_as_commented_alternatives() {
        let schema = json!({
            "type": "object",
            "properties": {
                "auth": {
                    "oneOf": [
                        {
                            "type": "object",
                            "properties": { "type": { "const": "none" } },
                            "required": ["type"]
                        },
                        {
                            "type": "object",
                            "properties": {
                                "type": { "const": "bearer" },
                                "token": { "type": "string" }
                            },
                            "required": ["type", "token"]
                        },
                        {
                            "type": "object",
                            "properties": {
                                "type": { "const": "basic" },
                                "username": { "type": "string" },
                                "password": { "type": "string" }
                            },
                            "required": ["type", "username", "password"]
                        }
                    ]
                }
            },
            "required": ["auth"]
        });
        let yaml = schema_to_yaml_template(&schema, 0);
        // The chosen variant (None by default) is inlined.
        assert!(yaml.contains("type: none"), "yaml: {yaml}");
        // Every other variant's tag appears in the alternatives block.
        assert!(yaml.contains("type: bearer"), "yaml: {yaml}");
        assert!(yaml.contains("type: basic"), "yaml: {yaml}");
        // Other variants' fields appear too, commented out.
        assert!(yaml.contains("# token: \"\""), "yaml: {yaml}");
        assert!(yaml.contains("# username: \"\""), "yaml: {yaml}");
        assert!(yaml.contains("# password: \"\""), "yaml: {yaml}");
    }

    #[test]
    fn tagged_enum_with_explicit_choice_inlines_that_variant() {
        use std::collections::HashMap;
        let schema = json!({
            "type": "object",
            "properties": {
                "auth": {
                    "oneOf": [
                        {
                            "type": "object",
                            "properties": { "type": { "const": "none" } },
                            "required": ["type"]
                        },
                        {
                            "type": "object",
                            "properties": {
                                "type": { "const": "bearer" },
                                "token": { "type": "string" }
                            },
                            "required": ["type", "token"]
                        }
                    ]
                }
            },
            "required": ["auth"]
        });
        let mut choices = HashMap::new();
        choices.insert("auth".to_string(), "bearer".to_string());
        let yaml = schema_to_yaml_template_with_choices(&schema, 0, &choices);

        // Bearer is the chosen variant, so its `token` field is emitted
        // uncommented with a REQUIRED marker.
        let token_line = yaml
            .lines()
            .find(|l| l.contains("token:"))
            .expect("token line missing");
        assert!(
            !token_line.trim_start().starts_with('#'),
            "expected chosen variant's token to be uncommented; got: {token_line:?}"
        );
        assert!(
            token_line.contains("REQUIRED"),
            "chosen variant fields should carry REQUIRED marker; got: {token_line:?}"
        );
        // The `none` variant still appears in the alternatives block,
        // commented out.
        let none_line = yaml
            .lines()
            .find(|l| l.contains("type: none"))
            .expect("none variant missing from alternatives");
        assert!(
            none_line.trim_start().starts_with('#'),
            "non-chosen variant should be commented out; got: {none_line:?}"
        );
    }

    #[test]
    fn discover_tagged_enum_fields_finds_top_level_oneof_properties() {
        let schema = json!({
            "type": "object",
            "properties": {
                "auth": {
                    "oneOf": [
                        {
                            "type": "object",
                            "properties": { "type": { "const": "none" } },
                            "required": ["type"]
                        },
                        {
                            "type": "object",
                            "properties": {
                                "type": { "const": "bearer" },
                                "token": { "type": "string" }
                            },
                            "required": ["type", "token"]
                        }
                    ]
                },
                "path": { "type": "string" }
            }
        });
        let fields = discover_tagged_enum_fields(&schema);
        assert_eq!(fields.len(), 1, "expected exactly one tagged-enum field");
        assert_eq!(fields[0].path, "auth");
        assert_eq!(fields[0].variants, vec!["none", "bearer"]);
    }

    #[test]
    fn discover_tagged_enum_fields_resolves_refs() {
        let schema = json!({
            "type": "object",
            "properties": {
                "auth": { "$ref": "#/$defs/Auth" }
            },
            "$defs": {
                "Auth": {
                    "oneOf": [
                        {
                            "type": "object",
                            "properties": { "type": { "const": "none" } },
                            "required": ["type"]
                        },
                        {
                            "type": "object",
                            "properties": {
                                "type": { "const": "bearer" },
                                "token": { "type": "string" }
                            },
                            "required": ["type", "token"]
                        }
                    ]
                }
            }
        });
        let fields = discover_tagged_enum_fields(&schema);
        assert_eq!(fields.len(), 1);
        assert_eq!(fields[0].path, "auth");
        assert_eq!(fields[0].variants, vec!["none", "bearer"]);
    }

    #[test]
    fn output_indent_matches_requested_column() {
        let schema = json!({
            "type": "object",
            "properties": { "name": { "type": "string" } },
            "required": ["name"]
        });
        let yaml = schema_to_yaml_template(&schema, 4);
        for line in yaml.lines() {
            if !line.trim().is_empty() {
                assert!(
                    line.starts_with("    "),
                    "line not indented 4 spaces: {line:?}"
                );
            }
        }
    }
}