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 projections' own behaviour, and the property the derive depends on: that they resolve
//! through a type ALIAS, which is what pattern-matching a written type could not do.

use super::{Optional, Repeated};

type Ids = Vec<u32>;
type MaybeId = Option<u32>;

#[test]
fn a_vec_is_repeated() {
  let mut ids: Vec<u32> = vec![1, 2, 3];
  assert_eq!(Repeated::as_slice(&ids), &[1, 2, 3]);
  Repeated::as_mut_slice(&mut ids)[0] = 9;
  assert_eq!(Repeated::as_slice(&ids), &[9, 2, 3]);
}

#[test]
fn an_option_is_optional() {
  assert_eq!(Optional::as_option(&Some(7u32)), Some(&7));
  assert_eq!(Optional::as_option(&None::<u32>), None);
}

#[test]
fn an_option_projects_mutably() {
  let mut held = Some(7u32);
  *Optional::as_option_mut(&mut held).expect("a value is held") = 9;
  assert_eq!(Optional::as_option(&held), Some(&9));
  assert_eq!(Optional::as_option_mut(&mut None::<u32>), None);
}

/// The whole reason the derive goes through a trait: an alias resolves, so the item type is
/// reached from a declaration that never spells `Vec` or `Option` out.
#[test]
fn the_projections_resolve_through_a_type_alias() {
  let ids: Ids = vec![4, 5];
  let slice: &[<Ids as Repeated>::Item] = ids.as_slice();
  assert_eq!(slice, &[4, 5]);

  let maybe: MaybeId = Some(6);
  let borrowed: Option<&<MaybeId as Optional>::Item> = maybe.as_option();
  assert_eq!(borrowed, Some(&6));
}

/// A container of one's own participates: the trait is `ingraph`'s, so implementing it for a
/// foreign or local type is ordinary work and needs no escape hatch in the derive.
///
/// Both halves of each trait, because both are what its owner needs — a projection that could only
/// borrow immutably would send them reaching past the trait to mutate.
#[test]
fn a_container_of_ones_own_can_project_either_way() {
  struct Pair(u8, u8);
  struct Boxed([u8; 2]);

  impl Repeated for Boxed {
    type Item = u8;

    fn as_slice(&self) -> &[u8] {
      &self.0
    }

    fn as_mut_slice(&mut self) -> &mut [u8] {
      &mut self.0
    }
  }

  impl Optional for Pair {
    type Item = u8;

    fn as_option(&self) -> Option<&u8> {
      (self.0 == self.1).then_some(&self.0)
    }

    fn as_option_mut(&mut self) -> Option<&mut u8> {
      (self.0 == self.1).then_some(&mut self.0)
    }
  }

  let mut boxed = Boxed([1, 2]);
  assert_eq!(Repeated::as_slice(&boxed), &[1, 2]);
  Repeated::as_mut_slice(&mut boxed)[1] = 5;
  assert_eq!(Repeated::as_slice(&boxed), &[1, 5]);

  let mut pair = Pair(3, 3);
  assert_eq!(Optional::as_option(&pair), Some(&3));
  *Optional::as_option_mut(&mut pair).expect("the halves match") = 4;
  assert_eq!(
    Optional::as_option(&pair),
    None,
    "and the projection is the type's own rule, whatever it is"
  );
  assert_eq!(Optional::as_option_mut(&mut Pair(3, 4)), None);
}