From 250fdb9f82e0db8c724d1c0b33fd39be43c83850 Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Thu, 2 Feb 2023 14:06:13 +0100 Subject: [PATCH] Can output parsed tree as JSON --- Cargo.lock | 2 ++ Cargo.toml | 2 ++ src/lib.rs | 6 ++++-- src/main.rs | 16 ++++++++++++++-- 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9c34ee9..892e8f8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -230,6 +230,8 @@ dependencies = [ "env_logger", "log", "okapi", + "serde", + "serde_json", "serde_yaml", ] diff --git a/Cargo.toml b/Cargo.toml index 0ed9dcc..70b1430 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,4 +10,6 @@ log = "0.4.17" env_logger = "0.10.0" clap = { version = "4.1.1", features = ["derive"] } okapi = "0.7.0-rc.1" +serde = { version = "1.0.152", features = ["derive"] } serde_yaml = "0.9.17" +serde_json = "1.0.91" diff --git a/src/lib.rs b/src/lib.rs index 5c24fc7..7d7ff24 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 { Null, Boolean, @@ -40,9 +41,10 @@ pub enum NodeType { Integer, } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, serde::Serialize)] pub struct TreeNode { pub name: String, + #[serde(flatten)] pub r#type: NodeType, } diff --git a/src/main.rs b/src/main.rs index feddb61..1257065 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use clap::Parser; +use clap::{Parser, Subcommand}; use openapi_parser::{build_tree, parse_schema}; /// Dump the tree structure of a schema element as dot file @@ -15,6 +15,16 @@ struct Args { /// 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() { @@ -30,5 +40,7 @@ fn main() { let schema = parse_schema(&file_content); 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()), + } }