diff --git a/crates/dagger-codegen/Cargo.lock b/crates/dagger-codegen/Cargo.lock index 3a12015..eac50c0 100644 --- a/crates/dagger-codegen/Cargo.lock +++ b/crates/dagger-codegen/Cargo.lock @@ -2,6 +2,16 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "ctor" +version = "0.1.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d2301688392eb071b0bf1a37be05c469d3cc4dbbd95df672fe28ab021e6a096" +dependencies = [ + "quote", + "syn", +] + [[package]] name = "dagger-codegen" version = "0.1.0" @@ -9,10 +19,24 @@ dependencies = [ "eyre", "genco", "graphql-introspection-query", + "itertools", + "pretty_assertions", "serde", "serde_json", ] +[[package]] +name = "diff" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8" + +[[package]] +name = "either" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91" + [[package]] name = "eyre" version = "0.6.8" @@ -60,6 +84,15 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683" +[[package]] +name = "itertools" +version = "0.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" +dependencies = [ + "either", +] + [[package]] name = "itoa" version = "1.0.5" @@ -72,6 +105,27 @@ version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6f61fba1741ea2b3d6a1e3178721804bb716a68a6aeba1149b5d52e3d464ea66" +[[package]] +name = "output_vt100" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "628223faebab4e3e40667ee0b2336d34a5b960ff60ea743ddfdbcf7770bcfb66" +dependencies = [ + "winapi", +] + +[[package]] +name = "pretty_assertions" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a25e9bcb20aa780fd0bb16b72403a9064d6b3f22f026946029acb941a50af755" +dependencies = [ + "ctor", + "diff", + "output_vt100", + "yansi", +] + [[package]] name = "proc-macro2" version = "1.0.50" @@ -155,3 +209,31 @@ name = "unicode-ident" version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "yansi" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec" diff --git a/crates/dagger-codegen/Cargo.toml b/crates/dagger-codegen/Cargo.toml index 49ae920..2b2544a 100644 --- a/crates/dagger-codegen/Cargo.toml +++ b/crates/dagger-codegen/Cargo.toml @@ -9,5 +9,7 @@ edition = "2021" eyre = "0.6.8" genco = "0.17.3" graphql-introspection-query = "0.2.0" +itertools = "0.10.5" +pretty_assertions = "1.3.0" serde = { version = "1.0.152", features = ["derive"] } serde_json = "1.0.91" diff --git a/crates/dagger-codegen/src/codegen.rs b/crates/dagger-codegen/src/codegen.rs index 765a367..32036bd 100644 --- a/crates/dagger-codegen/src/codegen.rs +++ b/crates/dagger-codegen/src/codegen.rs @@ -1,9 +1,12 @@ +use std::sync::Arc; + use graphql_introspection_query::introspection_response::{ FullType, IntrospectionResponse, Schema, }; +use itertools::Itertools; use crate::{ - handlers::{DynHandler, Handlers}, + handlers::{scalar::Scalar, DynHandler, Handlers}, predicates::is_custom_scalar_type, }; @@ -14,7 +17,9 @@ pub struct CodeGeneration { impl CodeGeneration { pub fn new() -> Self { - Self { handlers: vec![] } + Self { + handlers: vec![Arc::new(Scalar {})], + } } pub fn generate(&self, schema: &IntrospectionResponse) -> eyre::Result { @@ -34,8 +39,34 @@ impl CodeGeneration { let types = get_types(schema)?; //let remaining: Vec> = types.into_iter().map(type_name).collect(); + // + for (handler, types) in self.group_by_handlers(&types) { + for t in types { + if let Some(_) = self.type_name(&t) { + let rendered = handler.render(&t)?; + output.push_str(&rendered) + } + } + } - todo!() + Ok(output) + } + + pub fn group_by_handlers(&self, types: &Vec<&FullType>) -> Vec<(DynHandler, Vec)> { + let mut group = vec![]; + + for handler in self.handlers.iter() { + let mut group_types: Vec = vec![]; + for t in types.iter() { + if handler.predicate(*t) { + group_types.push(t.clone().clone()); + } + } + + group.push((handler.clone(), group_types)) + } + + group } pub fn type_name(&self, t: &FullType) -> Option { diff --git a/crates/dagger-codegen/src/handlers/mod.rs b/crates/dagger-codegen/src/handlers/mod.rs index 09588f5..29080cf 100644 --- a/crates/dagger-codegen/src/handlers/mod.rs +++ b/crates/dagger-codegen/src/handlers/mod.rs @@ -1,3 +1,5 @@ +pub mod scalar; + use std::sync::Arc; use genco::prelude::rust::Tokens; @@ -25,7 +27,7 @@ pub trait Handler { let name = t.name.as_ref().ok_or(eyre::anyhow!("name not found"))?; Ok(quote! { - pub $name {} { + pub struct $name {} { // TODO: Add fields } }) @@ -35,7 +37,7 @@ pub trait Handler { let name = t.name.as_ref().ok_or(eyre::anyhow!("name not found"))?; Ok(quote! { - pub $name {} { + impl $name {} { // TODO: Add fields } }) @@ -48,6 +50,7 @@ pub type Handlers = Vec; #[cfg(test)] mod tests { use graphql_introspection_query::introspection_response::FullType; + use pretty_assertions::assert_eq; use super::Handler; @@ -73,8 +76,8 @@ mod tests { assert_eq!( res, " -pub SomeName {} { } -pub SomeName {} { } +pub struct SomeName {} { } +impl SomeName {} { } " .to_string() ); diff --git a/crates/dagger-codegen/src/handlers/scalar.rs b/crates/dagger-codegen/src/handlers/scalar.rs new file mode 100644 index 0000000..5c8c553 --- /dev/null +++ b/crates/dagger-codegen/src/handlers/scalar.rs @@ -0,0 +1,14 @@ +use genco::Tokens; +use graphql_introspection_query::introspection_response::FullType; + +use crate::predicates::is_custom_scalar_type; + +use super::Handler; + +pub struct Scalar; + +impl Handler for Scalar { + fn predicate(&self, t: &FullType) -> bool { + is_custom_scalar_type(t) + } +} diff --git a/crates/dagger-codegen/src/lib.rs b/crates/dagger-codegen/src/lib.rs index 7038844..55a2b1a 100644 --- a/crates/dagger-codegen/src/lib.rs +++ b/crates/dagger-codegen/src/lib.rs @@ -9,6 +9,8 @@ mod tests { use crate::codegen::CodeGeneration; + use pretty_assertions::assert_eq; + #[test] fn can_generate_from_schema() { let schema: IntrospectionResponse = serde_json::from_str(INTROSPECTION_QUERY).unwrap();