From 2a1f7c3f2666f1f4caebf7c22707709741c2cfad Mon Sep 17 00:00:00 2001 From: kjuulh Date: Sun, 29 Jan 2023 20:14:52 +0100 Subject: [PATCH] with enum --- Cargo.lock | 6 ++++ Cargo.toml | 2 +- crates/dagger-codegen/Cargo.toml | 2 ++ crates/dagger-codegen/rust-toolchain | 1 + crates/dagger-codegen/src/codegen.rs | 24 +++++++++++--- .../src/handlers/enumeration.rs | 31 +++++++++++++++++++ crates/dagger-codegen/src/handlers/mod.rs | 1 + crates/dagger-codegen/src/handlers/scalar.rs | 4 ++- crates/dagger-codegen/src/predicates.rs | 7 +++++ crates/dagger-core/Cargo.toml | 8 +++++ crates/dagger-core/src/lib.rs | 1 + crates/dagger-sdk/Cargo.toml | 2 ++ crates/dagger-sdk/src/gen.rs | 26 +++++++++------- rust-toolchain | 1 + 14 files changed, 97 insertions(+), 19 deletions(-) create mode 100644 crates/dagger-codegen/rust-toolchain create mode 100644 crates/dagger-codegen/src/handlers/enumeration.rs create mode 100644 crates/dagger-core/Cargo.toml create mode 100644 crates/dagger-core/src/lib.rs create mode 100644 rust-toolchain diff --git a/Cargo.lock b/Cargo.lock index fe109c6..28a656e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -177,6 +177,7 @@ dependencies = [ name = "dagger-codegen" version = "0.1.0" dependencies = [ + "dagger-core", "eyre", "genco", "graphql-introspection-query", @@ -186,6 +187,10 @@ dependencies = [ "serde_json", ] +[[package]] +name = "dagger-core" +version = "0.1.0" + [[package]] name = "dagger-rs" version = "0.1.2" @@ -213,6 +218,7 @@ dependencies = [ name = "dagger-sdk" version = "0.1.0" dependencies = [ + "dagger-core", "genco", ] diff --git a/Cargo.toml b/Cargo.toml index 5117cf4..418bf96 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 [workspace] -members = ["crates/dagger-codegen", "crates/dagger-sdk"] +members = ["crates/dagger-codegen", "crates/dagger-sdk", "crates/dagger-core"] [dependencies] dagger-codegen = { path = "crates/dagger-codegen" } diff --git a/crates/dagger-codegen/Cargo.toml b/crates/dagger-codegen/Cargo.toml index 2b2544a..6f257b3 100644 --- a/crates/dagger-codegen/Cargo.toml +++ b/crates/dagger-codegen/Cargo.toml @@ -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" diff --git a/crates/dagger-codegen/rust-toolchain b/crates/dagger-codegen/rust-toolchain new file mode 100644 index 0000000..bf867e0 --- /dev/null +++ b/crates/dagger-codegen/rust-toolchain @@ -0,0 +1 @@ +nightly diff --git a/crates/dagger-codegen/src/codegen.rs b/crates/dagger-codegen/src/codegen.rs index 9793628..e815f04 100644 --- a/crates/dagger-codegen/src/codegen.rs +++ b/crates/dagger-codegen/src/codegen.rs @@ -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 { 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::().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)> { diff --git a/crates/dagger-codegen/src/handlers/enumeration.rs b/crates/dagger-codegen/src/handlers/enumeration.rs new file mode 100644 index 0000000..61c334c --- /dev/null +++ b/crates/dagger-codegen/src/handlers/enumeration.rs @@ -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 { + 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) + } +} diff --git a/crates/dagger-codegen/src/handlers/mod.rs b/crates/dagger-codegen/src/handlers/mod.rs index 844f7a7..1b5977d 100644 --- a/crates/dagger-codegen/src/handlers/mod.rs +++ b/crates/dagger-codegen/src/handlers/mod.rs @@ -1,3 +1,4 @@ +pub mod enumeration; pub mod scalar; mod utility; diff --git a/crates/dagger-codegen/src/handlers/scalar.rs b/crates/dagger-codegen/src/handlers/scalar.rs index bfd2e7d..4bebcb2 100644 --- a/crates/dagger-codegen/src/handlers/scalar.rs +++ b/crates/dagger-codegen/src/handlers/scalar.rs @@ -29,8 +29,10 @@ impl Handler for Scalar { fn render_struct(&self, t: &FullType) -> eyre::Result { 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); }) } diff --git a/crates/dagger-codegen/src/predicates.rs b/crates/dagger-codegen/src/predicates.rs index c7887c4..d4c8800 100644 --- a/crates/dagger-codegen/src/predicates.rs +++ b/crates/dagger-codegen/src/predicates.rs @@ -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 diff --git a/crates/dagger-core/Cargo.toml b/crates/dagger-core/Cargo.toml new file mode 100644 index 0000000..16edae4 --- /dev/null +++ b/crates/dagger-core/Cargo.toml @@ -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] diff --git a/crates/dagger-core/src/lib.rs b/crates/dagger-core/src/lib.rs new file mode 100644 index 0000000..d1e5bd8 --- /dev/null +++ b/crates/dagger-core/src/lib.rs @@ -0,0 +1 @@ +pub struct Scalar(String); diff --git a/crates/dagger-sdk/Cargo.toml b/crates/dagger-sdk/Cargo.toml index 93a7bd4..e089ce4 100644 --- a/crates/dagger-sdk/Cargo.toml +++ b/crates/dagger-sdk/Cargo.toml @@ -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 [dependencies] +dagger-core = { path = "../dagger-core" } + genco = "0.17.3" diff --git a/crates/dagger-sdk/src/gen.rs b/crates/dagger-sdk/src/gen.rs index 8dc6272..25a4ccb 100644 --- a/crates/dagger-sdk/src/gen.rs +++ b/crates/dagger-sdk/src/gen.rs @@ -1,23 +1,25 @@ +use dagger_core::Scalar; + // 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. 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 format is [os]/[platform]/[version] (e.g. darwin/arm64/v7, windows/amd64, linux/arm64). 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. pub struct SecretID(Scalar); - -/// A file identifier. -pub struct FileID(Scalar); - -/// A global cache volume identifier. -pub struct CacheID(Scalar); \ No newline at end of file diff --git a/rust-toolchain b/rust-toolchain new file mode 100644 index 0000000..bf867e0 --- /dev/null +++ b/rust-toolchain @@ -0,0 +1 @@ +nightly