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::{borrow::Borrow, hash::BuildHasher};
use std::collections::HashMap;

use super::{SlotName, is_identifier};
use crate::FieldName;

/// The charset at each of its edges. Every refusal below is a string that would reach a filesystem
/// or a `CREATE TABLE` unquoted, which is why the rule is a charset and not a length check: `.`
/// and `..` are unrepresentable because the dot is outside the set, so traversal is dead by
/// construction rather than by a special case.
#[test]
fn the_charset_admits_a_bare_identifier_and_nothing_else() {
  assert!(is_identifier(b"captions"));
  assert!(is_identifier(b"_captions"));
  assert!(is_identifier(b"captions_v2"));
  assert!(is_identifier(b"c9"));

  assert!(!is_identifier(b""));
  assert!(!is_identifier(b"9captions"));
  assert!(!is_identifier(b"captions v2"));
  assert!(!is_identifier(b"captions-v2"));
  assert!(!is_identifier(b"."));
  assert!(!is_identifier(b".."));
  assert!(!is_identifier(b"a/b"));
  assert!(!is_identifier(b"a\\b"));
  assert!(!is_identifier(b"a\0b"));
  assert!(!is_identifier("ключ".as_bytes()));
}

/// Constructible in a `const`, which is the only position the type exists for — and the position
/// where the assertion above is `E0080` instead of a panic (pinned by the type's doctest, since a
/// compile error is not observable from here).
#[test]
fn a_slot_name_is_const_constructible() {
  const CAPTIONS: &SlotName = SlotName::new("captions");

  assert_eq!(CAPTIONS.as_ref(), "captions");
}

/// The same refusal reached by the other door. A runtime call is not a position this crate uses,
/// so what this pins is that `new` carries the check itself rather than relying on the const
/// evaluator to apply it.
#[test]
#[should_panic = "a slot name must be non-empty ASCII"]
fn a_malformed_slot_name_is_refused() {
  let _ = SlotName::new("captions v2");
}

/// The two name types disagree about what a legal name is, and the disagreement is why they are
/// two types rather than one construction used twice: every string here is a field name, and none
/// of them is a slot name.
#[test]
fn a_field_name_admits_what_a_slot_name_refuses() {
  assert_eq!(FieldName::new("").len(), 0);
  assert_eq!(FieldName::new("a\"b").len(), 3);
  assert_eq!(FieldName::new("ключ").len(), 8);

  assert!(!is_identifier(b""));
  assert!(!is_identifier(b"a\"b"));
  assert!(!is_identifier("ключ".as_bytes()));
}

/// [`Borrow`]'s contract, which is the subtle half of the shared construction: `Eq`, `Ord` and
/// `Hash` all forward to the inner `str`, so a slot name and the `str` it borrows as are
/// interchangeable as a key.
#[test]
fn a_slot_name_and_its_borrowed_str_agree() {
  let slot = SlotName::new("captions");
  let borrowed: &str = slot.borrow();

  let indexes = HashMap::from([("captions", 0usize), ("transcripts", 1)]);
  assert_eq!(indexes.get(borrowed), Some(&0));
  assert_eq!(
    indexes.get(Borrow::<str>::borrow(SlotName::new("transcripts"))),
    Some(&1)
  );

  let hasher = indexes.hasher();
  assert_eq!(hasher.hash_one(slot), hasher.hash_one(borrowed));

  assert_eq!(slot, SlotName::new("captions"));
  assert_eq!(
    slot.cmp(SlotName::new("transcripts")),
    borrowed.cmp("transcripts")
  );
}