diff --git a/crates/dagger-codegen/src/functions.rs b/crates/dagger-codegen/src/functions.rs index f395706..c68c97a 100644 --- a/crates/dagger-codegen/src/functions.rs +++ b/crates/dagger-codegen/src/functions.rs @@ -168,6 +168,15 @@ pub fn type_field_has_optional(field: &FullTypeFields) -> bool { } pub fn type_ref_is_scalar(type_ref: &TypeRef) -> bool { + let mut type_ref = type_ref.clone(); + if type_ref + .kind + .pipe(|k| *k == __TypeKind::NON_NULL) + .unwrap_or(false) + { + type_ref = *type_ref.of_type.unwrap().clone(); + } + type_ref .kind .pipe(|k| *k == __TypeKind::SCALAR) @@ -175,6 +184,15 @@ pub fn type_ref_is_scalar(type_ref: &TypeRef) -> bool { } pub fn type_ref_is_object(type_ref: &TypeRef) -> bool { + let mut type_ref = type_ref.clone(); + if type_ref + .kind + .pipe(|k| *k == __TypeKind::NON_NULL) + .unwrap_or(false) + { + type_ref = *type_ref.of_type.unwrap().clone(); + } + type_ref .kind .pipe(|k| *k == __TypeKind::OBJECT) @@ -182,6 +200,15 @@ pub fn type_ref_is_object(type_ref: &TypeRef) -> bool { } pub fn type_ref_is_list(type_ref: &TypeRef) -> bool { + let mut type_ref = type_ref.clone(); + if type_ref + .kind + .pipe(|k| *k == __TypeKind::NON_NULL) + .unwrap_or(false) + { + type_ref = *type_ref.of_type.unwrap().clone(); + } + type_ref .kind .pipe(|k| *k == __TypeKind::LIST) diff --git a/crates/dagger-codegen/src/rust/functions.rs b/crates/dagger-codegen/src/rust/functions.rs index 1069bc8..a73ff58 100644 --- a/crates/dagger-codegen/src/rust/functions.rs +++ b/crates/dagger-codegen/src/rust/functions.rs @@ -2,9 +2,11 @@ use convert_case::{Case, Casing}; use dagger_core::introspection::FullTypeFields; use genco::prelude::rust; use genco::quote; +use genco::tokens::quoted; use crate::functions::{ - input_values_has_optionals, type_field_has_optional, type_ref_is_optional, CommonFunctions, + type_field_has_optional, type_ref_is_list, type_ref_is_object, type_ref_is_optional, + type_ref_is_scalar, CommonFunctions, }; use crate::utility::OptionExt; @@ -41,11 +43,101 @@ pub fn format_function(funcs: &CommonFunctions, field: &FullTypeFields) -> Optio $(signature)( $(args) ) -> $(output_type) { - todo!() + let mut query = self.selection.select($(quoted(field.name.as_ref()))); + + $(render_required_args(funcs, field)) + $(render_optional_args(funcs, field)) + + $(render_execution(funcs, field)) } }) } +fn render_required_args(funcs: &CommonFunctions, field: &FullTypeFields) -> Option { + if let Some(args) = field.args.as_ref() { + let args = args + .into_iter() + .map(|a| { + a.as_ref().and_then(|s| { + if type_ref_is_optional(&s.input_value.type_) { + return None; + } + + let n = format_struct_name(&s.input_value.name); + let name = &s.input_value.name; + + Some(quote! { + query.arg($(quoted(name)), $(n)); + }) + }) + }) + .flatten() + .collect::>(); + let required_args = quote! { + $(for arg in args join ($['\r']) => $arg) + }; + + Some(required_args) + } else { + None + } +} + +fn render_optional_args(funcs: &CommonFunctions, field: &FullTypeFields) -> Option { + if let Some(args) = field.args.as_ref() { + let args = args + .into_iter() + .map(|a| { + a.as_ref().and_then(|s| { + if !type_ref_is_optional(&s.input_value.type_) { + return None; + } + + let n = format_struct_name(&s.input_value.name); + let name = &s.input_value.name; + + Some(quote! { + if let Some($(&n)) = opts.$(&n) { + query.arg($(quoted(name)), $(&n)); + } + }) + }) + }) + .flatten() + .collect::>(); + + if args.len() == 0 { + return None; + } + + let required_args = quote! { + if let Some(opts) = opts { + $(for arg in args join ($['\r']) => $arg) + } + }; + + Some(required_args) + } else { + None + } +} + +fn render_execution(funcs: &CommonFunctions, field: &FullTypeFields) -> rust::Tokens { + if let Some(true) = field.type_.pipe(|t| type_ref_is_object(&t.type_ref)) { + let output_type = funcs.format_output_type(&field.type_.as_ref().unwrap().type_ref); + return quote! { + return $(output_type) { + proc: self.proc.clone(), + selection: query + } + }; + } + + quote! { + selection.execute() + } +} + fn format_function_args(funcs: &CommonFunctions, field: &FullTypeFields) -> Option { if let Some(args) = field.args.as_ref() { let args = args diff --git a/crates/dagger-codegen/src/rust/templates/input_tmpl.rs b/crates/dagger-codegen/src/rust/templates/input_tmpl.rs index f502e2f..6632981 100644 --- a/crates/dagger-codegen/src/rust/templates/input_tmpl.rs +++ b/crates/dagger-codegen/src/rust/templates/input_tmpl.rs @@ -6,7 +6,10 @@ use crate::functions::CommonFunctions; use crate::rust::functions::{format_name, format_struct_name}; pub fn render_input(funcs: &CommonFunctions, t: &FullType) -> eyre::Result { + let deserialize = rust::import("serde", "Deserialize"); + let serialize = rust::import("serde", "Serialize"); Ok(quote! { + #[derive($serialize, $deserialize)] pub struct $(format_name(t.name.as_ref().unwrap())) { $(render_input_fields(funcs, t.input_fields.as_ref().unwrap_or(&Vec::new()) )) } diff --git a/crates/dagger-codegen/src/rust/templates/object_tmpl.rs b/crates/dagger-codegen/src/rust/templates/object_tmpl.rs index 9b09f80..a393cdc 100644 --- a/crates/dagger-codegen/src/rust/templates/object_tmpl.rs +++ b/crates/dagger-codegen/src/rust/templates/object_tmpl.rs @@ -2,18 +2,21 @@ use dagger_core::introspection::{FullType, FullTypeFields, FullTypeFieldsArgs}; use genco::prelude::rust; use genco::quote; -use crate::functions::{ - input_values_has_optionals, type_field_has_optional, type_ref_is_optional, CommonFunctions, -}; +use crate::functions::{type_ref_is_optional, CommonFunctions}; use crate::rust::functions::{ field_options_struct_name, format_function, format_name, format_struct_name, }; use crate::utility::OptionExt; pub fn render_object(funcs: &CommonFunctions, t: &FullType) -> eyre::Result { + let selection = rust::import("crate::querybuilder", "Selection"); + let child = rust::import("std::process", "Child"); + let arc = rust::import("std::sync", "Arc"); + Ok(quote! { pub struct $(t.name.pipe(|s| format_name(s))) { - + pub proc: $arc<$child>, + pub selection: $selection, } $(t.fields.pipe(|f| render_optional_args(funcs, f))) diff --git a/crates/dagger-codegen/src/rust/templates/scalar_tmpl.rs b/crates/dagger-codegen/src/rust/templates/scalar_tmpl.rs index 7e2f84b..a83c664 100644 --- a/crates/dagger-codegen/src/rust/templates/scalar_tmpl.rs +++ b/crates/dagger-codegen/src/rust/templates/scalar_tmpl.rs @@ -6,7 +6,11 @@ use crate::rust::functions::format_name; use crate::utility::OptionExt; pub fn render_scalar(t: &FullType) -> eyre::Result { + let deserialize = rust::import("serde", "Deserialize"); + let serialize = rust::import("serde", "Serialize"); + Ok(quote! { + #[derive($serialize, $deserialize)] pub struct $(t.name.pipe(|n|format_name(n)))(String); }) } diff --git a/crates/dagger-sdk/src/gen.rs b/crates/dagger-sdk/src/gen.rs index fe0f8b7..c78af70 100644 --- a/crates/dagger-sdk/src/gen.rs +++ b/crates/dagger-sdk/src/gen.rs @@ -1,22 +1,43 @@ +use crate::querybuilder::Selection; +use serde::{Deserialize, Serialize}; +use std::process::Child; +use std::sync::Arc; + +#[derive(Serialize, Deserialize)] pub struct CacheId(String); +#[derive(Serialize, Deserialize)] pub struct ContainerId(String); +#[derive(Serialize, Deserialize)] pub struct DirectoryId(String); +#[derive(Serialize, Deserialize)] pub struct FileId(String); +#[derive(Serialize, Deserialize)] pub struct Platform(String); +#[derive(Serialize, Deserialize)] pub struct SecretId(String); +#[derive(Serialize, Deserialize)] pub struct SocketId(String); +#[derive(Serialize, Deserialize)] pub struct BuildArg { pub name: String, pub value: String, } -pub struct CacheVolume {} +pub struct CacheVolume { + pub proc: Arc, + pub selection: Selection, +} impl CacheVolume { pub fn id(&self) -> CacheId { - todo!() + let mut query = self.selection.select("id"); + + selection.execute() } } -pub struct Container {} +pub struct Container { + pub proc: Arc, + pub selection: Selection, +} pub struct ContainerBuildOpts { pub dockerfile: Option, @@ -65,76 +86,222 @@ pub struct ContainerWithNewFileOpts { impl Container { pub fn build(&self, context: DirectoryId, opts: Option) -> Container { - todo!() + let mut query = self.selection.select("build"); + + query.arg("context", context); + if let Some(opts) = opts { + if let Some(dockerfile) = opts.dockerfile { + query.arg("dockerfile", dockerfile); + } + if let Some(build_args) = opts.build_args { + query.arg("buildArgs", build_args); + } + if let Some(target) = opts.target { + query.arg("target", target); + } + } + + return Container { + proc: self.proc.clone(), + selection: query, + }; } pub fn default_args(&self) -> Vec { - todo!() + let mut query = self.selection.select("defaultArgs"); + + selection.execute() } pub fn directory(&self, path: String) -> Directory { - todo!() + let mut query = self.selection.select("directory"); + + query.arg("path", path); + + return Directory { + proc: self.proc.clone(), + selection: query, + }; } pub fn entrypoint(&self) -> Vec { - todo!() + let mut query = self.selection.select("entrypoint"); + + selection.execute() } pub fn env_variable(&self, name: String) -> String { - todo!() + let mut query = self.selection.select("envVariable"); + + query.arg("name", name); + + selection.execute() } pub fn env_variables(&self) -> Vec { - todo!() + let mut query = self.selection.select("envVariables"); + + selection.execute() } pub fn exec(&self, opts: Option) -> Container { - todo!() + let mut query = self.selection.select("exec"); + + if let Some(opts) = opts { + if let Some(args) = opts.args { + query.arg("args", args); + } + if let Some(stdin) = opts.stdin { + query.arg("stdin", stdin); + } + if let Some(redirect_stdout) = opts.redirect_stdout { + query.arg("redirectStdout", redirect_stdout); + } + if let Some(redirect_stderr) = opts.redirect_stderr { + query.arg("redirectStderr", redirect_stderr); + } + if let Some(experimental_privileged_nesting) = opts.experimental_privileged_nesting { + query.arg( + "experimentalPrivilegedNesting", + experimental_privileged_nesting, + ); + } + } + + return Container { + proc: self.proc.clone(), + selection: query, + }; } pub fn exit_code(&self) -> isize { - todo!() + let mut query = self.selection.select("exitCode"); + + selection.execute() } pub fn export(&self, path: String, opts: Option) -> bool { - todo!() + let mut query = self.selection.select("export"); + + query.arg("path", path); + if let Some(opts) = opts { + if let Some(platform_variants) = opts.platform_variants { + query.arg("platformVariants", platform_variants); + } + } + + selection.execute() } pub fn file(&self, path: String) -> File { - todo!() + let mut query = self.selection.select("file"); + + query.arg("path", path); + + return File { + proc: self.proc.clone(), + selection: query, + }; } pub fn from(&self, address: String) -> Container { - todo!() + let mut query = self.selection.select("from"); + + query.arg("address", address); + + return Container { + proc: self.proc.clone(), + selection: query, + }; } pub fn fs(&self) -> Directory { - todo!() + let mut query = self.selection.select("fs"); + + return Directory { + proc: self.proc.clone(), + selection: query, + }; } pub fn id(&self) -> ContainerId { - todo!() + let mut query = self.selection.select("id"); + + selection.execute() } pub fn label(&self, name: String) -> String { - todo!() + let mut query = self.selection.select("label"); + + query.arg("name", name); + + selection.execute() } pub fn labels(&self) -> Vec