Skip to main content

ingraph_codegen/search/
record.rs

1//! THE RECORD one recipe states — `impl DeclaredSearch`, the row that crosses the expansion wall.
2//!
3//! ```ignore
4//! impl DeclaredSearch for TravelSearch {
5//!   const SEATS: &'static [Seat] = &[Seat::of("text")];
6//!   const MEMBERS: &'static [Member] = &[
7//!     Member::of("subs").weighted(1.0).deep(50)
8//!       .bound(&[Binding::of("zh").shared(Bridge::Total)]),
9//!     Member::of("shots").weighted(2.0).deep(100).bound(&[
10//!       Binding::of("text").shared(Bridge::Total).floored(),
11//!       Binding::of("image").shared(Bridge::Total),
12//!     ]),
13//!   ];
14//!   const LIMIT: Option<NonZeroU32> = NonZeroU32::new(20);
15//! }
16//! ```
17//!
18//! # IT STATES, AND IT NEVER DERIVES
19//!
20//! Every row here is what the declaration WROTE. An omitted `weight` is an absent row and never the
21//! multiplicative identity written down; an omitted `limit` is an absent row and never the
22//! framework's twenty. A reader across the wall that found a number here would have no way to tell
23//! *the recipe said this* from *the emission guessed this*, and the whole reason the record family
24//! exists is that the two are different facts.
25//!
26//! The TYPES are deliberately absent — a member's index and an arm's tower are types, a `const` row
27//! cannot carry one, and the arity is variadic so an associated type cannot either. What checks a
28//! type is the emitted bound, where the compiler can see it.
29//!
30//! A declared FLOOR is the one VALUE that still cannot travel: a const in the ENGINE's own currency,
31//! an open type with no seat on a shared row. The row carries `is_floored` — the FACT.
32
33use quote::quote;
34
35use super::{MemberModel, SearchModel, SharedBridge};
36use crate::emit::framework;
37
38#[cfg(test)]
39mod tests;
40
41impl SearchModel {
42  /// The `DeclaredSearch` impl, whole.
43  #[must_use]
44  pub fn record(&self) -> proc_macro2::TokenStream {
45    let SearchModel { krate, name, .. } = self;
46    let declared = framework(krate, &["retrieve"], "DeclaredSearch");
47    let seat = framework(krate, &["retrieve"], "Seat");
48    let member = framework(krate, &["retrieve"], "Member");
49    let seats = self.seats.iter().map(|declared| {
50      let word = declared.name.to_string();
51      quote!(#seat::of(#word))
52    });
53    let members = self.members.iter().map(|declared| self.member(declared));
54    let limit = self.limit.map_or_else(
55      || quote!(::core::option::Option::None),
56      |limit| {
57        let limit = limit.get();
58        quote!(::core::num::NonZeroU32::new(#limit))
59      },
60    );
61    quote! {
62      #[automatically_derived]
63      impl #declared for #name {
64        const SEATS: &'static [#seat] = &[#(#seats,)*];
65        const MEMBERS: &'static [#member] = &[#(#members,)*];
66        const LIMIT: ::core::option::Option<::core::num::NonZeroU32> = #limit;
67      }
68    }
69  }
70
71  /// ONE member's row — the builder chain, whose every call is a word the declaration wrote.
72  fn member(&self, declared: &MemberModel) -> proc_macro2::TokenStream {
73    let krate = &self.krate;
74    let member = framework(krate, &["retrieve"], "Member");
75    let binding = framework(krate, &["retrieve"], "Binding");
76    let bridge = framework(krate, &["retrieve"], "Bridge");
77    let field = declared.field.to_string();
78    let weight = declared
79      .weight
80      .map(|weight| quote!(.weighted(#weight)))
81      .into_iter();
82    let depth = declared
83      .depth
84      .map(|depth| {
85        let depth = depth.get();
86        quote!(.deep(#depth))
87      })
88      .into_iter();
89    let floored = declared
90      .floor
91      .as_ref()
92      .map(|_| quote!(.floored()))
93      .into_iter();
94    let bindings = declared.arms.iter().map(|arm| {
95      let name = arm.name.to_string();
96      let shared = arm
97        .shared
98        .as_ref()
99        .map(|shared| {
100          let word = match shared.bridge {
101            SharedBridge::Total => quote!(Total),
102            SharedBridge::Fallible => quote!(Fallible),
103            SharedBridge::Hook(_) => quote!(Hook),
104            SharedBridge::FallibleHook(_) => quote!(FallibleHook),
105          };
106          quote!(.shared(#bridge::#word))
107        })
108        .into_iter();
109      let floored = arm.floor.as_ref().map(|_| quote!(.floored())).into_iter();
110      quote!(#binding::of(#name) #(#shared)* #(#floored)*)
111    });
112    quote! {
113      #member::of(#field)
114        #(#weight)*
115        #(#depth)*
116        #(#floored)*
117        .bound(&[#(#bindings,)*])
118    }
119  }
120}