ingraph-codegen 0.1.0

The emission engine behind ingraph-derive: a plain declaration model in, the ingraph framework faces out.
Documentation
//! THE RECORD one recipe states — `impl DeclaredSearch`, the row that crosses the expansion wall.
//!
//! ```ignore
//! impl DeclaredSearch for TravelSearch {
//!   const SEATS: &'static [Seat] = &[Seat::of("text")];
//!   const MEMBERS: &'static [Member] = &[
//!     Member::of("subs").weighted(1.0).deep(50)
//!       .bound(&[Binding::of("zh").shared(Bridge::Total)]),
//!     Member::of("shots").weighted(2.0).deep(100).bound(&[
//!       Binding::of("text").shared(Bridge::Total).floored(),
//!       Binding::of("image").shared(Bridge::Total),
//!     ]),
//!   ];
//!   const LIMIT: Option<NonZeroU32> = NonZeroU32::new(20);
//! }
//! ```
//!
//! # IT STATES, AND IT NEVER DERIVES
//!
//! Every row here is what the declaration WROTE. An omitted `weight` is an absent row and never the
//! multiplicative identity written down; an omitted `limit` is an absent row and never the
//! framework's twenty. A reader across the wall that found a number here would have no way to tell
//! *the recipe said this* from *the emission guessed this*, and the whole reason the record family
//! exists is that the two are different facts.
//!
//! The TYPES are deliberately absent — a member's index and an arm's tower are types, a `const` row
//! cannot carry one, and the arity is variadic so an associated type cannot either. What checks a
//! type is the emitted bound, where the compiler can see it.
//!
//! A declared FLOOR is the one VALUE that still cannot travel: a const in the ENGINE's own currency,
//! an open type with no seat on a shared row. The row carries `is_floored` — the FACT.

use quote::quote;

use super::{MemberModel, SearchModel, SharedBridge};
use crate::emit::framework;

#[cfg(test)]
mod tests;

impl SearchModel {
  /// The `DeclaredSearch` impl, whole.
  #[must_use]
  pub fn record(&self) -> proc_macro2::TokenStream {
    let SearchModel { krate, name, .. } = self;
    let declared = framework(krate, &["retrieve"], "DeclaredSearch");
    let seat = framework(krate, &["retrieve"], "Seat");
    let member = framework(krate, &["retrieve"], "Member");
    let seats = self.seats.iter().map(|declared| {
      let word = declared.name.to_string();
      quote!(#seat::of(#word))
    });
    let members = self.members.iter().map(|declared| self.member(declared));
    let limit = self.limit.map_or_else(
      || quote!(::core::option::Option::None),
      |limit| {
        let limit = limit.get();
        quote!(::core::num::NonZeroU32::new(#limit))
      },
    );
    quote! {
      #[automatically_derived]
      impl #declared for #name {
        const SEATS: &'static [#seat] = &[#(#seats,)*];
        const MEMBERS: &'static [#member] = &[#(#members,)*];
        const LIMIT: ::core::option::Option<::core::num::NonZeroU32> = #limit;
      }
    }
  }

  /// ONE member's row — the builder chain, whose every call is a word the declaration wrote.
  fn member(&self, declared: &MemberModel) -> proc_macro2::TokenStream {
    let krate = &self.krate;
    let member = framework(krate, &["retrieve"], "Member");
    let binding = framework(krate, &["retrieve"], "Binding");
    let bridge = framework(krate, &["retrieve"], "Bridge");
    let field = declared.field.to_string();
    let weight = declared
      .weight
      .map(|weight| quote!(.weighted(#weight)))
      .into_iter();
    let depth = declared
      .depth
      .map(|depth| {
        let depth = depth.get();
        quote!(.deep(#depth))
      })
      .into_iter();
    let floored = declared
      .floor
      .as_ref()
      .map(|_| quote!(.floored()))
      .into_iter();
    let bindings = declared.arms.iter().map(|arm| {
      let name = arm.name.to_string();
      let shared = arm
        .shared
        .as_ref()
        .map(|shared| {
          let word = match shared.bridge {
            SharedBridge::Total => quote!(Total),
            SharedBridge::Fallible => quote!(Fallible),
            SharedBridge::Hook(_) => quote!(Hook),
            SharedBridge::FallibleHook(_) => quote!(FallibleHook),
          };
          quote!(.shared(#bridge::#word))
        })
        .into_iter();
      let floored = arm.floor.as_ref().map(|_| quote!(.floored())).into_iter();
      quote!(#binding::of(#name) #(#shared)* #(#floored)*)
    });
    quote! {
      #member::of(#field)
        #(#weight)*
        #(#depth)*
        #(#floored)*
        .bound(&[#(#bindings,)*])
    }
  }
}