openapi-to-rust 0.5.2

Generate strongly-typed Rust structs, HTTP clients, and SSE streaming clients from OpenAPI 3.1 specifications
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
use clap::{Parser, Subcommand};
use openapi_to_rust::cli::{json_from_str_lossy, yaml_to_json_value};
use openapi_to_rust::server::{
    OperationIndex, Selector,
    edit::Editor as ServerEditor,
    list::{ListFilter, ListOutput, render as render_list},
    resolve as resolve_selectors,
};
use openapi_to_rust::{CodeGenerator, ConfigFile, SchemaAnalyzer};
use std::path::PathBuf;

#[derive(Parser)]
#[command(name = "openapi-to-rust")]
#[command(about = "Generate Rust types and clients from OpenAPI specs")]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Generate code from OpenAPI spec
    Generate {
        /// Path to configuration file (openapi-to-rust.toml)
        #[arg(short, long, default_value = "openapi-to-rust.toml")]
        config: PathBuf,
        /// Force every typed-scalar strategy back to "string" (Q2).
        /// Useful for bisecting regressions caused by typed-scalar
        /// adoption — overrides any `[generator.types]` settings in
        /// the TOML config.
        #[arg(long)]
        types_conservative: bool,
    },
    /// Validate configuration file without generating code
    Validate {
        /// Path to configuration file (openapi-to-rust.toml)
        #[arg(short, long, default_value = "openapi-to-rust.toml")]
        config: PathBuf,
    },
    /// Server codegen commands (opt-in Axum scaffolding).
    Server {
        #[command(subcommand)]
        action: ServerCommands,
    },
}

#[derive(Subcommand)]
enum ServerCommands {
    /// List every operation in a spec. Read-only.
    List {
        /// Path to the OpenAPI spec (.yaml/.yml/.json). If omitted,
        /// the spec_path from openapi-to-rust.toml is used.
        #[arg(long)]
        spec: Option<PathBuf>,
        /// Path to TOML config to read spec_path from when --spec is absent.
        #[arg(long, default_value = "openapi-to-rust.toml")]
        config: PathBuf,
        /// Substring match against tag names (case insensitive).
        #[arg(long)]
        tag: Option<String>,
        /// Exact HTTP method filter (GET, POST, ...; case insensitive).
        #[arg(long)]
        method: Option<String>,
        /// Substring match against operationId and path.
        #[arg(long)]
        grep: Option<String>,
        /// Emit JSON instead of an aligned table.
        #[arg(long)]
        json: bool,
    },
    /// Add a selector to `[server].operations` in the TOML config.
    /// Does not regenerate unless `--regenerate` is passed.
    Add {
        /// Selector: `operationId` | `METHOD /path` | `tag:<name>`.
        selector: Option<String>,
        /// Path to the OpenAPI spec. Defaults to the one in the config.
        #[arg(long)]
        spec: Option<PathBuf>,
        /// Path to the TOML config to edit.
        #[arg(long, default_value = "openapi-to-rust.toml")]
        config: PathBuf,
        /// Expand a tag and add each operationId individually
        /// (instead of adding a `tag:` selector).
        #[arg(long)]
        all_tag: Option<String>,
        /// Print the proposed change without writing.
        #[arg(long)]
        dry_run: bool,
        /// After updating the TOML, immediately run `generate` to
        /// emit code for the new selectors.
        #[arg(long)]
        regenerate: bool,
    },
    /// Remove a selector entry from `[server].operations`.
    Remove {
        /// Selector to remove. Matched verbatim against the TOML list.
        selector: String,
        /// Path to the TOML config to edit.
        #[arg(long, default_value = "openapi-to-rust.toml")]
        config: PathBuf,
        /// Print the proposed change without writing.
        #[arg(long)]
        dry_run: bool,
    },
}

#[tokio::main]
async fn main() {
    let cli = Cli::parse();
    if let Err(e) = run(cli).await {
        // Use Display, not Debug, so thiserror messages render with
        // their fuzzy-match suggestions (`Did you mean ...?`) instead
        // of as raw enum debug output.
        eprintln!("Error: {e}");
        std::process::exit(1);
    }
}

