#![expect(dead_code, reason = "example data types are reflected rather than executed")]
use std::convert::Infallible;
use clap::{Args, Parser, Subcommand};
use clap_schema::{CliSchema, CommandSchema, schema_handler};
use schemars::JsonSchema;
use serde::Serialize;
#[derive(Debug, Parser, CliSchema)]
#[command(name = "items")]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Debug, Subcommand, CommandSchema)]
enum Commands {
Create(CreateArgs),
}
#[derive(Debug, Args)]
struct CreateArgs {
#[arg(long)]
name: String,
}
#[derive(Debug, Serialize, JsonSchema)]
struct Item {
id: u64,
name: String,
}
#[schema_handler(CreateArgs)]
fn create(args: CreateArgs) -> Result<Item, Infallible> {
Ok(Item { id: 42, name: args.name })
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let contract = Cli::schema()?;
let command = contract.command_for::<CreateArgs>().expect("create command is registered");
println!("Command contract:");
println!("{}", serde_json::to_string_pretty(&command)?);
let created = create(CreateArgs { name: "example".to_owned() })?;
println!("\nRuntime value from the same handler:");
println!("{}", serde_json::to_string_pretty(&created)?);
Ok(())
}