ingraph 0.1.0

A declaration-driven indexing framework: records and nodes are written as Rust types, and the storage rows, the dataflow between them and the GraphQL query surface are emitted from the declaration.
Documentation
//! What layer two's seams owe: the MODE picks the face, one struct may wear both with two targets,
//! the machine bag hands one machine back rather than building a second, and a declaration's
//! unbound corpora become the launch floor's own rows.
//!
//! The launch-driven half — a real `Creation`, an emitted root, both faces walked before the graph
//! serves — is `ingraph/tests/init_walk_modes.rs`, where a deployment exists to drive.
use crate::Resources;

use super::{AsyncInitialize, Initialize, Initializes, Makers, UnboundCorpus};
use crate::{
  Async, AsyncResource, AsyncTypeVisitor, Block, Describe, Habitat, ResourceKind, Shared,
  SlotBinding, SlotColumn, SlotName, SlotSchema, TypeVisitor,
};

/// A BLOCKING target and an AWAITED one — the mixed deployment, at its smallest.
struct Corpora;
struct Remote;

impl AsyncResource for Corpora {
  type Options = ();
  type Error = core::convert::Infallible;

  async fn build(_ctx: &Resources, _options: ()) -> Result<Self, Self::Error> {
    Ok(Self)
  }
}

impl AsyncResource for Remote {
  type Options = ();
  type Error = core::convert::Infallible;

  async fn build(_ctx: &Resources, _options: ()) -> Result<Self, Self::Error> {
    Ok(Self)
  }
}

impl Habitat for Corpora {
  type Blueprint = &'static [SlotSchema];
}

impl Habitat for Remote {
  type Blueprint = &'static [SlotSchema];
}

/// **ONE STRUCT, BOTH FACES, TWO TARGETS** — the visitor freedom the ruling names, put to the
/// compiler rather than described: this machine is a `TypeVisitor` for one mount and an
/// `AsyncTypeVisitor` for another, and no blanket between the faces could have admitted it.
#[derive(Default)]
struct Maker {
  made: Vec<String>,
}

impl TypeVisitor for Maker {
  type Db = Corpora;
  type Error = core::convert::Infallible;

  fn next_type<E: Describe<Corpora>>(&mut self) -> Result<(), Self::Error> {
    for corpus in E::BLUEPRINT {
      self
        .made
        .push(format!("blocking:{}", corpus.slot().as_ref()));
    }
    Ok(())
  }
}

impl AsyncTypeVisitor for Maker {
  type Db = Remote;
  type Error = core::convert::Infallible;

  async fn next_type<E: Describe<Remote>>(&mut self) -> Result<(), Self::Error> {
    for corpus in E::BLUEPRINT {
      self
        .made
        .push(format!("awaited:{}", corpus.slot().as_ref()));
    }
    Ok(())
  }
}

impl Initialize for Corpora {
  type Visitor = Maker;

  fn initializer(_mount: Shared<Self>) -> Maker {
    Maker::default()
  }
}

impl AsyncInitialize for Remote {
  type Visitor = Maker;

  fn initializer(_mount: Shared<Self>) -> Maker {
    Maker::default()
  }
}

/// ONE DECLARATION with a home in BOTH targets — the mixed node, whose fields need initializing on
/// two sides at once.
struct Media;

impl Describe<Corpora> for Media {
  const BLUEPRINT: &'static [SlotSchema] = &[SlotSchema::new(
    SlotName::new("media__title__lexical"),
    &[SlotColumn::text("title")],
  )];
}

impl Describe<Remote> for Media {
  const BLUEPRINT: &'static [SlotSchema] = &[SlotSchema::new(
    SlotName::new("media__embedding__remote"),
    &[SlotColumn::text("embedding")],
  )];
}

/// A SECOND declaration in the blocking target alone, so *one machine per target, shown every
/// declaration that lives there* is observable.
struct Caption;

impl Describe<Corpora> for Caption {
  const BLUEPRINT: &'static [SlotSchema] = &[SlotSchema::new(
    SlotName::new("caption__text__lexical"),
    &[SlotColumn::text("text")],
  )];
}

