add format function

This commit is contained in:
Kasper Juul Hermansen 2023-02-15 21:15:11 +01:00
parent 4ebd1b4b17
commit 427e47c675
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912
5 changed files with 51 additions and 9 deletions

View File

@ -8,6 +8,18 @@ use dagger_core::introspection::Schema;
use self::generator::DynGenerator;
fn set_schema_parents(mut schema: Schema) -> Schema {
for t in schema.types.as_mut().into_iter().flatten().flatten() {
let t_parent = t.full_type.clone();
for mut field in t.full_type.fields.as_mut().into_iter().flatten() {
field.parent_type = Some(t_parent.clone());
}
}
return schema;
}
pub fn generate(schema: Schema, generator: DynGenerator) -> eyre::Result<String> {
let schema = set_schema_parents(schema);
generator.generate(schema)
}

View File

@ -1,5 +1,16 @@
use convert_case::{Case, Casing};
use dagger_core::introspection::FullTypeFields;
pub fn format_name(s: &str) -> String {
s.to_case(Case::Pascal)
}
pub fn field_options_struct_name(field: &FullTypeFields) -> Option<String> {
field
.parent_type
.as_ref()
.map(|p| p.name.as_ref())
.flatten()
.zip(field.name.as_ref())
.map(|(parent_name, field_name)| format!("{parent_name}{field_name}Opts"))
}

View File

@ -23,7 +23,7 @@ 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 = CommonFunctions::new(Arc::new(FormatTypeFunc {}));
let common_funcs = Arc::new(CommonFunctions::new(Arc::new(FormatTypeFunc {})));
println!("generating dagger for rust");
let visitor = Visitor {
@ -31,6 +31,8 @@ impl Generator for RustGenerator {
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)?;
@ -47,10 +49,11 @@ impl Generator for RustGenerator {
}),
visit_object: Arc::new({
let render = render.clone();
let common_funcs = common_funcs.clone();
move |t| {
println!("generating object");
let rendered_scalar = render_object(t)?;
let rendered_scalar = render_object(&common_funcs, t)?;
let mut render = render.lock().unwrap();
@ -63,6 +66,7 @@ impl Generator for RustGenerator {
}),
visit_input: Arc::new({
let render = render.clone();
let common_funcs = common_funcs.clone();
move |t| {
println!("generating input");
@ -79,6 +83,7 @@ impl Generator for RustGenerator {
}),
visit_enum: Arc::new({
let render = render.clone();
let common_funcs = common_funcs.clone();
move |t| {
println!("generating enum");

View File

@ -2,16 +2,17 @@ use dagger_core::introspection::{FullType, FullTypeFields};
use genco::prelude::rust;
use genco::quote;
use crate::rust::functions::format_name;
use crate::functions::CommonFunctions;
use crate::rust::functions::{field_options_struct_name, format_name};
use crate::utility::OptionExt;
pub fn render_object(t: &FullType) -> eyre::Result<rust::Tokens> {
pub fn render_object(funcs: &CommonFunctions, t: &FullType) -> eyre::Result<rust::Tokens> {
Ok(quote! {
pub struct $(t.name.pipe(|s| format_name(s))) {
}
$(t.fields.pipe(render_optional_args))
$(t.fields.pipe(|f| render_optional_args(funcs, f)))
impl $(t.name.pipe(|s| format_name(s))) {
@ -19,10 +20,13 @@ pub fn render_object(t: &FullType) -> eyre::Result<rust::Tokens> {
})
}
fn render_optional_args(fields: &Vec<FullTypeFields>) -> Option<rust::Tokens> {
fn render_optional_args(
funcs: &CommonFunctions,
fields: &Vec<FullTypeFields>,
) -> Option<rust::Tokens> {
let rendered_fields = fields
.iter()
.map(render_optional_arg)
.map(|f| render_optional_arg(funcs, f))
.flatten()
.collect::<Vec<_>>();
@ -35,6 +39,13 @@ fn render_optional_args(fields: &Vec<FullTypeFields>) -> Option<rust::Tokens> {
}
}
fn render_optional_arg(field: &FullTypeFields) -> Option<rust::Tokens> {
todo!()
fn render_optional_arg(funcs: &CommonFunctions, field: &FullTypeFields) -> Option<rust::Tokens> {
field
.type_
.pipe(|t| funcs.format_output_type(&t.type_ref))
.pipe(|f| {
quote! {
$f
}
})
}

View File

@ -161,6 +161,9 @@ pub struct FullTypeFields {
pub type_: Option<FullTypeFieldsType>,
pub is_deprecated: Option<bool>,
pub deprecation_reason: Option<String>,
#[serde(skip)]
pub parent_type: Option<FullType>,
}
#[derive(Clone, Debug, Deserialize)]