#![expect(dead_code, reason = "example data types are reflected rather than all 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 = "workspacectl")]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Debug, Subcommand, CommandSchema)]
enum Commands {
#[command(subcommand)]
Workspaces(WorkspacesCommands),
}
#[derive(Debug, Subcommand, CommandSchema)]
enum WorkspacesCommands {
Get(WorkspacesGetCommand),
}
#[derive(Debug, Args)]
struct WorkspacesGetCommand {
workspace_id: u64,
}
struct CliContext;
enum OutputMode {
Human,
}
#[derive(Debug, Serialize, JsonSchema)]
struct Workspace {
id: u64,
name: String,
}
#[schema_handler(WorkspacesGetCommand)]
fn get_workspace(
_ctx: CliContext,
command: &WorkspacesGetCommand,
_output: OutputMode,
) -> Result<Workspace, Infallible> {
Ok(Workspace { id: command.workspace_id, name: "Example workspace".to_owned() })
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let contract = Cli::schema()?;
let command = contract
.command_for::<WorkspacesGetCommand>()
.expect("workspaces get command is registered");
println!("Command contract selected by Rust type:");
println!("{}", serde_json::to_string_pretty(&command)?);
let cli = Cli::parse_from(["workspacectl", "workspaces", "get", "42"]);
let Commands::Workspaces(workspaces) = cli.command;
let WorkspacesCommands::Get(request) = workspaces;
let workspace = get_workspace(CliContext, &request, OutputMode::Human)?;
println!("\nRuntime result from the same command type:");
println!("{}", serde_json::to_string_pretty(&workspace)?);
Ok(())
}