Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 91 additions & 5 deletions src/bin/ucp-schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ use clap::{Parser, Subcommand};
use ucp_schema::{
bundle_refs, bundle_refs_with_url_mapping, compose_from_payload, compose_schema,
detect_direction, extract_capabilities, extract_capabilities_from_profile,
extract_jsonrpc_payload, is_url, lint, load_schema, load_schema_auto, resolve,
select_operation_schema, validate, ComposeError, DetectedDirection, Direction, FileStatus,
ResolveError, ResolveOptions, SchemaBaseConfig, ValidateError,
extract_jsonrpc_payload, generate_types, is_url, lint, load_schema, load_schema_auto, resolve,
select_operation_schema, validate, CodegenError, ComposeError, DetectedDirection, Direction,
FileStatus, GenerateTypesOptions, ResolveError, ResolveOptions, SchemaBaseConfig,
ValidateError,
};

/// Errors with associated CLI exit codes.
Expand All @@ -31,6 +32,12 @@ impl CliExitCode for ComposeError {
}
}

impl CliExitCode for CodegenError {
fn exit_code(&self) -> u8 {
CodegenError::exit_code(self) as u8
}
}

/// Map an error to a CLI exit code, reporting it in the configured format.
fn cli_err<E: std::fmt::Display + CliExitCode>(json_output: bool) -> impl FnOnce(E) -> u8 {
move |e| {
Expand Down Expand Up @@ -237,6 +244,50 @@ enum Commands {
#[arg(long, short)]
quiet: bool,
},

/// Generate a self-contained JSON Schema 2020-12 type bundle ($defs) from a UCP profile or capability set
#[command(name = "generate-types")]
GenerateTypes {
/// Path or URL to a UCP discovery profile (e.g. profile.json or https://allbirds.com/.well-known/ucp)
#[arg(long, short = 'p')]
profile: Option<String>,

/// Explicit capability FQDN to include in flag mode (repeatable or comma-separated, e.g. dev.ucp.shopping.checkout)
#[arg(
long = "capability",
alias = "capabilities",
value_delimiter = ',',
conflicts_with = "profile"
)]
capabilities: Vec<String>,

/// Explicit capability extension FQDN to include and compose in flag mode (repeatable or comma-separated, e.g. dev.ucp.shopping.fulfillment)
#[arg(
long = "extension",
alias = "extensions",
value_delimiter = ',',
conflicts_with = "profile"
)]
extensions: Vec<String>,

/// Local directory containing UCP JSON schema files (required in flag mode without --profile; optional local URL-mapping override when --profile is provided)
#[arg(long, short = 's', alias = "schema-local-base")]
schema_dir: Option<PathBuf>,

/// Output file path (emits JSON to stdout if omitted)
#[arg(long, short = 'o')]
output: Option<PathBuf>,

/// Pretty-print JSON output (defaults to true; use --pretty=false for compact)
#[arg(
long,
default_value_t = true,
num_args = 0..=1,
default_missing_value = "true",
action = clap::ArgAction::Set
)]
pretty: bool,
},
}

fn main() -> ExitCode {
Expand Down Expand Up @@ -323,6 +374,22 @@ fn main() -> ExitCode {
strict,
quiet,
} => run_lint(&path, &format, strict, quiet),

Commands::GenerateTypes {
profile,
capabilities,
extensions,
schema_dir,
output,
pretty,
} => run_generate_types(
profile,
capabilities,
extensions,
schema_dir,
output,
pretty,
),
};

match result {
Expand Down Expand Up @@ -715,9 +782,28 @@ fn run_validate(args: ValidateArgs) -> Result<(), u8> {
}
}

fn run_generate_types(
profile: Option<String>,
capabilities: Vec<String>,
extensions: Vec<String>,
schema_dir: Option<PathBuf>,
output: Option<PathBuf>,
pretty: bool,
) -> Result<(), u8> {
let options = GenerateTypesOptions {
profile,
capabilities: (!capabilities.is_empty()).then_some(capabilities),
extensions: (!extensions.is_empty()).then_some(extensions),
schema_dir,
..GenerateTypesOptions::default()
};
let bundle = generate_types(&options).map_err(cli_err(false))?;
write_json_output(&bundle, output, pretty)
}

/// Shared helper: serialize JSON and write to output or stdout.
fn write_json_output(
value: &serde_json::Value,
fn write_json_output<T: serde::Serialize + ?Sized>(
value: &T,
output: Option<PathBuf>,
pretty: bool,
) -> Result<(), u8> {
Expand Down
Loading
Loading