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::{Id, TypedId};

// The bound a backend writes over the vocabulary. Written once here so every row below is the same
// question asked of a different member.
fn admits<I: Id>() {}

/// Every member of the vocabulary, asserted by resolving the bound. The list IS the test — a member
/// dropped from `id.rs` reds here rather than at the first declaration that spells it.
///
/// Written out rather than driven from `for_each_id_member!`, and the independence is the whole
/// point: a consumer of that list SHRINKS with it and stays green, which is exactly the loss this
/// test exists to catch. The backend's completeness audit is the opposite case and must read the
/// same list, because what it asks is whether every CURRENT member renders.
#[test]
fn the_vocabulary_admits_its_members() {
  admits::<i8>();
  admits::<i16>();
  admits::<i32>();
  admits::<i64>();
  admits::<i128>();
  admits::<u8>();
  admits::<u16>();
  admits::<u32>();
  admits::<u64>();
  admits::<u128>();

  admits::<core::num::NonZeroI8>();
  admits::<core::num::NonZeroI16>();
  admits::<core::num::NonZeroI32>();
  admits::<core::num::NonZeroI64>();
  admits::<core::num::NonZeroI128>();
  admits::<core::num::NonZeroU8>();
  admits::<core::num::NonZeroU16>();
  admits::<core::num::NonZeroU32>();
  admits::<core::num::NonZeroU64>();
  admits::<core::num::NonZeroU128>();

  admits::<uuid::Uuid>();
  admits::<String>();
  admits::<smol_bytes::Utf8Bytes>();

  // The network family — all six of std's, admitted on measured orders.
  admits::<core::net::IpAddr>();
  admits::<core::net::Ipv4Addr>();
  admits::<core::net::Ipv6Addr>();
  admits::<core::net::SocketAddr>();
  admits::<core::net::SocketAddrV4>();
  admits::<core::net::SocketAddrV6>();

  // Fixed width at several widths, since the impl is const-generic and one arity would not show it.
  admits::<[u8; 1]>();
  admits::<[u8; 12]>();
  admits::<[u8; 16]>();
  admits::<[u8; 32]>();
}

#[cfg(feature = "smol_str")]
#[test]
fn the_text_member_under_its_feature_is_admitted() {
  admits::<smol_str::SmolStr>();
}

/// The BSON pair, which is the whole of what the `mongo` gate adds to the vocabulary.
#[cfg(feature = "mongo")]
#[test]
fn the_bson_members_are_admitted() {
  admits::<bson::Uuid>();
  admits::<bson::oid::ObjectId>();
}

/// A pointer over a member is a member, at every pointer kind and over every shape of pointee — the
/// delegation is by the POINTEE's membership, so it composes rather than being enumerated.
#[test]
fn a_pointer_over_a_member_is_a_member() {
  admits::<std::sync::Arc<u64>>();
  admits::<std::rc::Rc<u64>>();
  admits::<Box<u64>>();

  admits::<std::sync::Arc<uuid::Uuid>>();
  admits::<std::sync::Arc<String>>();
  admits::<std::sync::Arc<[u8; 16]>>();
  admits::<std::sync::Arc<core::net::IpAddr>>();

  // And it nests, since the inner pointer is itself a member.
  admits::<std::sync::Arc<Box<u64>>>();
}

/// The wrapper is a member wherever the entity NAMES IT BACK, over every shape of `I` the delegation
/// has to compose with — including a pointer, which is itself a member by the same law.
#[test]
fn a_typed_id_the_entity_names_back_is_a_member() {
  use crate::Identifiable;

  struct Scene {
    id: TypedId<uuid::Uuid, Scene>,
  }

  struct Slug {
    id: TypedId<String, Slug>,
  }

  struct Held {
    id: TypedId<std::sync::Arc<u64>, Held>,
  }

  impl Identifiable for Scene {
    type Id = TypedId<uuid::Uuid, Scene>;

    fn id_ref(&self) -> &Self::Id {
      &self.id
    }
  }

  impl Identifiable for Slug {
    type Id = TypedId<String, Slug>;

    fn id_ref(&self) -> &Self::Id {
      &self.id
    }
  }

  impl Identifiable for Held {
    type Id = TypedId<std::sync::Arc<u64>, Held>;

    fn id_ref(&self) -> &Self::Id {
      &self.id
    }
  }

  admits::<TypedId<uuid::Uuid, Scene>>();
  admits::<TypedId<String, Slug>>();
  admits::<TypedId<std::sync::Arc<u64>, Held>>();
}

