use quote::quote;
use crate::{
emit::leading,
interface::{InterfaceField, InterfaceModel},
model::DatabaseKind,
};
impl InterfaceModel {
pub(crate) fn bindings(&self) -> proc_macro2::TokenStream {
if !self.projects() {
return quote!();
}
let blocks = self.sql_databases().into_iter().map(|kind| {
let gate = self.framework(&["gate"], &kind.gate().to_string());
let binding = self.binding(kind);
let hydration = self.hydration(kind);
let crossing = self.crossing(kind);
quote! {
#gate! {
#binding
#hydration
#crossing
}
}
});
quote! {
const _: () = {
#(#blocks)*
};
}
}
fn binding(&self, kind: DatabaseKind) -> proc_macro2::TokenStream {
let interface = &self.name;
let vocabulary = super::default_kind_name(interface);
let row = super::default_row_name(interface, kind);
let cursor = self.cursor_name(kind);
let comparison = self.comparison_name(kind);
let order_by = super::default_order_by_name(interface);
let sql_interface = self.framework(&["query", "sql"], "SqlInterface");
let sql_query = self.framework(&["query", "sql"], "SqlQuery");
let record = self.framework(&["transform"], "Record");
let arm = self.framework(&["interface"], "InterfaceArm");
let column = self.framework(&["interface"], "InterfaceColumn");
let field_name = self.framework(&["field"], "FieldName");
let address_ty = self.framework(&["comparison"], "RecordAddress");
let order_term = self.framework(&["cursor"], "OrderTerm");
let id_alias = self.framework(&["interface"], "ID");
let words = self
.mints_the_default_words(kind)
.then(proc_macro2::TokenStream::new);
let words = words.unwrap_or_else(|| {
let discriminant = self.kind_word(kind);
let source = self.source_word(kind);
quote! {
const KIND: &'static #field_name = #field_name::new(#discriminant);
const SOURCE: &'static #field_name = #field_name::new(#source);
}
});
let (module, item) = kind.backend();
let backend = self.framework(module, item);
let arms = self.members.iter().map(|member| {
let variant = &member.ident;
let columns = self.projected().map(|field| {
let promised = field.name.to_string();
let held = field.column(&member.ident).to_string();
quote!(#column::new(#field_name::new(#promised), #field_name::new(#held)),)
});
quote!(#arm::new(#vocabulary::#variant, &[#(#columns)*]),)
});
let addresses = self.members.iter().map(|member| {
let variant = &member.ident;
let ty = &member.ty;
quote! {
#vocabulary::#variant =>
<<#ty as #record<#backend>>::Row as #sql_query<#backend>>::ADDRESS,
}
});
let values = self.projected().map(|field| {
let name = &field.name;
let external = field.name.to_string();
let variant = crate::emit::upper_camel(&external);
quote! {
#external => ::core::option::Option::Some(
#cursor::#variant(::core::clone::Clone::clone(&__ingraph_row.#name)),
),
}
});
let collections: Vec<&InterfaceField> = self.collected().collect();
let complex = self.framework(&["comparison"], "ComplexComparison");
let comparison_backend = self.framework(&["comparison"], "Backend");
let frame = self.framework(&["comparison"], "Frame");
let at_ty = self.framework(&["comparison"], "At");
let collection_arms = self.members.iter().map(|member| {
let variant = &member.ident;
let parts = collections.iter().map(|field| {
let rust = &field.name;
let held = field.column(&member.ident).to_string();
quote! {
let __ingraph_frame = #at_ty::frame(__ingraph_at, #field_name::new(#held));
__ingraph_builder = #complex::<#backend>::apply(
&__ingraph_filter.#rust,
#frame::at(&__ingraph_frame),
__ingraph_builder,
)?;
}
});
quote!(#vocabulary::#variant => { #(#parts)* })
});
let collection_counts = self.members.iter().map(|member| {
let variant = &member.ident;
let counts = collections.iter().map(|field| {
let rust = &field.name;
quote!(#complex::<#backend>::conjuncts(&__ingraph_filter.#rust))
});
quote!(#vocabulary::#variant => #(#counts)+*,)
});
let collection_items = collections.is_empty().then(proc_macro2::TokenStream::new);
let collection_items = collection_items.unwrap_or_else(|| {
let count_doc = leading(
"How many conjuncts this predicate's COLLECTION columns render for one member's arm.",
);
let apply_doc = leading(
"Render this predicate's COLLECTION columns for one member's arm, at the column THAT member \
calls each of them — which is what its own edge table is derived from.",
);
quote! {
#[doc = #count_doc]
fn collection_conjuncts(
__ingraph_filter: &Self::Comparison,
__ingraph_kind: #vocabulary,
) -> ::core::primitive::usize {
match __ingraph_kind {
#(#collection_counts)*
}
}
#[doc = #apply_doc]
fn collections<'__ingraph_q>(
__ingraph_filter: &'__ingraph_q Self::Comparison,
__ingraph_kind: #vocabulary,
__ingraph_at: #at_ty<'_>,
mut __ingraph_builder: <#backend as #comparison_backend>::Builder<'__ingraph_q>,
) -> ::core::result::Result<
<#backend as #comparison_backend>::Builder<'__ingraph_q>,
<#backend as #comparison_backend>::Error,
> {
match __ingraph_kind {
#(#collection_arms)*
}
::core::result::Result::Ok(__ingraph_builder)
}
}
});
let address_doc = leading(
"Where this member keeps its rows, and what one of them is addressed by — the MEMBER's own \
answer, reached through its query binding. An interface holds no table of its own to give, \
and a copy here would be a second spelling of a name `persist(<db>(table = \"…\"))` can move.",
);
let term_doc = leading(
"One written term as the statement renders it. There is no per-database spelling to choose \
between: every arm has already aliased its member's column to the interface's own name, so \
what the `ORDER BY` names is that name in every database.",
);
let value_doc = leading(
"The value this row holds at `term`. The TAIL's two slots are minted from the ADDRESS — the \
discriminant is what the address says it is, so the pair cannot disagree — and every other \
term is a shared column the projection already decoded.",
);
quote! {
#[automatically_derived]
impl #sql_interface<#backend> for #interface {
type Kind = #vocabulary;
type Row = #row;
type Comparison = #comparison<#backend>;
type Value = #cursor;
type Order = #order_by;
const ARMS: &'static [#arm<#vocabulary>] = &[#(#arms)*];
#words
#[doc = #address_doc]
fn address(kind: #vocabulary) -> #address_ty {
match kind {
#(#addresses)*
}
}
#[doc = #value_doc]
fn value(
__ingraph_row: &#row,
__ingraph_term: &#order_term,
) -> ::core::option::Option<#cursor> {
let __ingraph_column = #order_term::column(__ingraph_term);
if __ingraph_column == <#interface as #sql_interface<#backend>>::KIND {
return ::core::option::Option::Some(
#cursor::__IgKind(#row::id(__ingraph_row).kind()),
);
}
if __ingraph_column == #id_alias {
return ::core::option::Option::Some(#cursor::__IgAddress(
::core::clone::Clone::clone(#row::id(__ingraph_row)),
));
}
match #field_name::as_str(__ingraph_column) {
#(#values)*
_ => ::core::option::Option::None,
}
}
#[doc = #term_doc]
#[inline]
fn term(order: #order_by) -> #order_term {
#order_by::term(order)
}
#collection_items
}
}
}
}