async fn run(cli: Cli) -> Result<(), Box<dyn std::error::Error>> {
    match cli.command {
        Commands::Validate { config } => {
            println!("📖 Validating configuration from: {}", config.display());

            // Load and validate configuration from TOML
            match ConfigFile::load(&config) {
                Ok(_config_file) => {
                    println!("✅ Configuration is valid!");
                    Ok(())
                }
                Err(e) => {
                    eprintln!("❌ Configuration validation failed:");
                    eprintln!("{}", e);
                    std::process::exit(1);
                }
            }
        }
        Commands::Generate {
            config,
            types_conservative,
        } => {
            println!("📖 Reading configuration from: {}", config.display());

            // Load configuration from TOML
            let config_file = match ConfigFile::load(&config) {
                Ok(cf) => cf,
                Err(e) => {
                    eprintln!("❌ Failed to load configuration:");
                    eprintln!("{}", e);
                    std::process::exit(1);
                }
            };

            let mut generator_config = config_file.into_generator_config();

            // CLI override: `--types-conservative` collapses every
            // Q2 typed-scalar strategy back to plain `String`. Useful
            // for bisecting regressions caused by typed-scalar
            // adoption without editing the TOML config.
            if types_conservative {
                generator_config.types = openapi_to_rust::TypeMappingConfig::conservative();
            }

            println!(
                "📄 Reading OpenAPI spec: {}",
                generator_config.spec_path.display()
            );

            // Read and parse OpenAPI spec
            let spec_content = std::fs::read_to_string(&generator_config.spec_path)?;
            let spec_value: serde_json::Value = if generator_config.spec_path.extension()
                == Some(std::ffi::OsStr::new("yaml"))
                || generator_config.spec_path.extension() == Some(std::ffi::OsStr::new("yml"))
            {
                yaml_to_json_value(&spec_content)?
            } else {
                json_from_str_lossy(&spec_content)?
            };

            // Version gate: surface unsupported OAS major.minor early.
            let oas_version = spec_value
                .get("openapi")
                .and_then(|v| v.as_str())
                .unwrap_or("");
            match openapi_to_rust::cli::parse_oas_version(oas_version) {
                Some((3, 0)) | Some((3, 1)) => {}
                Some((3, 2)) => {
                    eprintln!("⚠️  OpenAPI {oas_version}: 3.2 is experimentally supported.");
                }
                Some((major, minor)) => {
                    eprintln!(
                        "❌ Unsupported OpenAPI version: {major}.{minor} ({oas_version:?}). \
                         This generator targets 3.0.x, 3.1.x, and (experimentally) 3.2.x. \
                         Swagger 2.0 and OAS 1.x are not supported."
                    );
                    std::process::exit(1);
                }
                None => {
                    let hint = if spec_value.get("swagger").is_some() {
                        " (looks like Swagger 2.0 — out of scope)"
                    } else {
                        ""
                    };
                    eprintln!(
                        "❌ Missing or unrecognised `openapi` field{hint}. Expected something like \"3.1.0\", got: {oas_version:?}"
                    );
                    std::process::exit(1);
                }
            }

            // Analyze schemas (with extensions if configured). Build a
            // TypeMapper from the user's [generator.types] config so
            // per-format strategies drive type generation (Q2.0).
            println!("🔍 Analyzing schemas...");
            let type_mapper = openapi_to_rust::TypeMapper::new(generator_config.types.clone());
            let mut analyzer = if generator_config.schema_extensions.is_empty() {
                SchemaAnalyzer::with_type_mapper(spec_value, type_mapper)?
            } else {
                println!(
                    "📎 Merging {} schema extension(s)",
                    generator_config.schema_extensions.len()
                );
                SchemaAnalyzer::new_with_extensions_and_type_mapper(
                    spec_value,
                    &generator_config.schema_extensions,
                    type_mapper,
                )?
            };
            let mut analysis = analyzer.analyze()?;

            println!("📊 Found {} schemas", analysis.schemas.len());
            println!("📊 Found {} operations", analysis.operations.len());

            // Optional: prune `analysis.schemas` to the transitive
            // closure reachable from the picked server operations.
            // Opt-in via `[server] prune_models = true`. When the HTTP
            // client is also enabled we warn that it'll lose types not
            // covered by the server scope.
            if let Some(server_section) = generator_config.server.as_ref()
                && server_section.prune_models
                && !server_section.operations.is_empty()
            {
                let pruned_count = prune_analysis_models(&mut analysis, server_section)?;
                println!(
                    "✂️  Pruned {pruned_count} schema(s) outside the server scope ({} remain)",
                    analysis.schemas.len()
                );
                if generator_config.enable_async_client || generator_config.enable_sse_client {
                    eprintln!(
                        "⚠️  prune_models = true: the HTTP client will only see types \
                         reachable from picked server operations. Set prune_models = false \
                         or extend [server].operations to keep additional types."
                    );
                }
            }

            // Generate code
            println!("⚙️  Generating code...");
            let generator = CodeGenerator::new(generator_config);
            let result = generator.generate_all(&mut analysis)?;

            // Write files
            generator.write_files(&result)?;

            println!(
                "✅ Generated {} files to {}",
                result.files.len(),
                generator.config().output_dir.display()
            );

            // P4: server-side scaffolding. Runs only when [server] is
            // set in the TOML and selectors resolve cleanly.
            if let Some(server_section) = generator.config().server.as_ref() {
                if !server_section.operations.is_empty() {
                    use openapi_to_rust::server::codegen::ServerCodegen;
                    println!("⚙️  Generating server scaffolding (axum)...");
                    let server_files =
                        ServerCodegen::new(generator.config(), &analysis, server_section)
                            .generate()?;
                    let out = generator.config().output_dir.clone();
                    let server_dir = out.join("server");
                    std::fs::create_dir_all(&server_dir)?;
                    for f in &server_files {
                        let path = out.join(&f.path);
                        if let Some(parent) = path.parent() {
                            std::fs::create_dir_all(parent)?;
                        }
                        std::fs::write(&path, &f.content)?;
                    }
                    // Append server module declaration to mod.rs.
                    let mod_path = out.join("mod.rs");
                    if mod_path.exists() {
                        let body = std::fs::read_to_string(&mod_path)?;
                        if !body.contains("pub mod server") {
                            let mut updated = body;
                            if !updated.ends_with('\n') {
                                updated.push('\n');
                            }
                            updated.push_str("\npub mod server;\npub use server::*;\n");
                            std::fs::write(&mod_path, updated)?;
                        }
                    }
                    println!(
                        "✅ Wrote {} server files to {}/server/",
                        server_files.len(),
                        out.display()
                    );
                    print_server_hint(&analysis, server_section);
                }
            }

            // Q2.8 dep advisory: surface optional crates the
            // generated code references so the operator knows what
            // to add to their Cargo.toml. write_files already
            // dropped a copy-pasteable REQUIRED_DEPS.toml next to
            // the generated module; the stderr summary makes it
            // discoverable without scanning the output dir.
            if !result.required_deps.is_empty() {
                eprintln!();
                eprintln!(
                    "📦 Generated code uses {} optional crate(s). Add to your Cargo.toml:",
                    result.required_deps.len()
                );
                eprintln!();
                eprintln!("[dependencies]");
                for dep in &result.required_deps {
                    eprintln!("{}", dep.to_toml_line());
                }
                eprintln!();
                eprintln!(
                    "(Same content written to {}/REQUIRED_DEPS.toml)",
                    generator.config().output_dir.display()
                );
            }

            Ok(())
        }
        Commands::Server { action } => match action {
            ServerCommands::List {
                spec,
                config,
                tag,
                method,
                grep,
                json,
            } => run_server_list(spec, config, tag, method, grep, json),
            ServerCommands::Add {
                selector,
                spec,
                config,
                all_tag,
                dry_run,
                regenerate,
            } => run_server_add(selector, spec, config, all_tag, dry_run, regenerate),
            ServerCommands::Remove {
                selector,
                config,
                dry_run,
            } => run_server_remove(selector, config, dry_run),
        },
    }
}

