47 lines
1.2 KiB
Rust
47 lines
1.2 KiB
Rust
use clap::{Parser, Subcommand};
|
|
use openapi_parser::{build_tree, parse_schema};
|
|
|
|
/// Dump the tree structure of a schema element as dot file
|
|
#[derive(Parser, Debug)]
|
|
#[command(author, version, about, long_about = None)]
|
|
struct Args {
|
|
/// The name of the file to dump
|
|
///
|
|
/// If this value is unspecified, the default petstore schema
|
|
/// is used instead
|
|
#[arg(short, long)]
|
|
file_name: Option<String>,
|
|
|
|
/// The name of the structure to dump
|
|
#[arg(short, long, default_value = "Pet")]
|
|
struct_name: String,
|
|
|
|
/// The action to perform
|
|
#[clap(subcommand)]
|
|
action: Action,
|
|
}
|
|
|
|
#[derive(Subcommand, Debug)]
|
|
enum Action {
|
|
/// Dump as JSON
|
|
Json,
|
|
}
|
|
|
|
fn main() {
|
|
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
|
|
|
|
let args = Args::parse();
|
|
|
|
let file_content = match args.file_name {
|
|
None => include_str!("../examples/petstore.yaml").to_string(),
|
|
Some(path) => std::fs::read_to_string(path).expect("Unable to load schema file!"),
|
|
};
|
|
|
|
let schema = parse_schema(&file_content);
|
|
let tree = build_tree(&args.struct_name, schema.components.as_ref().unwrap());
|
|
|
|
match args.action {
|
|
Action::Json => println!("{}", serde_json::to_string(&tree).unwrap()),
|
|
}
|
|
}
|