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
//! The rows' own SENTENCES, read without a graph.
//!
//! What the walk finds is pinned where the walk runs. What is here is what each row SAYS once it
//! has been found — the fix it names, and the two cases whose sentences must not converge.
use crate::Resources;

use super::{
  DisabledUpstreamError, LandsNowhereError, PureLeafError, Refusal, UndeclaredResourceError,
  UnknownNodeError,
};
use crate::{AsyncResource, EdgeId, NodeId, ResourceDemand};

/// Something to name in the resource row.
struct Whisper;

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

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

fn starved() -> Box<DisabledUpstreamError> {
  Box::new(DisabledUpstreamError::new(
    EdgeId::new(1),
    NodeId::new(2),
    "captions".into(),
    NodeId::new(1),
    "keyframes".into(),
  ))
}

#[test]
fn every_row_names_the_thing_and_then_the_fix() {
  let unknown = Refusal::UnknownNode(UnknownNodeError::new("keyfrmaes".into()));
  let rendered = unknown.to_string();
  assert!(rendered.contains("keyfrmaes"), "{rendered}");
  assert!(rendered.contains("does not declare"), "{rendered}");

  let undeclared = Refusal::UndeclaredResource(Box::new(UndeclaredResourceError::new(
    NodeId::new(1),
    "keyframes".into(),
    ResourceDemand::of::<Whisper>().resource(),
  )));
  let rendered = undeclared.to_string();
  assert!(rendered.contains("keyframes"), "{rendered}");
  assert!(rendered.contains("Whisper"), "{rendered}");
  assert!(rendered.contains("#[ingraph::resource]"), "{rendered}");

  let leaf = Refusal::PureLeaf(PureLeafError::new(NodeId::new(2), "captions".into()));
  let rendered = leaf.to_string();
  assert!(rendered.contains("captions"), "{rendered}");
  for way in ["a consumer", "a store", "a poll upstream", "a query arm"] {
    assert!(rendered.contains(way), "{way} is a way out: {rendered}");
  }

  let nowhere = Refusal::LandsNowhere(LandsNowhereError::new(NodeId::new(2), "captions".into()));
  let rendered = nowhere.to_string();
  assert!(rendered.contains("captions"), "{rendered}");
  // Both postures the effect is reached by, so the fix names the one this node is missing.
  assert!(rendered.contains("a lane of its own"), "{rendered}");
  assert!(
    rendered.contains("a consumer that lands what it emits"),
    "{rendered}"
  );
}

#[test]
fn the_leaf_and_the_landing_never_say_each_others_sentence() {
  // The two are one question from two sides — nothing keeps the result, versus a kept result
  // nothing keeps — and a reader told the wrong one goes looking for the wrong fix.
  let leaf = Refusal::PureLeaf(PureLeafError::new(NodeId::new(2), "captions".into())).to_string();
  let nowhere =
    Refusal::LandsNowhere(LandsNowhereError::new(NodeId::new(2), "captions".into())).to_string();
  assert!(leaf.contains("lands nothing"), "{leaf}");
  assert!(!leaf.contains("declares that it lands"), "{leaf}");
  assert!(nowhere.contains("declares that it lands"), "{nowhere}");
  assert!(!nowhere.contains("polls nothing"), "{nowhere}");
}

#[test]
fn the_two_starved_edges_never_share_an_escape() {
  // ONE fact, two sentences, and the sentences differ because the escape does: a channel listener
  // has the upstream's store to read instead, and a query edge has nothing at all.
  let channel = Refusal::DisabledUpstream(starved()).to_string();
  let query = Refusal::DisabledQueryUpstream(starved()).to_string();

  assert!(
    channel.contains("listen to its changes instead"),
    "{channel}"
  );
  assert!(!channel.contains("no store to fall back on"), "{channel}");
  assert!(query.contains("no store to fall back on"), "{query}");
  assert!(!query.contains("listen to its changes"), "{query}");
  for rendered in [&channel, &query] {
    assert!(rendered.contains("captions"), "{rendered}");
    assert!(rendered.contains("keyframes"), "{rendered}");
  }
}

#[test]
fn a_starved_edge_names_the_one_darkness_there_is() {
  // Presence is the only axis, so the sentence has one thing to say and no branch to pick it: the
  // upstream is dark because nobody wrote it a section, and the fix is to write one.
  let dark = Refusal::DisabledUpstream(starved()).to_string();
  assert!(dark.contains("no section names it"), "{dark}");
  assert!(!dark.contains("switched off"), "{dark}");
}

#[test]
fn a_row_is_reached_by_its_own_name() {
  // What a test — and a caller matching on the answer — reads a collected row through.
  let leaf = Refusal::PureLeaf(PureLeafError::new(NodeId::new(2), "captions".into()));
  assert!(leaf.is_pure_leaf());
  assert!(!leaf.is_unknown_node());
  assert_eq!(
    leaf
      .try_unwrap_pure_leaf_ref()
      .expect("the pure-leaf arm")
      .name(),
    "captions"
  );
  assert!(leaf.try_unwrap_unknown_node_ref().is_err());
}