mirror of
https://github.com/kjuulh/dagger-rs.git
synced 2025-08-04 14:53:25 +02:00
feature/add impl (#6)
* format code * with object gen and args * add implementation * add rust generator * reset generated code * add basic output * reset output * add object * add format function * with opts * fix vec * add context to unwrap * fix arguments * with function body * first complete generation: Still missing Vec<Obj> * run full alpine * add roadmap item
This commit is contained in:
64
crates/dagger-codegen/src/rust/format.rs
Normal file
64
crates/dagger-codegen/src/rust/format.rs
Normal file
@@ -0,0 +1,64 @@
|
||||
use crate::functions::FormatTypeFuncs;
|
||||
|
||||
use super::functions::format_name;
|
||||
|
||||
pub struct FormatTypeFunc;
|
||||
|
||||
impl FormatTypeFuncs for FormatTypeFunc {
|
||||
fn format_kind_list(&self, representation: &str) -> String {
|
||||
format!("Vec<{}>", representation)
|
||||
}
|
||||
|
||||
fn format_kind_scalar_string(&self, representation: &str) -> String {
|
||||
let mut rep = representation.to_string();
|
||||
rep.push_str("String");
|
||||
rep
|
||||
}
|
||||
|
||||
fn format_kind_scalar_int(&self, representation: &str) -> String {
|
||||
let mut rep = representation.to_string();
|
||||
rep.push_str("isize");
|
||||
rep
|
||||
}
|
||||
|
||||
fn format_kind_scalar_float(&self, representation: &str) -> String {
|
||||
let mut rep = representation.to_string();
|
||||
rep.push_str("float");
|
||||
rep
|
||||
}
|
||||
|
||||
fn format_kind_scalar_boolean(&self, representation: &str) -> String {
|
||||
let mut rep = representation.to_string();
|
||||
rep.push_str("bool");
|
||||
rep
|
||||
}
|
||||
|
||||
fn format_kind_scalar_default(
|
||||
&self,
|
||||
representation: &str,
|
||||
ref_name: &str,
|
||||
input: bool,
|
||||
) -> String {
|
||||
let mut rep = representation.to_string();
|
||||
rep.push_str(&format_name(ref_name));
|
||||
rep
|
||||
}
|
||||
|
||||
fn format_kind_object(&self, representation: &str, ref_name: &str) -> String {
|
||||
let mut rep = representation.to_string();
|
||||
rep.push_str(&format_name(ref_name));
|
||||
rep
|
||||
}
|
||||
|
||||
fn format_kind_input_object(&self, representation: &str, ref_name: &str) -> String {
|
||||
let mut rep = representation.to_string();
|
||||
rep.push_str(&format_name(ref_name));
|
||||
rep
|
||||
}
|
||||
|
||||
fn format_kind_enum(&self, representation: &str, ref_name: &str) -> String {
|
||||
let mut rep = representation.to_string();
|
||||
rep.push_str(&format_name(ref_name));
|
||||
rep
|
||||
}
|
||||
}
|
206
crates/dagger-codegen/src/rust/functions.rs
Normal file
206
crates/dagger-codegen/src/rust/functions.rs
Normal file
@@ -0,0 +1,206 @@
|
||||
use convert_case::{Case, Casing};
|
||||
use dagger_core::introspection::FullTypeFields;
|
||||
use genco::prelude::rust;
|
||||
use genco::quote;
|
||||
use genco::tokens::quoted;
|
||||
|
||||
use crate::functions::{
|
||||
type_field_has_optional, type_ref_is_list, type_ref_is_list_of_objects, type_ref_is_object,
|
||||
type_ref_is_optional, type_ref_is_scalar, CommonFunctions,
|
||||
};
|
||||
use crate::utility::OptionExt;
|
||||
|
||||
pub fn format_name(s: &str) -> String {
|
||||
s.to_case(Case::Pascal)
|
||||
}
|
||||
|
||||
pub fn format_struct_name(s: &str) -> String {
|
||||
s.to_case(Case::Snake)
|
||||
}
|
||||
|
||||
pub fn field_options_struct_name(field: &FullTypeFields) -> Option<String> {
|
||||
field
|
||||
.parent_type
|
||||
.as_ref()
|
||||
.map(|p| p.name.as_ref().map(|n| format_name(n)))
|
||||
.flatten()
|
||||
.zip(field.name.as_ref().map(|n| format_name(n)))
|
||||
.map(|(parent_name, field_name)| format!("{parent_name}{field_name}Opts"))
|
||||
}
|
||||
|
||||
pub fn format_function(funcs: &CommonFunctions, field: &FullTypeFields) -> Option<rust::Tokens> {
|
||||
let signature = quote! {
|
||||
pub fn $(field.name.pipe(|n | format_struct_name(n)))
|
||||
};
|
||||
let args = format_function_args(funcs, field);
|
||||
|
||||
let output_type = field
|
||||
.type_
|
||||
.pipe(|t| &t.type_ref)
|
||||
.pipe(|t| funcs.format_output_type(t));
|
||||
|
||||
Some(quote! {
|
||||
$(signature)(
|
||||
$(args)
|
||||
) -> $(output_type) {
|
||||
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 = query.arg($(quoted(name)), $(n)).unwrap();
|
||||
})
|
||||
})
|
||||
})
|
||||
.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 = query.arg($(quoted(name)), $(&n)).unwrap();
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
.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,
|
||||
conn: self.conn.clone(),
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
if let Some(true) = field
|
||||
.type_
|
||||
.pipe(|t| type_ref_is_list_of_objects(&t.type_ref))
|
||||
{
|
||||
let output_type = funcs.format_output_type(
|
||||
&field
|
||||
.type_
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.type_ref
|
||||
.of_type
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.of_type
|
||||
.as_ref()
|
||||
.unwrap(),
|
||||
);
|
||||
return quote! {
|
||||
return vec![$(output_type) {
|
||||
proc: self.proc.clone(),
|
||||
selection: query,
|
||||
conn: self.conn.clone(),
|
||||
}]
|
||||
};
|
||||
}
|
||||
|
||||
let graphql_client = rust::import("crate::client", "graphql_client");
|
||||
|
||||
quote! {
|
||||
query.execute(&$graphql_client(&self.conn)).unwrap().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
fn format_function_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 t = funcs.format_input_type(&s.input_value.type_);
|
||||
let n = format_struct_name(&s.input_value.name);
|
||||
|
||||
Some(quote! {
|
||||
$(n): $(t),
|
||||
})
|
||||
})
|
||||
})
|
||||
.flatten()
|
||||
.collect::<Vec<_>>();
|
||||
let required_args = quote! {
|
||||
&self,
|
||||
$(for arg in args join ($['\r']) => $arg)
|
||||
};
|
||||
|
||||
if type_field_has_optional(field) {
|
||||
Some(quote! {
|
||||
$(required_args)
|
||||
opts: Option<$(field_options_struct_name(field))>
|
||||
})
|
||||
} else {
|
||||
Some(required_args)
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
114
crates/dagger-codegen/src/rust/mod.rs
Normal file
114
crates/dagger-codegen/src/rust/mod.rs
Normal file
@@ -0,0 +1,114 @@
|
||||
pub mod format;
|
||||
mod functions;
|
||||
pub mod templates;
|
||||
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use dagger_core::introspection::Schema;
|
||||
use eyre::Context;
|
||||
use genco::prelude::rust;
|
||||
|
||||
use crate::functions::CommonFunctions;
|
||||
use crate::generator::Generator;
|
||||
use crate::visitor::{VisitHandlers, Visitor};
|
||||
|
||||
use self::format::FormatTypeFunc;
|
||||
use self::templates::enum_tmpl::render_enum;
|
||||
use self::templates::input_tmpl::render_input;
|
||||
use self::templates::object_tmpl::render_object;
|
||||
use self::templates::scalar_tmpl::render_scalar;
|
||||
|
||||
pub struct RustGenerator {}
|
||||
|
||||
impl Generator for RustGenerator {
|
||||
fn generate(&self, schema: Schema) -> eyre::Result<String> {
|
||||
let render = Arc::new(Mutex::new(rust::Tokens::new()));
|
||||
let common_funcs = Arc::new(CommonFunctions::new(Arc::new(FormatTypeFunc {})));
|
||||
println!("generating dagger for rust");
|
||||
|
||||
let visitor = Visitor {
|
||||
schema,
|
||||
handlers: VisitHandlers {
|
||||
visit_scalar: Arc::new({
|
||||
let render = render.clone();
|
||||
let common_funcs = common_funcs.clone();
|
||||
|
||||
move |t| {
|
||||
println!("generating scalar");
|
||||
let rendered_scalar = render_scalar(t)?;
|
||||
|
||||
let mut render = render.lock().unwrap();
|
||||
|
||||
render.append(rendered_scalar);
|
||||
render.push();
|
||||
|
||||
println!("generated scalar");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}),
|
||||
visit_object: Arc::new({
|
||||
let render = render.clone();
|
||||
let common_funcs = common_funcs.clone();
|
||||
|
||||
move |t| {
|
||||
println!("generating object");
|
||||
let rendered_scalar = render_object(&common_funcs, t)?;
|
||||
|
||||
let mut render = render.lock().unwrap();
|
||||
|
||||
render.append(rendered_scalar);
|
||||
render.push();
|
||||
println!("generated object");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}),
|
||||
visit_input: Arc::new({
|
||||
let render = render.clone();
|
||||
let common_funcs = common_funcs.clone();
|
||||
|
||||
move |t| {
|
||||
println!("generating input");
|
||||
let rendered_scalar = render_input(&common_funcs, t)?;
|
||||
|
||||
let mut render = render.lock().unwrap();
|
||||
|
||||
render.append(rendered_scalar);
|
||||
render.push();
|
||||
println!("generated input");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}),
|
||||
visit_enum: Arc::new({
|
||||
let render = render.clone();
|
||||
let common_funcs = common_funcs.clone();
|
||||
|
||||
move |t| {
|
||||
println!("generating enum");
|
||||
let rendered_scalar = render_enum(t)?;
|
||||
|
||||
let mut render = render.lock().unwrap();
|
||||
|
||||
render.append(rendered_scalar);
|
||||
render.push();
|
||||
println!("generated enum");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}),
|
||||
},
|
||||
};
|
||||
|
||||
visitor.run()?;
|
||||
|
||||
println!("done generating objects");
|
||||
|
||||
let rendered = render.lock().unwrap();
|
||||
|
||||
rendered
|
||||
.to_file_string()
|
||||
.context("could not render to file string")
|
||||
}
|
||||
}
|
33
crates/dagger-codegen/src/rust/templates/enum_tmpl.rs
Normal file
33
crates/dagger-codegen/src/rust/templates/enum_tmpl.rs
Normal file
@@ -0,0 +1,33 @@
|
||||
use dagger_core::introspection::FullType;
|
||||
use genco::prelude::rust;
|
||||
use genco::quote;
|
||||
|
||||
fn render_enum_values(values: &FullType) -> Option<rust::Tokens> {
|
||||
let values = values
|
||||
.enum_values
|
||||
.as_ref()
|
||||
.into_iter()
|
||||
.map(|values| {
|
||||
values
|
||||
.into_iter()
|
||||
.map(|val| quote! { $(val.name.as_ref()) })
|
||||
})
|
||||
.flatten()
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let mut tokens = rust::Tokens::new();
|
||||
for val in values {
|
||||
tokens.append(val);
|
||||
tokens.push();
|
||||
}
|
||||
|
||||
Some(tokens)
|
||||
}
|
||||
|
||||
pub fn render_enum(t: &FullType) -> eyre::Result<rust::Tokens> {
|
||||
Ok(quote! {
|
||||
pub enum $(t.name.as_ref()) {
|
||||
$(render_enum_values(t))
|
||||
}
|
||||
})
|
||||
}
|
38
crates/dagger-codegen/src/rust/templates/input_tmpl.rs
Normal file
38
crates/dagger-codegen/src/rust/templates/input_tmpl.rs
Normal file
@@ -0,0 +1,38 @@
|
||||
use dagger_core::introspection::{FullType, FullTypeInputFields};
|
||||
use genco::prelude::rust;
|
||||
use genco::quote;
|
||||
|
||||
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()) ))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn render_input_fields(
|
||||
funcs: &CommonFunctions,
|
||||
fields: &[FullTypeInputFields],
|
||||
) -> Option<rust::Tokens> {
|
||||
let rendered_fields = fields.iter().map(|f| render_input_field(funcs, f));
|
||||
|
||||
if rendered_fields.len() == 0 {
|
||||
None
|
||||
} else {
|
||||
Some(quote! {
|
||||
$(for field in rendered_fields join ($['\r']) => $field)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render_input_field(funcs: &CommonFunctions, field: &FullTypeInputFields) -> rust::Tokens {
|
||||
quote! {
|
||||
pub $(format_struct_name(&field.input_value.name)): $(funcs.format_input_type(&field.input_value.type_)),
|
||||
}
|
||||
}
|
4
crates/dagger-codegen/src/rust/templates/mod.rs
Normal file
4
crates/dagger-codegen/src/rust/templates/mod.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
pub mod enum_tmpl;
|
||||
pub mod input_tmpl;
|
||||
pub mod object_tmpl;
|
||||
pub mod scalar_tmpl;
|
112
crates/dagger-codegen/src/rust/templates/object_tmpl.rs
Normal file
112
crates/dagger-codegen/src/rust/templates/object_tmpl.rs
Normal file
@@ -0,0 +1,112 @@
|
||||
use dagger_core::introspection::{FullType, FullTypeFields, FullTypeFieldsArgs};
|
||||
use genco::prelude::rust;
|
||||
use genco::quote;
|
||||
|
||||
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 conn = rust::import("dagger_core::connect_params", "ConnectParams");
|
||||
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,
|
||||
pub conn: $conn,
|
||||
}
|
||||
|
||||
$(t.fields.pipe(|f| render_optional_args(funcs, f)))
|
||||
|
||||
impl $(t.name.pipe(|s| format_name(s))) {
|
||||
$(t.fields.pipe(|f| render_functions(funcs, f)))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn render_optional_args(
|
||||
funcs: &CommonFunctions,
|
||||
fields: &Vec<FullTypeFields>,
|
||||
) -> Option<rust::Tokens> {
|
||||
let rendered_fields = fields
|
||||
.iter()
|
||||
.map(|f| render_optional_arg(funcs, f))
|
||||
.flatten()
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
if rendered_fields.len() == 0 {
|
||||
None
|
||||
} else {
|
||||
Some(quote! {
|
||||
$(for field in rendered_fields join ($['\r']) => $field)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn render_optional_arg(funcs: &CommonFunctions, field: &FullTypeFields) -> Option<rust::Tokens> {
|
||||
let output_type = field_options_struct_name(field);
|
||||
let fields = field
|
||||
.args
|
||||
.pipe(|t| t.into_iter().flatten().collect::<Vec<_>>())
|
||||
.map(|t| {
|
||||
t.into_iter()
|
||||
.filter(|t| type_ref_is_optional(&t.input_value.type_))
|
||||
.collect::<Vec<_>>()
|
||||
})
|
||||
.pipe(|t| render_optional_field_args(funcs, t))
|
||||
.flatten();
|
||||
|
||||
if let Some(fields) = fields {
|
||||
Some(quote! {
|
||||
pub struct $output_type {
|
||||
$fields
|
||||
}
|
||||
})
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn render_optional_field_args(
|
||||
funcs: &CommonFunctions,
|
||||
args: &Vec<&FullTypeFieldsArgs>,
|
||||
) -> Option<rust::Tokens> {
|
||||
if args.len() == 0 {
|
||||
return None;
|
||||
}
|
||||
let rendered_args = args.into_iter().map(|a| &a.input_value).map(|a| {
|
||||
quote! {
|
||||
pub $(format_struct_name(&a.name)): Option<$(funcs.format_output_type(&a.type_))>,
|
||||
}
|
||||
});
|
||||
|
||||
Some(quote! {
|
||||
$(for arg in rendered_args join ($['\r']) => $arg)
|
||||
})
|
||||
}
|
||||
|
||||
fn render_functions(funcs: &CommonFunctions, fields: &Vec<FullTypeFields>) -> Option<rust::Tokens> {
|
||||
let rendered_functions = fields
|
||||
.iter()
|
||||
.map(|f| render_function(funcs, f))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
if rendered_functions.len() > 0 {
|
||||
Some(quote! {
|
||||
$(for func in rendered_functions join ($['\r']) => $func)
|
||||
})
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn render_function(funcs: &CommonFunctions, field: &FullTypeFields) -> Option<rust::Tokens> {
|
||||
Some(quote! {
|
||||
$(format_function(funcs, field))
|
||||
})
|
||||
}
|
16
crates/dagger-codegen/src/rust/templates/scalar_tmpl.rs
Normal file
16
crates/dagger-codegen/src/rust/templates/scalar_tmpl.rs
Normal file
@@ -0,0 +1,16 @@
|
||||
use dagger_core::introspection::FullType;
|
||||
use genco::prelude::rust;
|
||||
use genco::quote;
|
||||
|
||||
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