mirror of
https://github.com/kjuulh/dagger-rs.git
synced 2025-07-26 03:19:21 +02:00
added scalars
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
pub mod scalar;
|
||||
mod utility;
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
@@ -11,16 +12,15 @@ pub trait Handler {
|
||||
false
|
||||
}
|
||||
|
||||
fn render(&self, t: &FullType) -> eyre::Result<String> {
|
||||
let mut s = String::new();
|
||||
|
||||
s.push_str("\n");
|
||||
s.push_str(self.render_struct(t)?.to_string()?.as_str());
|
||||
s.push_str("\n");
|
||||
s.push_str(self.render_impl(t)?.to_string()?.as_str());
|
||||
s.push_str("\n");
|
||||
|
||||
Ok(s)
|
||||
fn render(&self, t: &FullType) -> eyre::Result<rust::Tokens> {
|
||||
let tstruct = self.render_struct(t)?;
|
||||
let timpl = self.render_impl(t)?;
|
||||
let mut out = rust::Tokens::new();
|
||||
out.append(tstruct);
|
||||
out.push();
|
||||
out.append(timpl);
|
||||
out.push();
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
fn render_struct(&self, t: &FullType) -> eyre::Result<Tokens> {
|
||||
@@ -74,12 +74,10 @@ mod tests {
|
||||
let res = handler.render(&t).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
res,
|
||||
"
|
||||
pub struct SomeName {} { }
|
||||
impl SomeName {} { }
|
||||
"
|
||||
.to_string()
|
||||
res.to_string().unwrap(),
|
||||
"pub struct SomeName {} { }
|
||||
impl SomeName {} { }"
|
||||
.to_string()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@@ -1,9 +1,9 @@
|
||||
use genco::Tokens;
|
||||
use genco::{prelude::rust, quote};
|
||||
use graphql_introspection_query::introspection_response::FullType;
|
||||
|
||||
use crate::predicates::is_custom_scalar_type;
|
||||
|
||||
use super::Handler;
|
||||
use super::{utility::render_description, Handler};
|
||||
|
||||
pub struct Scalar;
|
||||
|
||||
@@ -11,4 +11,30 @@ impl Handler for Scalar {
|
||||
fn predicate(&self, t: &FullType) -> bool {
|
||||
is_custom_scalar_type(t)
|
||||
}
|
||||
|
||||
fn render(&self, t: &FullType) -> eyre::Result<rust::Tokens> {
|
||||
let mut out = rust::Tokens::new();
|
||||
|
||||
let description =
|
||||
render_description(t).ok_or(eyre::anyhow!("could not find description"))?;
|
||||
let tstruct = self.render_struct(t)?;
|
||||
|
||||
out.append(description);
|
||||
out.push();
|
||||
out.append(tstruct);
|
||||
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
fn render_struct(&self, t: &FullType) -> eyre::Result<genco::prelude::rust::Tokens> {
|
||||
let name = t.name.as_ref().ok_or(eyre::anyhow!("name not found"))?;
|
||||
|
||||
Ok(quote! {
|
||||
pub struct $name (Scalar);
|
||||
})
|
||||
}
|
||||
|
||||
fn render_impl(&self, t: &FullType) -> eyre::Result<genco::prelude::rust::Tokens> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
15
crates/dagger-codegen/src/handlers/utility.rs
Normal file
15
crates/dagger-codegen/src/handlers/utility.rs
Normal file
@@ -0,0 +1,15 @@
|
||||
use genco::{prelude::*, quote};
|
||||
use graphql_introspection_query::introspection_response::FullType;
|
||||
|
||||
pub fn render_description(t: &FullType) -> Option<rust::Tokens> {
|
||||
if let Some(description) = t.description.as_ref() {
|
||||
let lines = description.split('\n');
|
||||
let output: rust::Tokens = quote! {
|
||||
$(for line in lines => $(format!("\n/// {line}")))
|
||||
};
|
||||
|
||||
return Some(output);
|
||||
}
|
||||
|
||||
None
|
||||
}
|
Reference in New Issue
Block a user