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 core::convert::Infallible;

use super::{AsyncConstructor, Resources};
use crate::{AsyncResource, Shared};

/// A shared citizen a construction reaches for, so the `ctx` half of the contract is exercised
/// rather than only spelled.
struct Egemma(usize);

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

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

/// A node with knobs, whose options carry a `Default` — so BOTH doors are available on it.
#[derive(Debug)]
struct Decode {
  threads: usize,
  dim: usize,
}

struct DecodeOptions {
  threads: usize,
}

impl Default for DecodeOptions {
  fn default() -> Self {
    Self { threads: 4 }
  }
}

impl AsyncConstructor for Decode {
  type Error = DecodeError;
  type Options = DecodeOptions;

  async fn with_options(ctx: &Resources, options: DecodeOptions) -> Result<Self, Self::Error> {
    if options.threads == 0 {
      return Err(DecodeError);
    }
    Ok(Self {
      threads: options.threads,
      dim: ctx.shared::<Egemma>().map_err(|_| DecodeError)?.0,
    })
  }
}

/// The author's own currency, which the trait bounds by nothing.
#[derive(Debug, PartialEq, Eq)]
struct DecodeError;

/// A ZERO-KNOB node — the shape the record names: `type Options = ();`.
struct Watchdog;

impl AsyncConstructor for Watchdog {
  type Error = Infallible;
  type Options = ();

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

/// The context a construction is handed, with the one resource in it.
fn context() -> Resources {
  Resources::new().with(Shared::new(Egemma(768)))
}

/// `with_options` is the launch's door: the section's own options go in, and what comes out carries
/// them.
#[tokio::test]
async fn with_options_takes_the_sections_own_options() {
  let decode = Decode::with_options(&context(), DecodeOptions { threads: 8 })
    .await
    .expect("eight threads is a legal budget");
  assert_eq!(decode.threads, 8);
}

/// `new` DELEGATES — it runs `with_options` at the options' own default and never a second body.
///
/// Read off the value: the default is four and nothing else in this file produces a four.
#[tokio::test]
async fn new_delegates_to_with_options_at_the_options_default() {
  let decode = Decode::new(&context()).await.expect("the default builds");
  assert_eq!(decode.threads, DecodeOptions::default().threads);
  assert_eq!(decode.threads, 4);
}

/// Both doors run the SAME body, which is what the delegation is for: the resource lookup written
/// once in `with_options` happens on the `new` path too.
#[tokio::test]
async fn both_doors_reach_the_shared_resource() {
  let ctx = context();
  let tuned = Decode::with_options(&ctx, DecodeOptions { threads: 2 })
    .await
    .expect("it builds");
  let plain = Decode::new(&ctx).await.expect("it builds");
  assert_eq!((tuned.dim, plain.dim), (768, 768));
}

/// A refusal is the AUTHOR's type, handed back unwrapped — the launch is what names the node around
/// it, and that is the round after this one.
#[tokio::test]
async fn a_refused_construction_reports_in_the_authors_own_currency() {
  let refusal = Decode::with_options(&context(), DecodeOptions { threads: 0 })
    .await
    .expect_err("no threads is no decoder");
  assert_eq!(refusal, DecodeError);
}

/// A construction that reaches for a resource nothing built gets the context's own refusal, mapped
/// into the node's currency — the honest tooth, seen from the body that trips it.
#[tokio::test]
async fn a_construction_reaching_past_its_declaration_is_refused() {
  let refusal = Decode::with_options(&Resources::new(), DecodeOptions { threads: 8 })
    .await
    .expect_err("nothing built an `Egemma`");
  assert_eq!(refusal, DecodeError);
}

/// A zero-knob node takes `()` and gets `new` for free, `()` being `Default`.
#[tokio::test]
async fn a_zero_knob_node_takes_the_unit_options() {
  let Watchdog = Watchdog::new(&Resources::new())
    .await
    .expect("nothing can refuse");
}