use quote::quote;
use crate::{
emit::leading,
interface::{InterfaceField, InterfaceModel},
model::DatabaseKind,
unit,
};
impl InterfaceModel {
pub(crate) fn ordering(&self) -> proc_macro2::TokenStream {
if !self.projects() {
return quote!();
}
let vis = &self.vis;
let interface = &self.name;
let name = super::default_order_by_name(interface);
let direction_ty = self.framework(&["graphql"], "OrderDirection");
let order_term = self.framework(&["cursor"], "OrderTerm");
let term_name = self.framework(&["cursor"], "TermName");
let field_name = self.framework(&["field"], "FieldName");
let stored: Vec<&InterfaceField> = self.stored().collect();
let rows: Vec<unit::OrderColumn> = stored.iter().map(|field| order_row(field)).collect();
let header = unit::UnitHeader {
vis,
name: name.clone(),
subject: format!("[`{interface}`]"),
note: Some(
"The columns are the INTERFACE's own vocabulary and never a member's: a union projects the \
interface's names, so those are the names a client may sort by. There is one rendering \
whatever database answers, because every arm has already aliased its member's column to \
the name this term carries."
.to_owned(),
),
};
let (declaration, direction) = unit::order_by(&header, &direction_ty, &rows);
let terms = stored.iter().zip(&rows).map(|(field, row)| {
let variant = &row.variant;
let external = field.name.to_string();
let build = if field.collects() {
quote!(#order_term::edge)
} else {
quote!(#order_term::new)
};
quote! {
Self::#variant(__ingraph_direction) =>
#build(#term_name::new(#external), #field_name::new(#external), __ingraph_direction),
}
});
let term_doc = leading(
"This term as a statement renders it: its rename-proof identity, the column the union \
projects it under, and the direction. The identity and the column are ONE name here — an \
interface's promise is the name, and no `persist(…)` can move it.",
);
let entries = self
.sql_databases()
.into_iter()
.map(|kind| self.entry_point(kind, &name));
let to_subject_path = self.framework(&["graphql"], "ToSubjectPath");
let subject_path = self.framework(&["graphql"], "SubjectPath");
let subject =
unit::order_subject_path(&name, &to_subject_path, &subject_path, &term_name, &rows);
quote! {
#declaration
#subject
impl #name {
#direction
#[doc = #term_doc]
#[inline]
#[must_use]
#vis const fn term(self) -> #order_term {
match self {
#(#terms)*
}
}
}
const _: () = {
#(#entries)*
};
}
}
fn entry_point(&self, kind: DatabaseKind, order_by: &syn::Ident) -> proc_macro2::TokenStream {
let vis = &self.vis;
let interface = &self.name;
let find = self.framework(&["query", "sql"], "InterfaceFind");
let begin_read = self.framework(&["query", "sql"], "BeginRead");
let acquire = self.framework(&["sqlx"], "Acquire");
let gate = self.framework(&["gate"], &kind.gate().to_string());
let (module, item) = kind.backend();
let backend = self.framework(module, item);
let name = kind.finder();
let executor = crate::emit::parameter("EXEC");
let doc = leading(&format!(
"Page the [`{interface}`]s stored in {what} — every member's rows in ONE order, resumable \
across the members.\n\nThe order is written as [`{order_by}`] terms and completed with the \
tail that makes it total; the page is bracketed in this dialect's read transaction, or \
passes through the one the surface already holds.",
what = super::row::what(kind)
));
quote! {
#gate! {
impl #interface {
#[doc = #doc]
#[inline]
#vis fn #name<'__ingraph_a, '__ingraph_c, #executor>(
executor: #executor,
) -> #find<'__ingraph_a, Self, #backend, #executor>
where
#executor: #acquire<'__ingraph_c, Database = #backend> + #begin_read,
{
#find::new(executor)
}
}
}
}
}
}
fn order_row(field: &InterfaceField) -> unit::OrderColumn {
let name = &field.name;
if !field.collects() {
return unit::OrderColumn::of(name, format!("shared `{name}` column"));
}
unit::OrderColumn::of(name, format!("shared `{name}` collection")).with_note(
"The order over a collection is element by element, shorter-is-less on a prefix, which is \
`ingraph`'s one meaning for a collection's order. A PAGE over this term is refused before a \
row is read: the union projects no column for a collection, so the term's subject is \
`TermSubject::Edge` and the degrade law's second row answers it."
.to_owned(),
)
}