use quote::{format_ident, quote};
const BUILT: &str = "__ingraph_built";
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Construction {
pub at: Option<syn::Path>,
pub entry: syn::Ident,
pub finish: Option<syn::Ident>,
pub fallible: bool,
pub blanket: Vec<Blanket>,
pub calls: Vec<Injection>,
}
impl Default for Construction {
fn default() -> Self {
Self {
at: None,
entry: format_ident!("new"),
finish: None,
fallible: false,
blanket: Vec::new(),
calls: Vec::new(),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Blanket {
pub ty: syn::Type,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Injection {
pub method: syn::Ident,
pub value: syn::Expr,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Build {
pub at: Option<syn::Path>,
pub seat: Option<Seat>,
pub cross: Option<Cross>,
pub option: bool,
pub skip: bool,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Seat {
Arg,
FinishArg,
With(Option<syn::Ident>),
Set(Option<syn::Ident>),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Cross {
Into,
Map(syn::Path),
}
pub struct Supplied {
pub fill: Filled,
pub gate: proc_macro2::TokenStream,
pub value: proc_macro2::TokenStream,
}
pub enum Filled {
Entry,
Finish,
Link(syn::Ident),
Setter(syn::Ident),
}
impl Seat {
#[must_use]
pub fn filled(&self, column: &syn::Ident) -> Filled {
match self {
Self::Arg => Filled::Entry,
Self::FinishArg => Filled::Finish,
Self::With(renamed) => Filled::Link(
renamed
.clone()
.unwrap_or_else(|| crate::method::Member::With.name(column)),
),
Self::Set(renamed) => Filled::Setter(
renamed
.clone()
.unwrap_or_else(|| crate::method::Member::Set.name(column)),
),
}
}
}
impl Construction {
#[must_use]
pub fn blankets(&self, ty: &syn::Type) -> bool {
let written = quote!(#ty).to_string();
self.blanket.iter().any(|rule| {
let rule = &rule.ty;
quote!(#rule).to_string() == written
})
}
#[must_use]
pub fn call(&self, host: &syn::Path, supplied: &[Supplied]) -> proc_macro2::TokenStream {
let entry = &self.entry;
let arguments = supplied
.iter()
.filter(|one| matches!(one.fill, Filled::Entry))
.map(|one| {
let (gate, value) = (&one.gate, &one.value);
quote!(#gate #value)
});
let mut head = quote!(#host::#entry(#(#arguments),*));
if self.fallible && self.finish.is_none() {
head = quote!(#head?);
}
let finish = self.finish.as_ref().map(|finish| {
let arguments = supplied
.iter()
.filter(|one| matches!(one.fill, Filled::Finish))
.map(|one| {
let (gate, value) = (&one.gate, &one.value);
quote!(#gate #value)
});
let call = quote!(.#finish(#(#arguments),*));
if self.fallible { quote!(#call?) } else { call }
});
let links: Vec<(&proc_macro2::TokenStream, Link<'_>)> = supplied
.iter()
.filter_map(|one| match &one.fill {
Filled::Link(method) => Some((&one.gate, Link::Chained(method, &one.value))),
Filled::Setter(method) => Some((&one.gate, Link::InPlace(method, &one.value))),
Filled::Entry | Filled::Finish => None,
})
.collect();
let injected: Vec<proc_macro2::TokenStream> = self
.calls
.iter()
.map(|call| {
let (method, value) = (&call.method, &call.value);
quote!(.#method(#value))
})
.collect();
let unrolled = links
.iter()
.any(|(gate, link)| !gate.is_empty() || matches!(link, Link::InPlace(..)));
if !unrolled {
let chained = links.iter().map(|(_, link)| link.chained());
return quote!(#head #(#chained)* #(#injected)* #finish);
}
let built = format_ident!("{BUILT}");
let steps = links.iter().map(|(gate, link)| {
let step = link.statement(&built);
quote! {
#gate
#step
}
});
quote! {
{
let mut #built = #head;
#(#steps)*
#(#built = #built #injected;)*
#built #finish
}
}
}
}
enum Link<'a> {
Chained(&'a syn::Ident, &'a proc_macro2::TokenStream),
InPlace(&'a syn::Ident, &'a proc_macro2::TokenStream),
}
impl Link<'_> {
fn chained(&self) -> proc_macro2::TokenStream {
match self {
Self::Chained(method, value) | Self::InPlace(method, value) => quote!(.#method(#value)),
}
}
fn statement(&self, built: &syn::Ident) -> proc_macro2::TokenStream {
match self {
Self::Chained(method, value) => quote! {
{
#built = #built.#method(#value);
}
},
Self::InPlace(method, value) => quote! {
{
#built.#method(#value);
}
},
}
}
}
#[cfg(test)]
mod tests;