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
//! What a label is, what it refuses, and the one thing it deliberately does not do.

use smol_bytes::Utf8Bytes;

use super::{EmptyLabel, Label};

/// The rule, at the constructor: a word gets in.
#[test]
fn a_word_is_a_label() {
  assert_eq!(Label::new("cat").unwrap().as_str(), "cat");
  assert_eq!(
    Label::new(String::from("golden retriever"))
      .unwrap()
      .as_str(),
    "golden retriever"
  );
  assert_eq!(
    Label::new(Utf8Bytes::from_static("person"))
      .unwrap()
      .as_str(),
    "person",
    "an owned carrier is handed over rather than copied"
  );
}

/// **NOTHING is not a word**, and neither is whitespace pretending to be one.
///
/// The second clause is the whole of what the trim decides. `"   "` names nothing, matches no query
/// and shows a reader nothing, so admitting it would put an invisible row in a table.
#[test]
fn nothing_is_refused_however_it_is_spelled() {
  assert_eq!(Label::new(""), Err(EmptyLabel));
  assert_eq!(Label::new(" "), Err(EmptyLabel));
  assert_eq!(Label::new("\t\n  \r"), Err(EmptyLabel));
  assert_eq!(
    Label::new("\u{3000}"),
    Err(EmptyLabel),
    "an ideographic space is whitespace, and `str::trim` knows it"
  );
}

/// **THE TRIM IS A PREDICATE AND NEVER A TRANSFORM** — the division with the engine, checked where
/// it would have been easiest to bend.
///
/// A constructor that rewrote its input would also be a decode-time canonicaliser, because every
/// door here re-enters `new`. This one rewrites nothing, so a value this crate wrote reads back byte
/// for byte.
#[test]
fn the_word_is_stored_verbatim() {
  assert_eq!(Label::new("  cat  ").unwrap().as_str(), "  cat  ");
  assert_eq!(Label::new("a\u{200b}b").unwrap().as_str(), "a\u{200b}b");
}

/// **CASE IS THE ENGINE'S**, so two spellings are two labels.
///
/// The producer this framework has met lowercases image labels and passes animal labels verbatim
/// from the same run, deliberately. A type that folded case would be overruling it about a
/// vocabulary it owns.
#[test]
fn case_is_not_folded() {
  let upper = Label::new("Golden Retriever").unwrap();
  let lower = Label::new("golden retriever").unwrap();

  assert_ne!(upper, lower);
  assert_eq!(upper.as_str(), "Golden Retriever");
}

/// The renderings: one bare, one quoted under the type's name — which is what makes a
/// whitespace-carrying label visible in a failure instead of vanishing into the alignment.
#[test]
fn it_renders_bare_and_debugs_quoted() {
  let label = Label::new(" cat ").unwrap();

  assert_eq!(label.to_string(), " cat ");
  assert_eq!(format!("{label:?}"), r#"Label(" cat ")"#);
}

/// The order is the text's, bytewise — which is what the lexical filter slots and the cursor both
/// walk.
#[test]
fn the_order_is_the_words() {
  let mut words = [
    Label::new("dog").unwrap(),
    Label::new("cat").unwrap(),
    Label::new("bird").unwrap(),
  ];
  words.sort();

  assert_eq!(
    words.map(|word| String::from(word.as_str())),
    ["bird", "cat", "dog"]
  );
}

/// **THE KEYSET POSITION round-trips, and a forged one is refused** — because a cursor is a string a
/// CLIENT hands back.
#[test]
fn the_cursor_round_trips_and_re_validates() {
  use crate::CursorValue;

  let label = Label::new("traffic light").unwrap();
  let mut written = Vec::new();
  label.write_cursor(&mut written);

  assert_eq!(written, b"traffic light");
  assert_eq!(Label::read_cursor(&written), Some(label));

  assert_eq!(Label::read_cursor(b""), None);
  assert_eq!(Label::read_cursor(b"  \t "), None);
  assert_eq!(
    Label::read_cursor(&[0xff, 0xfe]),
    None,
    "the bytes are a client's, so invalid UTF-8 is refused rather than assumed"
  );
}

/// The parse door and the two conversions out, which is what a comparison's `TryFrom` reaches
/// through.
#[test]
fn it_parses_and_converts_out() {
  assert_eq!("cat".parse::<Label>().unwrap(), Label::new("cat").unwrap());
  assert!("".parse::<Label>().is_err());

  let label = Label::new("cat").unwrap();
  assert_eq!(String::from(label.clone()), "cat");
  assert_eq!(Utf8Bytes::from(label).as_str(), "cat");
}

/// One sentence, said the same way wherever a label is refused.
#[test]
fn the_refusal_says_what_a_label_is() {
  assert_eq!(
    EmptyLabel.to_string(),
    "a label is a word: it holds at least one character that is not whitespace"
  );
}

/// **The TEXT reading, pinned**: a label takes the twenty-slot kind rather than the eight-slot one,
/// which is what puts `contains` and the three `nocase` families on a label column.
#[test]
fn a_label_column_is_guarded_by_the_text_kind() {
  use core::marker::PhantomData as P;

  let _: P<crate::TextFilter<Label, super::LabelWire>> =
    P::<<Label as crate::StringFilterMarker>::Filter>;
  let _: P<crate::StringMarker<Label>> = P::<<Label as crate::DefaultMarker>::Marker>;
}