/// **THE MODE PICKS THE FACE.** `Initializes<Block>` drives the blocking visitor and
/// `Initializes<Async>` the awaited one, both through one call shape — which is what lets an emitted
/// launch spell one line per target whichever face that target wears.
#[tokio::test]
async fn the_mode_picks_the_face_and_both_are_driven_by_one_call_shape() {
  let mut blocking = <Corpora as Initializes<Block>>::initializer(Shared::new(Corpora));
  <Corpora as Initializes<Block>>::show::<Media>(&mut blocking)
    .await
    .expect("the blocking face refuses nothing");
  assert_eq!(blocking.made, ["blocking:media__title__lexical"]);

  let mut awaited = <Remote as Initializes<Async>>::initializer(Shared::new(Remote));
  <Remote as Initializes<Async>>::show::<Media>(&mut awaited)
    .await
    .expect("the awaited face refuses nothing");
  assert_eq!(awaited.made, ["awaited:media__embedding__remote"]);
}

/// **THE COMPLETENESS LAW's SMALLEST FORM**: one declaration's homes are made on BOTH sides, and the
/// union of the two steps is what covers it. Neither face is skipped for the other being present.
#[tokio::test]
async fn a_mixed_declaration_is_visited_on_both_faces() {
  let mut blocking = <Corpora as Initializes<Block>>::initializer(Shared::new(Corpora));
  let mut awaited = <Remote as Initializes<Async>>::initializer(Shared::new(Remote));

  <Corpora as Initializes<Block>>::show::<Media>(&mut blocking)
    .await
    .expect("made");
  <Remote as Initializes<Async>>::show::<Media>(&mut awaited)
    .await
    .expect("made");

  assert_eq!(
    [blocking.made.as_slice(), awaited.made.as_slice()].concat(),
    [
      "blocking:media__title__lexical",
      "awaited:media__embedding__remote"
    ],
    "a mixed node's two sides are two steps, and both ran",
  );
}

/// **ONE MACHINE PER TARGET.** The bag builds on first ask and hands the SAME machine back after —
/// which is what makes a slot maker see every declaration that lives in its mount, in member order,
/// rather than a fresh one per declaration.
#[test]
fn the_bag_builds_one_machine_and_hands_it_back() {
  let mut makers = Makers::default();

  TypeVisitor::next_type::<Media>(makers.entry::<Maker>(Maker::default))
    .expect("the first declaration is made");
  // A SECOND declaration in the same target: the closure panics if the bag built a second machine,
  // so what this asserts is that one maker saw both — the walk's own shape, reached from steps that
  // are emitted per declaration.
  TypeVisitor::next_type::<Caption>(makers.entry::<Maker>(|| panic!("a second machine was built")))
    .expect("the second declaration is made");

  assert_eq!(
    makers.entry::<Maker>(Maker::default).made,
    [
      "blocking:media__title__lexical",
      "blocking:caption__text__lexical"
    ],
    "one machine per target, shown every declaration that lives there, in member order",
  );
}

/// The bag's `Debug` reports HOW MANY machines and never what they hold — a slot maker is the
/// author's type, and deriving would put a `Debug` bound on every one of them.
#[test]
fn the_bag_reports_its_size_and_not_its_machines() {
  let mut makers = Makers::default();
  let _ = makers.entry::<Maker>(Maker::default);
  assert_eq!(format!("{makers:?}"), "Makers { built: 1 }");
}

/// **THE UNBOUND HALF.** A declaration states the corpora whose legs named nothing as a `const`, and
/// each becomes the launch floor's own row — the input the sole-default law resolves, which is what
/// makes laws two and three reachable from an emitted launch.
#[test]
fn an_unbound_corpus_becomes_the_launch_floors_own_row() {
  const UNBOUND: &[UnboundCorpus] = &[UnboundCorpus::new(
    SlotName::new("bare__body__body"),
    ResourceKind::Documents,
  )];

  let rows: Vec<SlotBinding> = UNBOUND.iter().map(SlotBinding::from).collect();
  assert_eq!(
    rows,
    [SlotBinding::new(
      "bare__body__body",
      ResourceKind::Documents
    )],
  );
}

/// A declaration that elects into nothing takes BOTH defaults, which is what keeps a hand-written
/// one whole: an empty walk and an empty unbound list, neither of them written.
#[tokio::test]
async fn a_declaration_that_elects_nothing_takes_both_defaults() {
  struct Bare;
  impl super::Habitats for Bare {}

  assert!(<Bare as super::Habitats>::UNBOUND.is_empty());
}