// The EXCLUSIONS are pinned where a refusal is observable — the trait's own `compile_fail`
// doctests, one per excluded shape (`Vec<u8>`, `usize`, and a downstream type against the seal), and
// the wrapper's two on its admission row (a non-member `I`, and an `I` the entity did not name).
// They cannot be asserted from here: a type that is simply not a member fails nothing at runtime,
// and a `#[test]` naming one would not compile.

/// The entity marker contributes NO auto-trait obligation, which is what `PhantomData<fn() -> E>`
/// buys and a bare `PhantomData<E>` does not. An `Rc` stands in the marker position because it is
/// neither [`Send`] nor [`Sync`], and the wrapper is asked for neither on its account.
#[test]
fn the_entity_marker_carries_no_auto_trait() {
  fn is_send<T: Send>() {}
  fn is_sync<T: Sync>() {}

  is_send::<TypedId<u64, std::rc::Rc<()>>>();
  is_sync::<TypedId<u64, std::rc::Rc<()>>>();
}

/// The marker implements NOTHING — no derives, no impls of any kind — and the id over it still
/// copies, compares, hashes and prints. Every one of those is written by hand bounding the stored
/// id; a derive-shaped bound smuggled onto any of them reds here.
#[test]
fn the_entity_marker_implements_nothing() {
  struct Marker;

  fn is_copy<T: Copy>() {}
  fn is_eq<T: Eq>() {}
  fn is_hash<T: core::hash::Hash>() {}
  fn is_debug<T: core::fmt::Debug>() {}

  is_copy::<TypedId<u64, Marker>>();
  is_eq::<TypedId<u64, Marker>>();
  is_hash::<TypedId<u64, Marker>>();
  is_debug::<TypedId<u64, Marker>>();
}

/// The stored id is the whole of the value: both accessors hand back what the constructor took, two
/// ids are equal exactly when their stored ids are, and the map keyed by one finds what an equal one
/// put there.
#[test]
fn the_wrapper_is_the_id_it_holds() {
  struct Scene;

  let seven = TypedId::<u64, Scene>::new(7);

  assert_eq!(seven.id(), 7);
  assert_eq!(seven.id_ref(), &7);
  assert_eq!(seven, TypedId::new(7));
  assert_ne!(seven, TypedId::new(8));
  assert_eq!(format!("{seven:?}"), "TypedId(7)");

  let mut rows = std::collections::HashMap::new();
  rows.insert(seven, "a-scene");
  assert_eq!(rows.get(&TypedId::<u64, Scene>::new(7)), Some(&"a-scene"));
}

/// The measured loss the wrapper repairs, as the thing it is: with two records addressed by the same
/// BARE member, the forward equation binds either record against either id and cannot say which is
/// meant. The wrapper's own `compile_fail` twin is the other half — same equation, refused.
#[test]
fn a_bare_id_cannot_tell_two_records_apart() {
  use crate::{Identifiable, refers_to};

  struct Take(u64);
  struct Cut(u64);

  impl Identifiable for Take {
    type Id = u64;

    fn id_ref(&self) -> &u64 {
      &self.0
    }
  }

  impl Identifiable for Cut {
    type Id = u64;

    fn id_ref(&self) -> &u64 {
      &self.0
    }
  }

  // A field holding a `Cut`'s id, checked against a `ref` naming `Take`. Both resolve.
  refers_to::<<Cut as Identifiable>::Id, Take>();
  refers_to::<<Take as Identifiable>::Id, Cut>();
}