Can output parsed tree as JSON

This commit is contained in:
Pierre HUBERT 2023-02-02 14:06:13 +01:00
parent 9be4e5ee89
commit 250fdb9f82
4 changed files with 22 additions and 4 deletions

2
Cargo.lock generated
View File

@ -230,6 +230,8 @@ dependencies = [
"env_logger", "env_logger",
"log", "log",
"okapi", "okapi",
"serde",
"serde_json",
"serde_yaml", "serde_yaml",
] ]

View File

@ -10,4 +10,6 @@ log = "0.4.17"
env_logger = "0.10.0" env_logger = "0.10.0"
clap = { version = "4.1.1", features = ["derive"] } clap = { version = "4.1.1", features = ["derive"] }
okapi = "0.7.0-rc.1" okapi = "0.7.0-rc.1"
serde = { version = "1.0.152", features = ["derive"] }
serde_yaml = "0.9.17" serde_yaml = "0.9.17"
serde_json = "1.0.91"

View File

@ -29,7 +29,8 @@ fn expect_schema_object(s: &Schema) -> &SchemaObject {
} }
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone, serde::Serialize)]
#[serde(tag = "type")]
pub enum NodeType { pub enum NodeType {
Null, Null,
Boolean, Boolean,
@ -40,9 +41,10 @@ pub enum NodeType {
Integer, Integer,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone, serde::Serialize)]
pub struct TreeNode { pub struct TreeNode {
pub name: String, pub name: String,
#[serde(flatten)]
pub r#type: NodeType, pub r#type: NodeType,
} }

View File

@ -1,4 +1,4 @@
use clap::Parser; use clap::{Parser, Subcommand};
use openapi_parser::{build_tree, parse_schema}; use openapi_parser::{build_tree, parse_schema};
/// Dump the tree structure of a schema element as dot file /// Dump the tree structure of a schema element as dot file
@ -15,6 +15,16 @@ struct Args {
/// The name of the structure to dump /// The name of the structure to dump
#[arg(short, long, default_value = "Pet")] #[arg(short, long, default_value = "Pet")]
struct_name: String, struct_name: String,
/// The action to perform
#[clap(subcommand)]
action: Action,
}
#[derive(Subcommand, Debug)]
enum Action {
/// Dump as JSON
Json,
} }
fn main() { fn main() {
@ -30,5 +40,7 @@ fn main() {
let schema = parse_schema(&file_content); let schema = parse_schema(&file_content);
let tree = build_tree(&args.struct_name, schema.components.as_ref().unwrap()); let tree = build_tree(&args.struct_name, schema.components.as_ref().unwrap());
println!("{:#?}", tree); match args.action {
Action::Json => println!("{}", serde_json::to_string(&tree).unwrap()),
}
} }