added scalars

This commit is contained in:
2023-01-29 15:19:21 +01:00
parent 0d6e6e57ae
commit e5874141b3
10 changed files with 170 additions and 35 deletions

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;