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::*;
use crate::NormCoord;

fn code(payload: &str, symbology: Symbology) -> BarcodeDetection<NormCoord> {
  BarcodeDetection::new(
    payload,
    symbology,
    Score::new(0.98).expect("the fixture's confidence is in range"),
    BoundingBox::try_unit(0.05, 0.60, 0.90, 0.20).expect("the fixture's rectangle is in frame"),
  )
}

/// **EIGHT COLUMNS, SEVEN OF THEM THE VALUE**, and the names in segment order.
#[test]
fn a_decoded_code_states_its_columns_and_which_of_them_hold_the_value() {
  type Code = BarcodeDetection<NormCoord>;

  assert_eq!(crate::width(<Code as ColumnKind>::SEGMENTS), 8);
  assert_eq!(crate::value_width(<Code as ColumnKind>::SEGMENTS), 7);

  let names: Vec<_> = <Code as ColumnKind>::SEGMENTS
    .iter()
    .map(|segment| segment.field().expect("named").declared().as_str())
    .collect();
  assert_eq!(names, ["payload", "symbology", "confidence", "bbox"]);
}

/// **SEVEN LEVELS OVER EIGHT LEAVES**, and the eighth answering `None`.
#[test]
fn a_codes_order_names_its_seven_value_leaves_and_never_the_boxs_index() {
  let levels: Vec<_> = <BarcodeDetection<NormCoord> as ColumnKind>::ORDER
    .iter()
    .map(|level| level.leaf())
    .collect();
  assert_eq!(levels, [0, 1, 2, 3, 4, 5, 6]);

  let ticket = code("M1DOE/JOHN", Symbology::Pdf417);
  assert_eq!(
    crate::OrderLeaves::leaf(&ticket, 1),
    Some(BarcodeDetectionLeaf::Symbology(Symbology::Pdf417))
  );
  assert!(matches!(
    crate::OrderLeaves::leaf(&ticket, 5),
    Some(BarcodeDetectionLeaf::Bbox(BoundingBoxLeaf::Width(_)))
  ));
  assert_eq!(crate::OrderLeaves::leaf(&ticket, 7), None);
}

/// **THE CURSOR ROUND TRIP**, with TWO variable-width members — which is what the member framing
/// exists for and what a division by constants could not have carried.
#[test]
fn a_code_round_trips_through_a_cursor_and_refuses_a_forged_one() {
  use crate::CursorValue as _;

  for value in [
    code("https://example.invalid/a", Symbology::Qr),
    code("", Symbology::new("han xin")),
  ] {
    let mut bytes = Vec::new();
    value.write_cursor(&mut bytes);
    assert_eq!(
      <BarcodeDetection<NormCoord> as crate::CursorValue>::read_cursor(&bytes),
      Some(value)
    );
  }

  let mut bytes = Vec::new();
  code("x", Symbology::Qr).write_cursor(&mut bytes);
  assert_eq!(
    <BarcodeDetection<NormCoord> as crate::CursorValue>::read_cursor(&bytes[..bytes.len() - 1]),
    None
  );
}

/// EMPTINESS is the conjunction of the four facets', with the two text-shaped ones reached through
/// two different kinds.
#[test]
fn a_filter_is_empty_until_one_of_its_four_facets_is_set() {
  type Face = BarcodeDetectionFilter<crate::ScalarFilter<NormCoord>>;

  assert!(crate::GraphqlFilter::is_empty(&Face::new()));
  assert!(crate::GraphqlFilter::is_empty(
    &Face::new().with_symbology(EnumFilter::new())
  ));
  assert!(!crate::GraphqlFilter::is_empty(
    &Face::new().with_symbology(EnumFilter::new().with_eq(Symbology::Qr))
  ));
}