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::NodeSection;

/// A node's own options, as its author writes them.
#[derive(Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
struct DecodeOptions {
  threads: usize,
}

/// A written section carries what it was written with, and writing it is the whole of what runs the
/// node.
#[test]
fn a_section_states_its_options_and_runs() {
  let section = NodeSection::new(DecodeOptions { threads: 8 });
  assert_eq!(section.options(), Some(&DecodeOptions { threads: 8 }));
  assert_eq!(section.into_options(), Some(DecodeOptions { threads: 8 }));
}

/// A bare section — `[decode]` with nothing under it — runs the node at its OWN defaults, and needs
/// no `Default` on the options to say so.
#[test]
fn a_bare_section_runs_with_no_options_of_its_own() {
  /// Deliberately NOT `Default`: what fills the absence is the construction's door, so the carrier
  /// must not demand a value it cannot build.
  #[derive(Debug, PartialEq, Eq)]
  struct Mandatory {
    path: &'static str,
  }

  let bare = NodeSection::<Mandatory>::default();
  assert_eq!(bare.options(), None);
}

#[cfg(feature = "serde")]
mod deserialization {
  use super::{DecodeOptions, NodeSection};

  /// One section, as a deployment writes it — see the dev-dependency's own note for why a real
  /// format and not serde's toy map deserializer.
  fn section(document: &str) -> Result<NodeSection<DecodeOptions>, serde_json::Error> {
    serde_json::from_str(document)
  }

  /// `[decode]` alone: the node runs, at its own defaults, and no key was needed to say either.
  #[test]
  fn an_empty_section_runs_at_the_options_own_defaults() {
    let parsed = section("{}").expect("an empty section parses");
    assert_eq!(parsed.options(), None);
  }

  /// **The soft switch is GONE, and its word is now an ordinary unknown key.**
  ///
  /// Presence is the only enablement axis, so `enabled` names nothing on the shelf and falls to
  /// `deny_unknown_fields` exactly as a typo does — a refusal naming the key, at deserialization,
  /// where the reader is looking at the file. A document carrying the old word is told so rather
  /// than silently parsed into a section that runs.
  #[test]
  fn the_dead_switch_is_refused_as_an_unknown_key() {
    let refusal =
      section(r#"{ "enabled": false }"#).expect_err("`enabled` is no longer on the shelf");
    let sentence = refusal.to_string();
    assert!(sentence.contains("enabled"), "{sentence}");
    assert!(sentence.contains("unknown field"), "{sentence}");
  }

  /// The knobs live UNDER `options` rather than mixed into the section — which is what leaves room
  /// on the shelf for the framework's own per-node blocks.
  #[test]
  fn the_knobs_sit_under_their_own_key() {
    let parsed =
      section(r#"{ "options": { "threads": 8 } }"#).expect("a section with knobs parses");
    assert_eq!(parsed.options(), Some(&DecodeOptions { threads: 8 }));
  }

  /// A key the shelf does not have is a REFUSAL naming it — the typo lands at deserialization,
  /// where the reader is looking at the file.
  #[test]
  fn an_unknown_key_is_refused_by_name() {
    let refusal = section(r#"{ "enabeld": false }"#).expect_err("`enabeld` is not on the shelf");
    let sentence = refusal.to_string();
    assert!(sentence.contains("enabeld"), "{sentence}");
    assert!(sentence.contains("unknown field"), "{sentence}");
  }

  /// A required knob stays required: the section defaults the KEY, never the author's own fields.
  ///
  /// This is what the written `bound(deserialize = …)` buys. serde's inferred bound would have read
  /// the `options` default as `O: Default`, and an options type without one would have had no
  /// section at all rather than one that refuses by name.
  #[test]
  fn a_missing_knob_inside_a_present_options_block_is_refused() {
    let refusal = section(r#"{ "options": {} }"#).expect_err("`threads` has no default of its own");
    assert!(refusal.to_string().contains("threads"), "{refusal}");
  }
}