fn resolve_spec_path(
    spec: Option<PathBuf>,
    config: &std::path::Path,
) -> Result<PathBuf, Box<dyn std::error::Error>> {
    match spec {
        Some(p) => Ok(p),
        None => {
            let cf = ConfigFile::load(config).map_err(|e| {
                format!(
                    "no --spec provided and failed to load {}: {}",
                    config.display(),
                    e
                )
            })?;
            Ok(cf.into_generator_config().spec_path)
        }
    }
}

fn load_analysis(
    spec_path: &std::path::Path,
) -> Result<openapi_to_rust::SchemaAnalysis, Box<dyn std::error::Error>> {
    let spec_content = std::fs::read_to_string(spec_path)?;
    let spec_value: serde_json::Value = if spec_path.extension()
        == Some(std::ffi::OsStr::new("yaml"))
        || spec_path.extension() == Some(std::ffi::OsStr::new("yml"))
    {
        yaml_to_json_value(&spec_content)?
    } else {
        json_from_str_lossy(&spec_content)?
    };
    let mut analyzer = SchemaAnalyzer::new(spec_value)?;
    Ok(analyzer.analyze()?)
}

fn run_server_list(
    spec: Option<PathBuf>,
    config: PathBuf,
    tag: Option<String>,
    method: Option<String>,
    grep: Option<String>,
    json: bool,
) -> Result<(), Box<dyn std::error::Error>> {
    let spec_path = resolve_spec_path(spec, &config)?;
    let analysis = load_analysis(&spec_path)?;
    let index = OperationIndex::from_analysis(&analysis);

    let filter = ListFilter { tag, method, grep };
    let output = if json {
        ListOutput::Json
    } else {
        ListOutput::Table
    };
    let (body, _count) = render_list(&index, &filter, output);
    print!("{body}");
    Ok(())
}

