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::{FanError, LaneFan};

use crate::{FanAll, Lane, LaneIdent, LaneRouter, RoutedProbe, ScriptRouter};

/// A corpus with a choice to make.
static FANNED: &[Lane] = &[
  Lane::of(LaneIdent::new("en")).accepting(&[LaneIdent::new("latn")]),
  Lane::of(LaneIdent::new("ja")).accepting(&[
    LaneIdent::new("hani"),
    LaneIdent::new("hira"),
    LaneIdent::new("kana"),
  ]),
];

/// A corpus with nothing to choose between.
static SOLE: &[Lane] = &[Lane::of(LaneIdent::new("en")).accepting(&[LaneIdent::new("latn")])];

/// A router that names a lane nobody declared — the drift guardrail ② exists for.
struct Astray;

impl LaneRouter for Astray {
  type Question = str;
  type Routed = String;
  type Error = Infallible;

  fn route(&self, question: &str) -> Result<Vec<RoutedProbe<String>>, Infallible> {
    Ok(vec![RoutedProbe::new(
      LaneIdent::new("sw"),
      question.to_owned(),
    )])
  }
}

/// A router that refuses in its own words.
struct Refuses;

/// What [`Refuses`] refuses with.
#[derive(Debug, PartialEq, Eq)]
struct NotToday;

impl core::fmt::Display for NotToday {
  fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    f.write_str("not today")
  }
}

impl core::error::Error for NotToday {}

impl LaneRouter for Refuses {
  type Question = str;
  type Routed = String;
  type Error = NotToday;

  fn route(&self, _question: &str) -> Result<Vec<RoutedProbe<String>>, NotToday> {
    Err(NotToday)
  }
}

/// A router that routes nothing at all.
struct Silent;

impl LaneRouter for Silent {
  type Question = str;
  type Routed = String;
  type Error = Infallible;

  fn route(&self, _question: &str) -> Result<Vec<RoutedProbe<String>>, Infallible> {
    Ok(Vec::new())
  }
}

/// The mount, and the house pair that works it.
#[test]
fn a_router_mounts_and_unmounts() {
  let bare = LaneFan::<ScriptRouter>::new(FANNED);
  assert!(bare.router().is_none());
  assert_eq!(bare.lanes(), FANNED);

  let mounted = bare.with_router(ScriptRouter::new(FANNED));
  assert!(mounted.router().is_some());

  let mut held = mounted;
  held.set_router(None);
  assert!(held.router().is_none());
}

/// **GUARDRAIL ①**: several lanes and nothing to choose between them is refused at boot, and the
/// refusal names both roads out of it.
#[test]
fn a_fanned_corpus_with_no_router_is_refused_at_boot() {
  let refusal = LaneFan::<ScriptRouter>::new(FANNED)
    .check()
    .expect_err("two lanes, no router");
  let sentence = refusal.to_string();

  assert_eq!(refusal.lanes(), FANNED);
  assert!(sentence.contains("2 lanes"), "{sentence}");
  assert!(sentence.contains("`en`"), "{sentence}");
  assert!(sentence.contains("`ja`"), "{sentence}");
  // BOTH ESCAPES are named, the recommended one first.
  assert!(sentence.contains("ScriptWeightedFan"), "{sentence}");
  assert!(sentence.contains("FanAll"), "{sentence}");
}

/// A corpus with ONE lane has no choice to make, so it is not refused for the want of a router.
#[test]
fn a_corpus_with_nothing_to_choose_passes_with_nothing_mounted() {
  assert!(LaneFan::<ScriptRouter>::new(SOLE).check().is_ok());

  static NONE: &[Lane] = &[];
  assert!(LaneFan::<ScriptRouter>::new(NONE).check().is_ok());
}

/// Mounting either shipped router satisfies the guardrail.
#[test]
fn a_mounted_router_satisfies_the_boot_check() {
  assert!(
    LaneFan::new(FANNED)
      .with_router(ScriptRouter::new(FANNED))
      .check()
      .is_ok()
  );
  assert!(
    LaneFan::new(FANNED)
      .with_router(FanAll::<str>::new(FANNED))
      .check()
      .is_ok()
  );
}

/// **GUARDRAIL ②**: a probe naming a lane this corpus does not hold is refused, with the roster.
#[test]
fn a_probe_naming_an_undeclared_lane_is_refused() {
  let fan = LaneFan::new(FANNED).with_router(Astray);
  let refusal = fan.route("hello").expect_err("`sw` is not a lane here");

  match &refusal {
    FanError::Unroutable(unroutable) => {
      assert_eq!(unroutable.lane(), "sw");
      assert_eq!(unroutable.lanes(), FANNED);
      let sentence = unroutable.to_string();
      assert!(sentence.contains("`sw`"), "{sentence}");
      assert!(sentence.contains("`en`"), "{sentence}");
    }
    other => panic!("expected the unroutable arm, got {other}"),
  }
  assert!(refusal.to_string().contains("does not hold"));
}

/// **GUARDRAIL ③**: an empty split is the empty set — a result, not a refusal.
#[test]
fn an_empty_split_is_the_empty_set() {
  let fan = LaneFan::new(FANNED).with_router(Silent);
  assert!(fan.route("anything").expect("empty is legal").is_empty());

  // And through the shipped router, where an empty question produces no runs at all.
  let shipped = LaneFan::new(FANNED).with_router(ScriptRouter::new(FANNED));
  assert!(shipped.route("").expect("no runs").is_empty());
}

/// The router's own refusal is carried WHOLE, so a caller reaches its type without a downcast.
#[test]
fn a_routers_own_refusal_crosses_untouched() {
  let fan = LaneFan::new(FANNED).with_router(Refuses);

  match fan.route("hello").expect_err("this router refuses") {
    FanError::Router(NotToday) => {}
    other => panic!("expected the router arm, got {other}"),
  }
}

/// Asking a fan with nothing mounted is the boot refusal reaching the question it would otherwise
/// have answered wrongly.
#[test]
fn routing_with_nothing_mounted_refuses() {
  let fan = LaneFan::<ScriptRouter>::new(FANNED);

  match fan.route("hello").expect_err("nothing is mounted") {
    FanError::Missing(missing) => assert_eq!(missing.lanes(), FANNED),
    other => panic!("expected the missing arm, got {other}"),
  }
}

/// A guarded route answers exactly what the router said, when the router stayed inside the roster.
#[test]
fn a_guarded_route_passes_the_routers_answer_through() {
  let fan = LaneFan::new(FANNED).with_router(ScriptRouter::new(FANNED));
  let dispatches = fan.route("日本語 report").expect("both scripts land");

  assert_eq!(dispatches.len(), 2);
  assert_eq!(dispatches[0].lane(), LaneIdent::new("ja"));
  assert_eq!(dispatches[1].lane(), LaneIdent::new("en"));
}

/// The sum is a real error, and every arm keeps its own source.
#[test]
fn the_refusal_sum_carries_each_cause() {
  let missing: FanError<NotToday> = FanError::Missing(super::RouterMissing::new(FANNED));
  let router: FanError<NotToday> = FanError::Router(NotToday);

  assert!(core::error::Error::source(&missing).is_some());
  assert!(core::error::Error::source(&router).is_some());
  assert!(router.to_string().contains("not today"));
}