added scalars

This commit is contained in:
Kasper Juul Hermansen 2023-01-29 15:19:21 +01:00
parent 0d6e6e57ae
commit e5874141b3
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912
10 changed files with 170 additions and 35 deletions

73
Cargo.lock generated
View File

@ -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"

View File

@ -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"

View File

@ -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<String> {
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<Option<String>> = 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<FullType>)> {

View File

@ -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<String> {
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<rust::Tokens> {
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<Tokens> {
@ -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()
);
}
}

View File

@ -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<rust::Tokens> {
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<genco::prelude::rust::Tokens> {
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<genco::prelude::rust::Tokens> {
todo!()
}
}

View File

@ -0,0 +1,15 @@
use genco::{prelude::*, quote};
use graphql_introspection_query::introspection_response::FullType;
pub fn render_description(t: &FullType) -> Option<rust::Tokens> {
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
}

View File

@ -1,4 +1,4 @@
mod codegen;
pub mod codegen;
mod handlers;
mod models;
mod predicates;

View File

@ -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::<String>("output") {
// TODO: Write to file
} else {
//println!("{}", code);
println!("{}", code);
}
Ok(())

View File

@ -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;

22
src/main.rs Normal file
View File

@ -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::<Vec<String>>();
let args = args.iter().map(|s| s.as_str()).collect::<Vec<&str>>();
Cli::new()?.execute(args.as_slice())?;
Ok(())
}