with enum

This commit is contained in:
Kasper Juul Hermansen 2023-01-29 20:14:52 +01:00
parent 0bf6b0e91e
commit 2a1f7c3f26
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912
14 changed files with 97 additions and 19 deletions

6
Cargo.lock generated
View File

@ -177,6 +177,7 @@ dependencies = [
name = "dagger-codegen" name = "dagger-codegen"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"dagger-core",
"eyre", "eyre",
"genco", "genco",
"graphql-introspection-query", "graphql-introspection-query",
@ -186,6 +187,10 @@ dependencies = [
"serde_json", "serde_json",
] ]
[[package]]
name = "dagger-core"
version = "0.1.0"
[[package]] [[package]]
name = "dagger-rs" name = "dagger-rs"
version = "0.1.2" version = "0.1.2"
@ -213,6 +218,7 @@ dependencies = [
name = "dagger-sdk" name = "dagger-sdk"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"dagger-core",
"genco", "genco",
] ]

View File

@ -9,7 +9,7 @@ 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 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[workspace] [workspace]
members = ["crates/dagger-codegen", "crates/dagger-sdk"] members = ["crates/dagger-codegen", "crates/dagger-sdk", "crates/dagger-core"]
[dependencies] [dependencies]
dagger-codegen = { path = "crates/dagger-codegen" } dagger-codegen = { path = "crates/dagger-codegen" }

View File

@ -6,6 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
dagger-core = { path = "../dagger-core" }
eyre = "0.6.8" eyre = "0.6.8"
genco = "0.17.3" genco = "0.17.3"
graphql-introspection-query = "0.2.0" 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::{ use graphql_introspection_query::introspection_response::{
FullType, IntrospectionResponse, Schema, FullType, IntrospectionResponse, Schema,
}; };
use crate::{ use crate::{
handlers::{scalar::Scalar, DynHandler, Handlers}, handlers::{enumeration::Enumeration, scalar::Scalar, DynHandler, Handlers},
predicates::is_custom_scalar_type, predicates::is_custom_scalar_type,
}; };
@ -18,7 +21,7 @@ pub struct CodeGeneration {
impl CodeGeneration { impl CodeGeneration {
pub fn new() -> Self { pub fn new() -> Self {
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> { fn generate_from_schema(&self, schema: &Schema) -> eyre::Result<String> {
let mut output = rust::Tokens::new(); let mut output = rust::Tokens::new();
output.push();
output.append(quote! { output.append(quote! {
$(format!("// code generated by dagger. DO NOT EDIT.")) $(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>)> { 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; pub mod scalar;
mod utility; mod utility;

View File

@ -29,8 +29,10 @@ impl Handler for Scalar {
fn render_struct(&self, t: &FullType) -> eyre::Result<genco::prelude::rust::Tokens> { 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 name = t.name.as_ref().ok_or(eyre::anyhow!("name not found"))?;
let scalar = rust::import("dagger_core", "Scalar");
Ok(quote! { 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 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 { pub fn is_custom_scalar_type(t: &FullType) -> bool {
if is_scalar_type(t) { if is_scalar_type(t) {
// TODO: Insert scalar // TODO: Insert scalar

View File

@ -0,0 +1,8 @@
[package]
name = "dagger-core"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View File

@ -0,0 +1 @@
pub struct Scalar(String);

View File

@ -9,4 +9,6 @@ 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 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
dagger-core = { path = "../dagger-core" }
genco = "0.17.3" genco = "0.17.3"

View File

@ -1,23 +1,25 @@
use dagger_core::Scalar;
// code generated by dagger. DO NOT EDIT. // code generated by dagger. DO NOT EDIT.
/// A global cache volume identifier.
pub struct CacheID(Scalar);
/// A content-addressed directory identifier.
pub struct DirectoryID(Scalar);
/// A content-addressed socket identifier. /// A content-addressed socket identifier.
pub struct SocketID(Scalar); pub struct SocketID(Scalar);
/// A unique container identifier. Null designates an empty container (scratch).
pub struct ContainerID(Scalar);
/// A file identifier.
pub struct FileID(Scalar);
/// The platform config OS and architecture in a Container. /// The platform config OS and architecture in a Container.
/// The format is [os]/[platform]/[version] (e.g. darwin/arm64/v7, windows/amd64, linux/arm64). /// The format is [os]/[platform]/[version] (e.g. darwin/arm64/v7, windows/amd64, linux/arm64).
pub struct Platform(Scalar); pub struct Platform(Scalar);
/// A unique container identifier. Null designates an empty container (scratch).
pub struct ContainerID(Scalar);
/// A content-addressed directory identifier.
pub struct DirectoryID(Scalar);
/// A unique identifier for a secret. /// A unique identifier for a secret.
pub struct SecretID(Scalar); pub struct SecretID(Scalar);
/// A file identifier.
pub struct FileID(Scalar);
/// A global cache volume identifier.
pub struct CacheID(Scalar);

1
rust-toolchain Normal file
View File

@ -0,0 +1 @@
nightly