From c4ceb92e11ce40e62151b7f3c62d946892510571 Mon Sep 17 00:00:00 2001 From: Sandipsinh Rathod Date: Sun, 22 Dec 2024 16:19:39 -0500 Subject: [PATCH] move openapi to openapi module --- src/config/config.rs | 5 +- src/config/fixargs.rs | 11 ++-- src/config/handle_files.rs | 14 +++-- src/config/mod.rs | 4 +- src/config/schema_document.rs | 101 ------------------------------ src/config/to_config.rs | 9 --- src/config/to_wit.rs | 8 +-- src/config/wit_types.rs | 4 +- src/main.rs | 16 ++--- src/merge_right.rs | 3 +- src/openapi/mod.rs | 2 + src/{ => openapi}/openapi.rs | 21 +------ src/{ => openapi}/openapi_spec.rs | 8 +-- 13 files changed, 38 insertions(+), 168 deletions(-) delete mode 100644 src/config/schema_document.rs delete mode 100644 src/config/to_config.rs create mode 100644 src/openapi/mod.rs rename src/{ => openapi}/openapi.rs (57%) rename src/{ => openapi}/openapi_spec.rs (97%) diff --git a/src/config/config.rs b/src/config/config.rs index 215fa92..84761df 100644 --- a/src/config/config.rs +++ b/src/config/config.rs @@ -18,7 +18,7 @@ pub struct World { pub exports: Vec, } -#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, MergeRight)] +#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, MergeRight, derive_setters::Setters)] pub struct Interface { pub name: String, pub records: BTreeSet, @@ -42,6 +42,7 @@ impl Ord for Interface { pub struct Record { pub name: String, pub fields: BTreeSet, + pub added_fields: BTreeSet, } impl PartialOrd for Record { @@ -84,7 +85,7 @@ pub struct Parameter { #[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, MergeRight)] pub struct Field { pub name: String, - pub field_type: String, + pub field_type: WitType, } impl PartialOrd for Field { diff --git a/src/config/fixargs.rs b/src/config/fixargs.rs index 957b3f1..0a8bfb4 100644 --- a/src/config/fixargs.rs +++ b/src/config/fixargs.rs @@ -1,12 +1,11 @@ -use std::collections::BTreeSet; use base64::Engine; use tailcall_valid::{Valid, Validator}; -use crate::config::schema_document::{SchemaDocument, Field, Interface, Record}; +use crate::config::config::{Config, Field, Interface, Record}; use crate::config::wit_types::WitType; use crate::merge_right::MergeRight; use crate::tryfold::TryFold; -pub fn fix_args(config: SchemaDocument) -> Valid { +pub fn fix_args(config: Config) -> Valid { Valid::succeed(config) .and_then(|mut config| { Valid::from_iter(config.interfaces.iter().cloned().collect::>(), |mut interface| { @@ -94,8 +93,8 @@ interface types { } */ -fn fix_field<'a>() -> TryFold<'a, (&'a SchemaDocument, &'a Interface, &'a Record, &'a Field, &'a WitType), Field, anyhow::Error> { - TryFold::<(&SchemaDocument, &Interface, &Record, &Field, &WitType), Field, anyhow::Error>::new(move |(config, interface, rec, field, wit), mut o| { +fn fix_field<'a>() -> TryFold<'a, (&'a Config, &'a Interface, &'a Record, &'a Field, &'a WitType), Field, anyhow::Error> { + TryFold::<(&Config, &Interface, &Record, &Field, &WitType), Field, anyhow::Error>::new(move |(config, interface, rec, field, wit), o| { match &wit { WitType::Option(x) => { return fix_field().try_fold(&(*config, *interface, *rec, *field, x), o); @@ -119,7 +118,7 @@ fn fix_field<'a>() -> TryFold<'a, (&'a SchemaDocument, &'a Interface, &'a Record WitType::Tuple(x) => { // TODO: Fix the possible conflicts here return Valid::from_iter(x.iter(), |x| fix_field().try_fold(&(*config, *interface, *rec, *field, x), o.clone())) - .and_then(|v| Valid::succeed(v.into_iter().fold((*field).clone(), |mut a, b| { + .and_then(|v| Valid::succeed(v.into_iter().fold((*field).clone(), |a, b| { a.merge_right(b) }))); } diff --git a/src/config/handle_files.rs b/src/config/handle_files.rs index fbf4d13..d24ffaa 100644 --- a/src/config/handle_files.rs +++ b/src/config/handle_files.rs @@ -1,11 +1,13 @@ -use std::collections::{BTreeSet, HashMap}; +use std::collections::BTreeSet; + use anyhow::{anyhow, Error}; use tailcall_valid::{Valid, Validator}; -use crate::config::schema_document::{SchemaDocument, Field, Interface, Record}; -use crate::config::wit_types::WitType; -use crate::openapi_spec::{OpenApiSpec, Resolved}; -pub fn handle_types(mut config: SchemaDocument, spec: &OpenApiSpec) -> Valid { +use crate::config::config::{Config, Field, Interface, Record}; +use crate::config::wit_types::WitType; +use crate::openapi::openapi_spec::{OpenApiSpec, Resolved}; + +pub fn handle_types(mut config: Config, spec: &OpenApiSpec) -> Valid { let mut generated_records = BTreeSet::new(); fn process_wit_type( @@ -60,7 +62,7 @@ pub fn handle_types(mut config: SchemaDocument, spec: &OpenApiSpec) -> .and_then(|schemas| { Valid::from_iter(schemas.iter(), |(record_name, schema)| { Valid::from_option(schema.type_.as_ref(), anyhow!("Type is required")) - .and_then(|type_| { + .and_then(|_type_| { Valid::from_option(schema.properties.as_ref(), anyhow!("Properties are required")) .and_then(|properties| { Valid::from_iter(properties, |(field_name, field_schema)| { diff --git a/src/config/mod.rs b/src/config/mod.rs index 8a18149..193fee1 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -1,7 +1,5 @@ -pub mod schema_document; +pub mod config; pub mod wit_types; pub mod to_wit; pub mod fixargs; pub mod handle_files; -pub mod config; -pub mod to_config; diff --git a/src/config/schema_document.rs b/src/config/schema_document.rs deleted file mode 100644 index 1d9299f..0000000 --- a/src/config/schema_document.rs +++ /dev/null @@ -1,101 +0,0 @@ -use std::collections::BTreeSet; -use serde::{Deserialize, Serialize}; -use macros::MergeRight; -use crate::config::wit_types::WitType; - -#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, MergeRight)] -pub struct SchemaDocument { - pub package: String, - pub interfaces: BTreeSet, - pub world: World, -} - -#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, MergeRight)] -pub struct World { - pub name: String, - pub uses: Vec, - pub imports: Vec, - pub exports: Vec, -} - -#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, MergeRight, derive_setters::Setters)] -pub struct Interface { - pub name: String, - pub records: BTreeSet, - pub uses: Vec, - pub functions: Vec, -} - -impl PartialOrd for Interface { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.name.cmp(&other.name)) - } -} - -impl Ord for Interface { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - self.name.cmp(&other.name) - } -} - -#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, MergeRight)] -pub struct Record { - pub name: String, - pub fields: BTreeSet, - pub added_fields: BTreeSet, -} - -impl PartialOrd for Record { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.name.cmp(&other.name)) - } -} - -impl Ord for Record { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - self.name.cmp(&other.name) - } -} - -#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, MergeRight)] -pub struct UseStatement { - pub name: String, - pub items: Vec, -} - -#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, MergeRight)] -pub struct Function { - pub name: String, - pub parameters: Vec, - pub return_type: ReturnTy, -} - -#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, MergeRight)] -pub struct ReturnTy { - pub return_type: String, - pub error_type: String, -} - -#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, MergeRight)] -pub struct Parameter { - pub name: String, - pub parameter_type: String, -} - -#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, MergeRight)] -pub struct Field { - pub name: String, - pub field_type: WitType, -} - -impl PartialOrd for Field { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.name.cmp(&other.name)) - } -} - -impl Ord for Field { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - self.name.cmp(&other.name) - } -} diff --git a/src/config/to_config.rs b/src/config/to_config.rs deleted file mode 100644 index 47143bb..0000000 --- a/src/config/to_config.rs +++ /dev/null @@ -1,9 +0,0 @@ -use tailcall_valid::Valid; -use crate::config::config::Config; -use crate::config::schema_document::SchemaDocument; - -impl SchemaDocument { - pub fn to_config(&self) -> Valid { - todo!() - } -} \ No newline at end of file diff --git a/src/config/to_wit.rs b/src/config/to_wit.rs index 62e05c3..80d87aa 100644 --- a/src/config/to_wit.rs +++ b/src/config/to_wit.rs @@ -1,12 +1,8 @@ -use crate::config::schema_document::{SchemaDocument, Field, Function, Interface, Parameter, Record, ReturnTy, UseStatement, World}; +use crate::config::config::{Config, Field, Function, Interface, Parameter, Record, ReturnTy, UseStatement, World}; use crate::config::wit_types::WitType; use convert_case::{Case, Casing}; use std::collections::HashSet; -pub trait ToWit { - fn to_wit(&self) -> String; -} - lazy_static::lazy_static! { static ref RESERVED_WORDS: HashSet<&'static str> = { let mut set = HashSet::new(); @@ -41,7 +37,7 @@ fn generate_wit_name(name: &str) -> String { final_name } -impl SchemaDocument { +impl Config { pub fn to_wit(&self) -> String { let package = format!("package {};\n", generate_wit_name(&self.package)); let world = self.world.to_wit(); diff --git a/src/config/wit_types.rs b/src/config/wit_types.rs index e492d1a..b60c0bb 100644 --- a/src/config/wit_types.rs +++ b/src/config/wit_types.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; use tailcall_valid::{Valid, Validator}; -use crate::openapi_spec::{OpenApiSpec, Resolved, Schema}; +use crate::openapi::openapi_spec::{OpenApiSpec, Resolved, Schema}; #[derive(Debug, Clone, Default, PartialEq, Eq, strum_macros::Display, Serialize, Deserialize)] pub enum WitType { @@ -102,7 +102,7 @@ impl WitType { #[cfg(test)] mod tests { use std::collections::HashMap; - use crate::openapi_spec::Components; + use crate::openapi::openapi_spec::Components; use super::*; #[test] diff --git a/src/main.rs b/src/main.rs index 30d9a63..53a1d55 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,11 @@ +use anyhow::{bail, Result}; +use convert_case::{Case, Casing}; +use serde_json::Value; + +use crate::value::value; + mod value; mod openapi; -mod openapi_spec; mod config; mod transformer; mod transform; @@ -8,11 +13,6 @@ mod tryfold; mod merge_right; mod primitive; -use serde_json::{Map, Value}; -use anyhow::{Result, bail}; -use convert_case::{Casing, Case}; -use crate::value::value; - #[derive(Debug, Clone)] pub struct WIPValue(Value); @@ -29,7 +29,7 @@ impl WIPValue { if let Value::Object(schemas_map) = schemas { wip.push_str("interface types {\n"); for (name, schema) in schemas_map { - let record = Self::process_schema(name, schema, schemas_map)?; + let record = Self::process_schema(name, schema)?; wip.push_str(&record); } wip.push_str("}\n\n"); @@ -57,7 +57,7 @@ impl WIPValue { } } - fn process_schema(name: &str, schema: &Value, schemas_map: &Map) -> Result { + fn process_schema(name: &str, schema: &Value) -> Result { if let Value::Object(schema_map) = schema { let mut record = format!(" record {} {{\n", name.from_case(Case::Camel).to_case(Case::Kebab)); if let Some(properties) = schema_map.get("properties") { diff --git a/src/merge_right.rs b/src/merge_right.rs index 150cf8d..b5b0883 100644 --- a/src/merge_right.rs +++ b/src/merge_right.rs @@ -1,4 +1,5 @@ use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; + use crate::config::wit_types::WitType; pub trait MergeRight { @@ -87,8 +88,6 @@ impl MergeRight for WitType { mod tests { use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; - use serde_json::json; - use super::MergeRight; #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] diff --git a/src/openapi/mod.rs b/src/openapi/mod.rs new file mode 100644 index 0000000..241f8ed --- /dev/null +++ b/src/openapi/mod.rs @@ -0,0 +1,2 @@ +pub mod openapi_spec; +pub mod openapi; \ No newline at end of file diff --git a/src/openapi.rs b/src/openapi/openapi.rs similarity index 57% rename from src/openapi.rs rename to src/openapi/openapi.rs index 584d3fd..55e6681 100644 --- a/src/openapi.rs +++ b/src/openapi/openapi.rs @@ -1,25 +1,8 @@ -use anyhow::Error; -use tailcall_valid::{Valid, Validator}; - -use crate::config::schema_document::SchemaDocument; -use crate::config::fixargs::fix_args; -use crate::config::handle_files::handle_types; -use crate::openapi_spec::OpenApiSpec; - -fn to_config(spec: OpenApiSpec) -> Valid { - let spec = spec.into_resolved(); - Valid::succeed(SchemaDocument::default()) - .and_then(|config| handle_types(config, &spec)) - .and_then(|config| fix_args(config)) -} - #[cfg(test)] mod t { use tailcall_valid::Validator; use wit_parser::Resolve; - - use crate::openapi::to_config; - use crate::openapi_spec::OpenApiSpec; + use crate::openapi::openapi_spec::OpenApiSpec; #[test] fn tp() { @@ -27,7 +10,7 @@ mod t { let content = std::fs::read_to_string(path).unwrap(); let y: OpenApiSpec = serde_yaml::from_str(&content).unwrap(); - let mut res = to_config(y).to_result(); + let mut res = y.into_config().to_result(); match res.as_mut() { Ok(v) => { // println!("{}", serde_json::to_string_pretty(v).unwrap()); diff --git a/src/openapi_spec.rs b/src/openapi/openapi_spec.rs similarity index 97% rename from src/openapi_spec.rs rename to src/openapi/openapi_spec.rs index 8daf08b..b1a12c1 100644 --- a/src/openapi_spec.rs +++ b/src/openapi/openapi_spec.rs @@ -6,7 +6,7 @@ use schemars::JsonSchema; use tailcall_valid::{Valid, Validator}; use crate::config::fixargs::fix_args; use crate::config::handle_files::handle_types; -use crate::config::schema_document::SchemaDocument; +use crate::config::config::Config; #[derive(Default)] pub struct Unresolved; @@ -247,15 +247,15 @@ pub struct XML { } impl OpenApiSpec { - fn to_config(&self) -> Valid { - Valid::succeed(SchemaDocument::default()) + pub fn to_config(&self) -> Valid { + Valid::succeed(Config::default()) .and_then(|config| handle_types(config, &self)) .and_then(|config| fix_args(config)) } } impl OpenApiSpec { - fn into_config(self) -> Valid { + pub fn into_config(self) -> Valid { self.into_resolved() .to_config() }