From e5874141b3b6256b7ac2a0bf653089fa7bcc5d14 Mon Sep 17 00:00:00 2001 From: kjuulh Date: Sun, 29 Jan 2023 15:19:21 +0100 Subject: [PATCH] added scalars --- Cargo.lock | 73 +++++++++++++++++++ Cargo.toml | 5 ++ crates/dagger-codegen/src/codegen.rs | 13 +++- crates/dagger-codegen/src/handlers/mod.rs | 30 ++++---- crates/dagger-codegen/src/handlers/scalar.rs | 30 +++++++- crates/dagger-codegen/src/handlers/utility.rs | 15 ++++ crates/dagger-codegen/src/lib.rs | 2 +- src/cli_generate.rs | 5 +- src/lib.rs | 10 --- src/main.rs | 22 ++++++ 10 files changed, 170 insertions(+), 35 deletions(-) create mode 100644 crates/dagger-codegen/src/handlers/utility.rs delete mode 100644 src/lib.rs create mode 100644 src/main.rs diff --git a/Cargo.lock b/Cargo.lock index c8b3587..fe109c6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -163,11 +163,35 @@ dependencies = [ "typenum", ] +[[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" +dependencies = [ + "eyre", + "genco", + "graphql-introspection-query", + "itertools", + "pretty_assertions", + "serde", + "serde_json", +] + [[package]] name = "dagger-rs" version = "0.1.2" dependencies = [ "clap", + "dagger-codegen", "dirs", "eyre", "flate2", @@ -185,6 +209,19 @@ dependencies = [ "tempfile", ] +[[package]] +name = "dagger-sdk" +version = "0.1.0" +dependencies = [ + "genco", +] + +[[package]] +name = "diff" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8" + [[package]] name = "digest" version = "0.10.6" @@ -672,6 +709,15 @@ dependencies = [ "windows-sys", ] +[[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" @@ -843,6 +889,15 @@ version = "6.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b7820b9daea5457c9f21c69448905d723fbd21136ccf521748f23fd49e723ee" +[[package]] +name = "output_vt100" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "628223faebab4e3e40667ee0b2336d34a5b960ff60ea743ddfdbcf7770bcfb66" +dependencies = [ + "winapi", +] + [[package]] name = "percent-encoding" version = "2.2.0" @@ -877,6 +932,18 @@ 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" @@ -1535,3 +1602,9 @@ checksum = "6d1526bbe5aaeb5eb06885f4d987bcdfa5e23187055de9b83fe00156a821fabc" dependencies = [ "libc", ] + +[[package]] +name = "yansi" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec" diff --git a/Cargo.toml b/Cargo.toml index f4df823..5117cf4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,12 @@ description = "A dagger sdk for rust, written in rust" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[workspace] +members = ["crates/dagger-codegen", "crates/dagger-sdk"] + [dependencies] +dagger-codegen = { path = "crates/dagger-codegen" } + clap = "4.1.4" dirs = "4.0.0" eyre = "0.6.8" diff --git a/crates/dagger-codegen/src/codegen.rs b/crates/dagger-codegen/src/codegen.rs index 32036bd..e75eebe 100644 --- a/crates/dagger-codegen/src/codegen.rs +++ b/crates/dagger-codegen/src/codegen.rs @@ -1,5 +1,6 @@ use std::sync::Arc; +use genco::{prelude::rust, quote, quote_in}; use graphql_introspection_query::introspection_response::{ FullType, IntrospectionResponse, Schema, }; @@ -34,8 +35,10 @@ impl CodeGeneration { } fn generate_from_schema(&self, schema: &Schema) -> eyre::Result { - let mut output = String::new(); - output.push_str("# code generated by dagger. DO NOT EDIT."); + let mut output = rust::Tokens::new(); + output.append(quote! { + $(format!("// code generated by dagger. DO NOT EDIT.")) + }); let types = get_types(schema)?; //let remaining: Vec> = types.into_iter().map(type_name).collect(); @@ -44,12 +47,14 @@ impl CodeGeneration { for t in types { if let Some(_) = self.type_name(&t) { let rendered = handler.render(&t)?; - output.push_str(&rendered) + + output.push(); + output.append(rendered); } } } - Ok(output) + Ok(output.to_string()?) } pub fn group_by_handlers(&self, types: &Vec<&FullType>) -> Vec<(DynHandler, Vec)> { diff --git a/crates/dagger-codegen/src/handlers/mod.rs b/crates/dagger-codegen/src/handlers/mod.rs index 29080cf..844f7a7 100644 --- a/crates/dagger-codegen/src/handlers/mod.rs +++ b/crates/dagger-codegen/src/handlers/mod.rs @@ -1,4 +1,5 @@ pub mod scalar; +mod utility; use std::sync::Arc; @@ -11,16 +12,15 @@ pub trait Handler { false } - fn render(&self, t: &FullType) -> eyre::Result { - let mut s = String::new(); - - s.push_str("\n"); - s.push_str(self.render_struct(t)?.to_string()?.as_str()); - s.push_str("\n"); - s.push_str(self.render_impl(t)?.to_string()?.as_str()); - s.push_str("\n"); - - Ok(s) + fn render(&self, t: &FullType) -> eyre::Result { + let tstruct = self.render_struct(t)?; + let timpl = self.render_impl(t)?; + let mut out = rust::Tokens::new(); + out.append(tstruct); + out.push(); + out.append(timpl); + out.push(); + Ok(out) } fn render_struct(&self, t: &FullType) -> eyre::Result { @@ -74,12 +74,10 @@ mod tests { let res = handler.render(&t).unwrap(); assert_eq!( - res, - " -pub struct SomeName {} { } -impl SomeName {} { } -" - .to_string() + res.to_string().unwrap(), + "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 index 5c8c553..b3f0df9 100644 --- a/crates/dagger-codegen/src/handlers/scalar.rs +++ b/crates/dagger-codegen/src/handlers/scalar.rs @@ -1,9 +1,9 @@ -use genco::Tokens; +use genco::{prelude::rust, quote}; use graphql_introspection_query::introspection_response::FullType; use crate::predicates::is_custom_scalar_type; -use super::Handler; +use super::{utility::render_description, Handler}; pub struct Scalar; @@ -11,4 +11,30 @@ impl Handler for Scalar { fn predicate(&self, t: &FullType) -> bool { is_custom_scalar_type(t) } + + fn render(&self, t: &FullType) -> eyre::Result { + let mut out = rust::Tokens::new(); + + let description = + render_description(t).ok_or(eyre::anyhow!("could not find description"))?; + let tstruct = self.render_struct(t)?; + + out.append(description); + out.push(); + out.append(tstruct); + + Ok(out) + } + + fn render_struct(&self, t: &FullType) -> eyre::Result { + let name = t.name.as_ref().ok_or(eyre::anyhow!("name not found"))?; + + Ok(quote! { + pub struct $name (Scalar); + }) + } + + fn render_impl(&self, t: &FullType) -> eyre::Result { + todo!() + } } diff --git a/crates/dagger-codegen/src/handlers/utility.rs b/crates/dagger-codegen/src/handlers/utility.rs new file mode 100644 index 0000000..46dc15b --- /dev/null +++ b/crates/dagger-codegen/src/handlers/utility.rs @@ -0,0 +1,15 @@ +use genco::{prelude::*, quote}; +use graphql_introspection_query::introspection_response::FullType; + +pub fn render_description(t: &FullType) -> Option { + if let Some(description) = t.description.as_ref() { + let lines = description.split('\n'); + let output: rust::Tokens = quote! { + $(for line in lines => $(format!("\n/// {line}"))) + }; + + return Some(output); + } + + None +} diff --git a/crates/dagger-codegen/src/lib.rs b/crates/dagger-codegen/src/lib.rs index 55a2b1a..9872943 100644 --- a/crates/dagger-codegen/src/lib.rs +++ b/crates/dagger-codegen/src/lib.rs @@ -1,4 +1,4 @@ -mod codegen; +pub mod codegen; mod handlers; mod models; mod predicates; diff --git a/src/cli_generate.rs b/src/cli_generate.rs index 8780fe4..14696ce 100644 --- a/src/cli_generate.rs +++ b/src/cli_generate.rs @@ -1,4 +1,5 @@ use clap::{Arg, ArgMatches}; +use dagger_codegen::codegen::CodeGeneration; use crate::{config::Config, engine::Engine, session::Session}; @@ -17,12 +18,12 @@ impl GenerateCommand { let session = Session::new(); let req = session.start(&cfg, &conn)?; let schema = session.schema(req)?; - //let code = CodeGeneration::generate(&schema)?; + let code = CodeGeneration::new().generate(&schema)?; if let Some(output) = arg_matches.get_one::("output") { // TODO: Write to file } else { - //println!("{}", code); + println!("{}", code); } Ok(()) diff --git a/src/lib.rs b/src/lib.rs deleted file mode 100644 index 376eedd..0000000 --- a/src/lib.rs +++ /dev/null @@ -1,10 +0,0 @@ -pub mod cli; -mod cli_generate; -mod cli_session; -mod config; -mod connect_params; -pub mod dagger; -mod downloader; -mod engine; -mod schema; -mod session; diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..5473f75 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,22 @@ +use cli::Cli; + +pub mod cli; +mod cli_generate; +mod cli_session; +mod config; +mod connect_params; +pub mod dagger; +mod downloader; +mod engine; +mod schema; +mod session; + +fn main() -> eyre::Result<()> { + let args = std::env::args(); + let args = args.collect::>(); + let args = args.iter().map(|s| s.as_str()).collect::>(); + + Cli::new()?.execute(args.as_slice())?; + + Ok(()) +}