with enum

This commit is contained in:
2023-01-29 20:14:52 +01:00
parent 0bf6b0e91e
commit 2a1f7c3f26
14 changed files with 97 additions and 19 deletions

View File

@@ -6,6 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
dagger-core = { path = "../dagger-core" }
eyre = "0.6.8"
genco = "0.17.3"
graphql-introspection-query = "0.2.0"

View File

@@ -0,0 +1 @@
nightly

View File

@@ -1,12 +1,15 @@
use std::sync::Arc;
use std::{
io::{BufWriter, Write},
sync::Arc,
};
use genco::{prelude::rust, quote};
use genco::{fmt, prelude::rust, prelude::*, quote};
use graphql_introspection_query::introspection_response::{
FullType, IntrospectionResponse, Schema,
};
use crate::{
handlers::{scalar::Scalar, DynHandler, Handlers},
handlers::{enumeration::Enumeration, scalar::Scalar, DynHandler, Handlers},
predicates::is_custom_scalar_type,
};
@@ -18,7 +21,7 @@ pub struct CodeGeneration {
impl CodeGeneration {
pub fn new() -> Self {
Self {
handlers: vec![Arc::new(Scalar {})],
handlers: vec![Arc::new(Scalar {}), Arc::new(Enumeration {})],
}
}
@@ -35,6 +38,7 @@ impl CodeGeneration {
fn generate_from_schema(&self, schema: &Schema) -> eyre::Result<String> {
let mut output = rust::Tokens::new();
output.push();
output.append(quote! {
$(format!("// code generated by dagger. DO NOT EDIT."))
});
@@ -52,7 +56,17 @@ impl CodeGeneration {
}
}
Ok(output.to_string()?)
let mut buffer = BufWriter::new(Vec::new());
let mut w = fmt::IoWriter::new(buffer.by_ref());
let fmt = fmt::Config::from_lang::<Rust>().with_indentation(fmt::Indentation::Space(4));
let config = rust::Config::default();
// Prettier imports and use.
//.with_default_import(rust::ImportMode::Qualified);
output.format_file(&mut w.as_formatter(&fmt), &config)?;
let out = String::from_utf8(buffer.into_inner()?)?;
Ok(out)
}
pub fn group_by_handlers(&self, types: &Vec<&FullType>) -> Vec<(DynHandler, Vec<FullType>)> {

View File

@@ -0,0 +1,31 @@
use genco::{prelude::rust, quote};
use graphql_introspection_query::introspection_response::FullType;
use crate::predicates::is_enum_type;
use super::{utility::render_description, Handler};
pub struct Enumeration;
impl Handler for Enumeration {
fn predicate(&self, t: &FullType) -> bool {
is_enum_type(t)
}
fn render(&self, t: &FullType) -> eyre::Result<rust::Tokens> {
let name = t
.name
.as_ref()
.ok_or(eyre::anyhow!("could not get name from type"))?;
let description =
render_description(t).ok_or(eyre::anyhow!("could not find description"))?;
let out = quote! {
$description
pub enum $name {}
};
Ok(out)
}
}

View File

@@ -1,3 +1,4 @@
pub mod enumeration;
pub mod scalar;
mod utility;

View File

@@ -29,8 +29,10 @@ impl Handler for Scalar {
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"))?;
let scalar = rust::import("dagger_core", "Scalar");
Ok(quote! {
pub struct $name(Scalar);
pub struct $name($scalar);
})
}

View File

@@ -9,6 +9,13 @@ pub fn is_scalar_type(t: &FullType) -> bool {
false
}
pub fn is_enum_type(t: &FullType) -> bool {
if let Some(introspection_response::__TypeKind::ENUM) = t.kind {
return true;
}
false
}
pub fn is_custom_scalar_type(t: &FullType) -> bool {
if is_scalar_type(t) {
// TODO: Insert scalar