mirror of
https://github.com/kjuulh/dagger-rs.git
synced 2025-12-29 11:11:02 +01:00
with function body
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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<rust::Tokens> {
|
||||
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::<Vec<_>>();
|
||||
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<rust::Tokens> {
|
||||
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::<Vec<_>>();
|
||||
|
||||
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<rust::Tokens> {
|
||||
if let Some(args) = field.args.as_ref() {
|
||||
let args = args
|
||||
|
||||
@@ -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<rust::Tokens> {
|
||||
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()) ))
|
||||
}
|
||||
|
||||
@@ -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<rust::Tokens> {
|
||||
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)))
|
||||
|
||||
@@ -6,7 +6,11 @@ use crate::rust::functions::format_name;
|
||||
use crate::utility::OptionExt;
|
||||
|
||||
pub fn render_scalar(t: &FullType) -> eyre::Result<rust::Tokens> {
|
||||
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);
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user