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 INLINE ENCODING, against RFC 4648's own vectors.
//!
//! An artifact's `data` field hands a client a string and never a byte array, so what the string
//! spells is a contract with every decoder on the other side. The suite is here rather than in an
//! integration test because the encoder is private: an e2e read proves that bytes SURVIVE the round
//! trip, and only these prove that what travels is the encoding the schema promises — a decoder
//! written against §4 is the party that would find out otherwise.

use super::base64;

/// §10's test vectors, verbatim. They are the whole of the specification a client needs.
#[test]
fn the_rfc_4648_vectors_are_what_it_writes() {
  assert_eq!(base64(b""), "");
  assert_eq!(base64(b"f"), "Zg==");
  assert_eq!(base64(b"fo"), "Zm8=");
  assert_eq!(base64(b"foo"), "Zm9v");
  assert_eq!(base64(b"foob"), "Zm9vYg==");
  assert_eq!(base64(b"fooba"), "Zm9vYmE=");
  assert_eq!(base64(b"foobar"), "Zm9vYmFy");
}

/// **§4 AND NOT §5**, at the two symbols that are the whole difference between the alphabets.
///
/// The cursor's encoder writes `-` and `_` here, and it is right to: a cursor is a URL parameter. An
/// inline object is a JSON string value, and what a client's decoder takes untold is the standard
/// alphabet. Same crate, two encodings, and this is what keeps them from drifting into one.
#[test]
fn the_two_symbols_are_the_standard_alphabets() {
  assert_eq!(base64(&[0xff, 0xff, 0xfe]), "///+");
  assert!(!base64(&[0xff, 0xff, 0xfe]).contains(['-', '_']));
}

/// **PADDED TO A MULTIPLE OF FOUR**, which is the other half of §4: a decoder that requires padding
/// is conformant, so a renderer that omits it is not.
#[test]
fn every_output_is_a_whole_number_of_groups() {
  for len in 0..32_usize {
    let bytes = vec![0xa5; len];
    let encoded = base64(&bytes);
    assert_eq!(encoded.len() % 4, 0, "{len}: {encoded}");
    assert_eq!(encoded.len(), len.div_ceil(3) * 4, "{len}: {encoded}");
  }
}

/// **EVERY BYTE VALUE SURVIVES**, and the round trip is closed against a decoder written here rather
/// than against the encoder's own table — an encoder checked by itself agrees with itself.
#[test]
fn every_byte_round_trips() {
  const ALPHABET: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

  let bytes: Vec<u8> = (0..=255).collect();
  let encoded = base64(&bytes);

  let mut decoded = Vec::with_capacity(bytes.len());
  for group in encoded.as_bytes().chunks(4) {
    let symbols: Vec<u8> = group
      .iter()
      .take_while(|symbol| **symbol != b'=')
      .map(|symbol| {
        ALPHABET
          .iter()
          .position(|candidate| candidate == symbol)
          .expect("every symbol is in the alphabet") as u8
      })
      .collect();
    let mut packed = 0u32;
    for (i, six) in symbols.iter().enumerate() {
      packed |= u32::from(*six) << (18 - i * 6);
    }
    for i in 0..symbols.len() - 1 {
      decoded.push(((packed >> (16 - i * 8)) & 0xff) as u8);
    }
  }

  assert_eq!(decoded, bytes);
}