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 via's two functions, read as a serde field would reach them.
//!
//! The probe is a struct carrying the attribute an emitted document carries, so what is under test
//! is the road serde actually takes rather than a direct call: `#[serde(with = "…")]` is spent by
//! serde's own expansion, and a signature it cannot instantiate is a compile error here.

use bson::{Bson, doc};
use serde::{Deserialize, Serialize};

/// A document field of a type that HAS serde rows, so the claim is about the road rather than about
/// a type with nowhere else to go.
#[derive(Debug, Serialize, Deserialize)]
struct Counted {
  #[serde(with = "super")]
  hits: i64,
}

/// The shape an application declares — a pattern that may be absent, which is the field type the
/// citizen has to land as.
#[cfg(feature = "regex")]
#[derive(Debug, Serialize, Deserialize)]
struct Watch {
  #[serde(with = "super")]
  root: String,
  #[serde(with = "super")]
  include: Option<regex::Regex>,
}

#[test]
fn a_field_with_a_serde_row_writes_the_bson_its_carrier_writes() {
  let document = bson::serialize_to_document(&Counted { hits: 7 }).expect("the row writes");
  assert_eq!(document, doc! { "hits": 7i64 });

  let read: Counted = bson::deserialize_from_document(document).expect("the row reads");
  assert_eq!(read.hits, 7);
}

/// **THE PATTERN CROSSES SERDE WITHOUT A SERDE ROW**, which is the whole of what this seat buys:
/// `regex::Regex` implements neither `Serialize` nor `Deserialize`, and both directions answer.
#[cfg(feature = "regex")]
#[test]
fn a_pattern_field_round_trips_as_its_text_with_no_serde_row_anywhere() {
  let watch = Watch {
    root: "/media".to_owned(),
    include: Some(regex::Regex::new(r"\.mp4$").expect("a pattern")),
  };

  let document = bson::serialize_to_document(&watch).expect("the row writes");
  assert_eq!(document, doc! { "root": "/media", "include": r"\.mp4$" });

  let read: Watch = bson::deserialize_from_document(document).expect("the row reads");
  assert_eq!(read.root, "/media");
  assert_eq!(
    read.include.as_ref().map(regex::Regex::as_str),
    Some(r"\.mp4$")
  );
}

/// An ABSENT pattern is a `null`, on the two-state reading `MongoExtract` states — and it comes back
/// absent rather than as a pattern matching nothing.
#[cfg(feature = "regex")]
#[test]
fn an_absent_pattern_is_a_null_in_both_directions() {
  let watch = Watch {
    root: "/media".to_owned(),
    include: None,
  };

  let document = bson::serialize_to_document(&watch).expect("the row writes");
  assert_eq!(document.get("include"), Some(&Bson::Null));

  let read: Watch = bson::deserialize_from_document(document).expect("the row reads");
  assert!(read.include.is_none());
}

/// **A STORED PATTERN THAT NO LONGER COMPILES IS A NAMED DECODE REFUSAL, NEVER A PANIC.**
///
/// The text is legal BSON and legal UTF-8 and is not a pattern, which is the document an older
/// build, another writer or a hand can leave behind. What comes back names the situation — the
/// sentence `MongoExtract`'s row refused with — rather than unwinding inside a deserializer.
#[cfg(feature = "regex")]
#[test]
fn a_stored_pattern_that_no_longer_compiles_is_refused_by_name() {
  let forged = doc! { "root": "/media", "include": "a(b" };

  let refused =
    bson::deserialize_from_document::<Watch>(forged).expect_err("the text is not a pattern");

  assert!(
    refused.to_string().contains("no longer a pattern"),
    "the refusal names the situation: {refused}"
  );
}

/// The wrong BSON TYPE under a pattern's key is the extraction's own mismatch, with both aliases —
/// so a document written against a different schema reports as one rather than as a compile failure.
#[cfg(feature = "regex")]
#[test]
fn a_pattern_key_holding_another_bson_type_names_both_aliases() {
  let forged = doc! { "root": "/media", "include": 7i64 };

  let refused =
    bson::deserialize_from_document::<Watch>(forged).expect_err("a long is not a pattern");
  let said = refused.to_string();

  assert!(said.contains("long"), "it names what was found: {said}");
  assert!(said.contains("string"), "it names what was stored: {said}");
}