with object gen and args

This commit is contained in:
Kasper Juul Hermansen 2023-02-11 16:17:04 +01:00
parent d0483b0434
commit 3453e8cac6
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912
2 changed files with 81 additions and 23 deletions

View File

@ -7,8 +7,11 @@ use super::{
utility::{render_description_from_field, render_description_from_input_value},
};
pub fn render_fields(fields: &Vec<FullTypeFields>) -> eyre::Result<Option<rust::Tokens>> {
pub fn render_fields(
fields: &Vec<FullTypeFields>,
) -> eyre::Result<Option<(rust::Tokens, rust::Tokens)>> {
let mut collected_fields: Vec<rust::Tokens> = vec![];
let mut collected_args: Vec<rust::Tokens> = vec![];
for field in fields.iter() {
let name = field.name.as_ref().map(|n| n.to_case(Case::Snake)).unwrap();
let output = render_field_output(field)?;
@ -18,22 +21,23 @@ pub fn render_fields(fields: &Vec<FullTypeFields>) -> eyre::Result<Option<rust::
None => None,
};
let mut tkns = rust::Tokens::new();
if let Some(args) = args.as_ref() {
tkns.append(quote! {
let mut args_tkns = rust::Tokens::new();
args_tkns.append(quote! {
$description
pub struct $(&name)Args {
pub struct $(&field.name.as_ref().map(|n| n.to_case(Case::Pascal)).unwrap())Args {
$(&args.args)
}
});
tkns.push();
}
args_tkns.push();
collected_args.push(args_tkns);
}
let mut tkns = rust::Tokens::new();
tkns.append(quote! {
pub fn $(&name)(
&self,
$(if let Some(_) = args.as_ref() => args: $(&name)Args)
$(if let Some(_) = args.as_ref() => args: &$(&field.name.as_ref().map(|n| n.to_case(Case::Pascal)).unwrap())Args)
) -> $(&output) {
let query = self.selection.select($(field.name.as_ref().map(|n| format!("\"{}\"", n))));
$(if let Some(_) = args.as_ref() => query.args(args);)
@ -51,9 +55,14 @@ pub fn render_fields(fields: &Vec<FullTypeFields>) -> eyre::Result<Option<rust::
collected_fields.push(tkns);
}
Ok(Some(quote! {
$(for field in collected_fields => $field $['\n'] )
}))
Ok(Some((
quote! {
$(for arg in collected_args => $arg $['\n'] )
},
quote! {
$(for field in collected_fields => $field $['\n'] )
},
)))
}
struct Arg {
@ -91,15 +100,9 @@ fn render_args(args: &[Option<FullTypeFieldsArgs>]) -> Option<CollectedArgs> {
};
for arg in collected_args {
if let Some(desc) = arg.description {
if let Some(inner_desc) = collected_arg.description.as_mut() {
inner_desc.append(desc);
inner_desc.push();
}
}
collected_arg.args.append(quote! {
$(arg.name.to_case(Case::Snake)): $(arg.type_),
$(arg.description)
pub $(arg.name.to_case(Case::Snake)): $(arg.type_),
});
collected_arg.args.push();
}

View File

@ -29,14 +29,24 @@ impl Handler for Object {
None => None,
};
let child = rust::import("std::process", "Child");
let connect_params = rust::import("dagger_core::connect_params", "ConnectParams");
let selection = rust::import("crate::querybuilder", "Selection");
let arc = rust::import("std::sync", "Arc");
let out = quote! {
$(if fields.as_ref().is_some() => $(fields.as_ref().map(|f| &f.0)))
$(if description.is_some() => $description)
pub struct $name {
$(if input_fields.is_some() => $input_fields)
pub conn: $connect_params,
pub proc: $arc<$child>,
pub selection: $selection,
}
impl $name {
$(if fields.is_some() => $fields)
$(if fields.is_some() => $(fields.map(|f| f.1)))
}
};
@ -85,9 +95,18 @@ mod tests {
enum_values: None,
possible_types: None,
};
let expected = r#"
let expected = r#"use crate::querybuilder::Selection;
use dagger_core::connect_params::ConnectParams;
use std::process::Child;
use std::sync::Arc;
/// A directory whose contents persists across sessions
pub struct CacheVolume {}
pub struct CacheVolume {
pub conn: ConnectParams,
pub proc: Arc<Child>,
pub selection: Selection,
}
impl CacheVolume {
pub fn id(
@ -168,7 +187,43 @@ impl CacheVolume {
enum_values: None,
possible_types: None,
};
let expected = r#"
let expected = r#"use crate::querybuilder::Selection;
use dagger_core::connect_params::ConnectParams;
use std::process::Child;
use std::sync::Arc;
/// Loads a container from ID.
/// Null ID returns an empty container (scratch).
/// Optional platform argument initializes new containers to execute and publish as that platform. Platform defaults to that of the builder's host.
pub struct ContainerArgs {
pub id: Option<ContainerID>,
pub platform: Option<Platform>,
}
pub struct Query {
pub conn: ConnectParams,
pub proc: Arc<Child>,
pub selection: Selection,
}
impl Query {
pub fn container(
&self,
args: &ContainerArgs
) -> CacheID {
let query = self.selection.select("container");
query.args(args);
CacheID {
conn: self.conn.clone(),
proc: self.proc.clone(),
selection: query,
}
todo!()
}
}
"#;
let handler = Object {};
let obj = handler.render(&t).unwrap();