From d9187b2296c893b019759c0393a46ddfd5f25d8a Mon Sep 17 00:00:00 2001 From: Mendy Man Date: Wed, 12 Mar 2025 18:40:37 -0400 Subject: [PATCH] Add `include-mode` to select which operations to include Keep the same default behavior to only include public operations --- src/api.rs | 16 ++++++++++++---- src/main.rs | 19 +++++++++++++++---- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/api.rs b/src/api.rs index 4ac242c..38655c6 100644 --- a/src/api.rs +++ b/src/api.rs @@ -9,6 +9,7 @@ use serde::Serialize; use crate::{ types::{FieldType, Types}, util::{get_schema_name, serialize_btree_map_values}, + IncludeMode, }; /// The API we generate a client for. @@ -24,7 +25,7 @@ impl Api { pub(crate) fn new( paths: openapi::Paths, component_schemas: &IndexMap, - include_hidden: bool, + include_mode: IncludeMode, ) -> anyhow::Result { let mut resources = BTreeMap::new(); @@ -40,7 +41,7 @@ impl Api { for (method, op) in path_item { if let Some((res_path, op)) = - Operation::from_openapi(&path, method, op, component_schemas, include_hidden) + Operation::from_openapi(&path, method, op, component_schemas, include_mode) { let resource = get_or_insert_resource(&mut resources, res_path); resource.operations.push(op); @@ -184,7 +185,7 @@ impl Operation { method: &str, op: openapi::Operation, component_schemas: &IndexMap, - include_hidden: bool, + include_mode: IncludeMode, ) -> Option<(Vec, Self)> { let Some(op_id) = op.operation_id else { // ignore operations without an operationId @@ -192,7 +193,14 @@ impl Operation { }; tracing::Span::current().record("op_id", &op_id); - if !include_hidden && op.extensions.get("x-hidden").is_some_and(|val| val == true) { + // verbose, but very easy to understand + let x_hidden = op.extensions.get("x-hidden").is_some_and(|val| val == true); + let include_operation = match include_mode { + IncludeMode::OnlyPublic => !x_hidden, + IncludeMode::PublicAndHidden => true, + IncludeMode::OnlyHidden => x_hidden, + }; + if !include_operation { return None; } diff --git a/src/main.rs b/src/main.rs index f10e23e..a019c5b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -53,15 +53,26 @@ struct GenerateFlags { #[clap(long)] no_postprocess: bool, - /// Include operations in the output that are marked `"x-hidden": true`. - #[clap(long)] - include_hidden: bool, + /// Which operations to include + #[clap(long, value_enum, default_value_t=IncludeMode::OnlyPublic)] + include_mode: IncludeMode, /// Write api.ron and types.ron files, as a debugging aid. #[clap(long)] debug: bool, } +#[derive(Copy, Clone, clap::ValueEnum)] +#[clap(rename_all = "kebab-case")] +enum IncludeMode { + /// Only public options + OnlyPublic, + /// Both public operations and operations marked with `x-hidden` + PublicAndHidden, + /// Only operations marked with `x-hidden` + OnlyHidden, +} + fn main() -> anyhow::Result<()> { tracing_subscriber::fmt().with_writer(io::stderr).init(); @@ -119,7 +130,7 @@ fn analyze_and_generate( let webhooks = get_webhooks(&spec); let mut components = spec.components.unwrap_or_default(); if let Some(paths) = spec.paths { - let api = Api::new(paths, &components.schemas, flags.include_hidden).unwrap(); + let api = Api::new(paths, &components.schemas, flags.include_mode).unwrap(); let types = api.types(&mut components.schemas, webhooks); if flags.debug {