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
use super::{MissingResourceError, Resources};
use crate::{AsyncResource, Shared};

/// One resource, and a second of a different type — the two the map has to keep apart.
struct Egemma(usize);
struct Whisper(&'static str);

/// A third nothing ever builds, so a lookup for it is the refusal.
struct Ecapa;

impl AsyncResource for Egemma {
  type Error = core::convert::Infallible;
  type Options = usize;

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

impl AsyncResource for Whisper {
  type Error = core::convert::Infallible;
  type Options = &'static str;

  async fn build(_ctx: &Resources, model: &'static str) -> Result<Self, Self::Error> {
    Ok(Self(model))
  }
}

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

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

/// Two resources in, two out, each at its own type — which is the whole of what the erasure has to
/// get right.
#[test]
fn a_typed_lookup_answers_at_the_type_it_was_asked_at() {
  let ctx = Resources::new()
    .with(Shared::new(Egemma(768)))
    .with(Shared::new(Whisper("large-v3")));

  assert_eq!(ctx.len(), 2);
  assert_eq!(ctx.shared::<Egemma>().expect("it is in").0, 768);
  assert_eq!(ctx.shared::<Whisper>().expect("it is in").0, "large-v3");
}

/// A lookup for a resource nothing built NAMES the type — the module note's honest tooth, and the
/// reason the door is a `Result` rather than an `Option`.
#[test]
fn a_resource_nothing_built_is_a_refusal_that_names_it() {
  let ctx = Resources::new().with(Shared::new(Egemma(768)));

  let refusal = ctx
    .shared::<Ecapa>()
    .expect_err("nothing ever built an `Ecapa`");
  assert_eq!(refusal, MissingResourceError::of::<Ecapa>());
  assert!(refusal.resource().as_str().contains("Ecapa"));
  assert!(refusal.to_string().contains("Ecapa"), "{refusal}");
}

/// `holds` answers the same question without wanting the handle.
#[test]
fn holds_reads_membership_at_the_type() {
  let mut ctx = Resources::new();
  assert!(ctx.is_empty());
  assert!(!ctx.holds::<Egemma>());

  ctx.insert(Shared::new(Egemma(512)));
  assert!(ctx.holds::<Egemma>());
  assert!(!ctx.holds::<Whisper>());
  assert!(!ctx.is_empty());
}

/// A lookup CLONES the handle rather than rebuilding: one construction, N holders — which is what
/// makes reaching for a model in five constructions cost five `Arc` bumps.
#[test]
fn a_lookup_clones_the_handle_rather_than_the_resource() {
  let built = Shared::new(Egemma(768));
  let ctx = Resources::new().with(built);

  let first = ctx.shared::<Egemma>().expect("it is in");
  let second = ctx.shared::<Egemma>().expect("it is in");
  assert!(core::ptr::eq(first.get(), second.get()));
  // The map's own handle plus the two taken out.
  assert_eq!(first.holders(), 3);
}

/// A second handle for a type already in states the context's last word — the answer a map can
/// give, and one the walk never asks for.
#[test]
fn a_second_handle_for_one_type_replaces_the_first() {
  let ctx = Resources::new()
    .with(Shared::new(Egemma(768)))
    .with(Shared::new(Egemma(1024)));

  assert_eq!(ctx.len(), 1);
  assert_eq!(ctx.shared::<Egemma>().expect("it is in").0, 1024);
}

/// The context crosses threads, because everything in it already does.
#[test]
fn the_context_is_send_and_sync() {
  const fn crosses<T: Send + Sync>() {}

  crosses::<Resources>();
}

/// The `Debug` is the ARITY, never the resources — see the impl's own note.
#[test]
fn the_debug_reports_how_many_were_built() {
  let ctx = Resources::new()
    .with(Shared::new(Egemma(768)))
    .with(Shared::new(Whisper("large-v3")));

  assert_eq!(
    format!("{ctx:?}"),
    "Resources { resources: 2, goodbyes: 0 }"
  );
}