fn run_server_add(
    selector: Option<String>,
    spec: Option<PathBuf>,
    config: PathBuf,
    all_tag: Option<String>,
    dry_run: bool,
    regenerate: bool,
) -> Result<(), Box<dyn std::error::Error>> {
    let spec_path = resolve_spec_path(spec, &config)?;
    let analysis = load_analysis(&spec_path)?;
    let index = OperationIndex::from_analysis(&analysis);

    // Determine which selectors to add. --all-tag expands; otherwise
    // the single positional selector is added verbatim.
    let to_add: Vec<String> = match (&selector, &all_tag) {
        (Some(_), Some(_)) => {
            return Err("provide either <selector> or --all-tag, not both".into());
        }
        (None, None) => {
            return Err("missing argument: provide <selector> or --all-tag <name>".into());
        }
        (Some(s), None) => vec![s.clone()],
        (None, Some(tag)) => {
            let sel = Selector::Tag(tag.clone());
            let res = resolve_selectors(&[sel], &index)?;
            res.operations
                .iter()
                .map(|op| op.operation_id.clone())
                .collect()
        }
    };

    // Validate every selector resolves before touching the file.
    for s in &to_add {
        let parsed = Selector::parse(s)?;
        let _ = resolve_selectors(&[parsed], &index)?;
    }

    let mut editor = ServerEditor::open(&config)?;
    let mut added: Vec<String> = Vec::new();
    let mut already_present: Vec<String> = Vec::new();
    for s in &to_add {
        if editor.add(s)? {
            added.push(s.clone());
        } else {
            already_present.push(s.clone());
        }
    }

    if dry_run {
        println!("--- dry-run: proposed config ---");
        print!("{}", editor.rendered());
        println!("--- end ---");
    } else {
        editor.save()?;
    }

    // Summary
    for s in &added {
        print_add_summary(s, &analysis, &index)?;
    }
    if !already_present.is_empty() {
        for s in &already_present {
            println!("• `{s}` already in [server].operations — no change.");
        }
    }
    if !dry_run && !added.is_empty() {
        let next_step = if regenerate {
            "Regenerating now..."
        } else {
            "Run `openapi-to-rust generate` to emit code."
        };
        println!(
            "\n✓ Added {} entr{} to {}. {next_step}",
            added.len(),
            if added.len() == 1 { "y" } else { "ies" },
            config.display(),
        );
        if regenerate {
            // Re-exec ourselves in `generate` mode against the same
            // config. We do this via the binary path (current_exe) so
            // we pick up the same compiled artefact the user is using.
            let exe = std::env::current_exe()?;
            let status = std::process::Command::new(exe)
                .arg("generate")
                .arg("--config")
                .arg(&config)
                .status()?;
            if !status.success() {
                return Err(format!("regenerate failed with status {status}").into());
            }
        }
    }
    Ok(())
}

fn print_add_summary(
    selector_str: &str,
    analysis: &openapi_to_rust::SchemaAnalysis,
    index: &OperationIndex,
) -> Result<(), Box<dyn std::error::Error>> {
    let parsed = Selector::parse(selector_str)?;
    let res = resolve_selectors(&[parsed], index)?;
    for op in &res.operations {
        let info = analysis
            .operations
            .get(&op.operation_id)
            .ok_or("operation found in index but missing from analysis")?;
        let tag_part = if op.tags.is_empty() {
            "<untagged>".to_string()
        } else {
            op.tags.join(",")
        };
        println!(
            "\n+ `{}`\n  {} {}  (tag: {})",
            selector_str, op.method, op.path, tag_part
        );
        if let Some(rb) = &info.request_body {
            if let Some(name) = rb.schema_name() {
                println!("  Request:  {name}");
            } else {
                println!("  Request:  (non-JSON body)");
            }
        } else {
            println!("  Request:  (none)");
        }
        if !info.response_schemas.is_empty() {
            let mut parts: Vec<String> = info
                .response_schemas
                .iter()
                .map(|(code, ty)| format!("{code}={ty}"))
                .collect();
            parts.sort();
            println!("  Response: {}", parts.join("  "));
        }
        println!(
            "  Streaming: {}",
            if op.supports_streaming { "yes" } else { "no" }
        );
    }
    Ok(())
}

/// Drop schemas from `analysis.schemas` that are unreachable from
/// the operations picked by `[server].operations`. Returns the count
/// of schemas removed.
fn prune_analysis_models(
    analysis: &mut openapi_to_rust::SchemaAnalysis,
    server: &openapi_to_rust::config::ServerSection,
) -> Result<usize, Box<dyn std::error::Error>> {
    use openapi_to_rust::server::OperationIndex;
    use openapi_to_rust::server::codegen::reachable_schemas;

    let index = OperationIndex::from_analysis(analysis);
    let selectors: Vec<Selector> = server
        .operations
        .iter()
        .map(|s| Selector::parse(s))
        .collect::<Result<_, _>>()?;
    let resolution = resolve_selectors(&selectors, &index)?;

    // Translate resolved summaries back to full OperationInfo refs
    // so reachability can walk request/response/parameter shapes.
    let ops: Vec<&openapi_to_rust::analysis::OperationInfo> = resolution
        .operations
        .iter()
        .filter_map(|s| analysis.operations.get(&s.operation_id))
        .collect();

    let keep = reachable_schemas(analysis, &ops);
    let before = analysis.schemas.len();
    analysis.schemas.retain(|k, _| keep.contains(k));
    Ok(before - analysis.schemas.len())
}

/// Surface a paste-ready impl skeleton at the end of `generate`.
/// Reads the picked operations from the analysis to name the trait,
/// method, and body type concretely. Goes to stderr so it doesn't
/// pollute machine-readable stdout consumers.
fn print_server_hint(
    analysis: &openapi_to_rust::SchemaAnalysis,
    server: &openapi_to_rust::config::ServerSection,
) {
    use heck::{ToPascalCase, ToSnakeCase};

    // Pick the first resolved op to ground the skeleton in concrete
    // names. Showing one is enough — users extrapolate to siblings.
    let first_op_id = server.operations.first().and_then(|raw| {
        Selector::parse(raw).ok().and_then(|sel| match sel {
            Selector::OperationId(id) => Some(id),
            Selector::MethodPath { method, path } => analysis
                .operations
                .values()
                .find(|op| op.method == method && op.path == path)
                .map(|op| op.operation_id.clone()),
            Selector::Tag(t) => analysis
                .operations
                .values()
                .find(|op| op.tags.iter().any(|tag| tag == &t))
                .map(|op| op.operation_id.clone()),
        })
    });

    let Some(first_op_id) = first_op_id else {
        return;
    };
    let Some(op) = analysis.operations.get(&first_op_id) else {
        return;
    };
    let method = op.operation_id.to_snake_case();
    let response_ty = format!("{}Response", op.operation_id.to_pascal_case());
    let body_param = match &op.request_body {
        Some(rb) => match rb.schema_name() {
            Some(name) => format!(", body: {name}"),
            None => String::new(),
        },
        None => String::new(),
    };
    let tag = op.tags.first().cloned().unwrap_or_else(|| "Server".into());
    let trait_name = format!("{}Api", tag.to_pascal_case());
    let router_fn = format!("{}_router", trait_name.to_snake_case());

    eprintln!();
    eprintln!("📝 Next step — implement the trait:");
    eprintln!();
    eprintln!("   #[derive(Clone)]");
    eprintln!("   pub struct AppState {{ /* state goes here */ }}");
    eprintln!();
    eprintln!("   #[axum::async_trait]");
    eprintln!("   impl {trait_name} for AppState {{");
    eprintln!("       async fn {method}(&self{body_param}) -> {response_ty} {{");
    eprintln!("           todo!()");
    eprintln!("       }}");
    eprintln!("   }}");
    eprintln!();
    eprintln!("   // In main():");
    eprintln!("   let app = {router_fn}(AppState {{ /* … */ }});");
    eprintln!();
    if op.supports_streaming {
        eprintln!("   For streaming, return `{response_ty}::OkStream(sse_response(your_stream))`.");
    }
}

fn run_server_remove(
    selector: String,
    config: PathBuf,
    dry_run: bool,
) -> Result<(), Box<dyn std::error::Error>> {
    let mut editor = ServerEditor::open(&config)?;
    let removed = editor.remove(&selector)?;
    if !removed {
        println!("• `{selector}` not present in [server].operations — no change.");
        return Ok(());
    }
    if dry_run {
        println!("--- dry-run: proposed config ---");
        print!("{}", editor.rendered());
        println!("--- end ---");
    } else {
        editor.save()?;
        println!(
            "✓ Removed `{selector}` from {}. Handler code in your crate may now be dead — review.",
            config.display()
        );
    }
    Ok(())
}