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::{SubjectPath, ToSubjectPath};
use crate::TermName;

// The wire half needs a decoder and a built schema to be visible at all, so it is tested through
// one rather than beside the subject rows.
#[cfg(feature = "async-graphql")]
mod decode;

/// Stands in for one emitted `<T>OrderBy`, with the subject shapes the rule has to tell apart:
/// two ROOT subjects, two sharing a first segment, and the key.
///
/// `title` carries the case a rendered name would break. A declaration may write
/// `postgres(rename = "name")` on it, which is `Name`'s own column — so a subject computed
/// from the name a database renders would answer `name` for both variants, and an order naming both
/// would be refused as a repeat. The identity here is the RECORD FIELD's name, which no `persist`
/// block moves, and the two stay two.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "async-graphql", derive(async_graphql::OneofObject))]
pub(super) enum MediaOrderBy {
  Title(crate::OrderDirection),
  Name(crate::OrderDirection),
  CoverTitle(crate::OrderDirection),
  CoverDuration(crate::OrderDirection),
  Id(crate::OrderDirection),
}

impl ToSubjectPath for MediaOrderBy {
  fn subject_path(&self) -> SubjectPath {
    match self {
      Self::Title(_) => {
        const SUBJECT: SubjectPath = SubjectPath::new(&[TermName::new("title")]);
        SUBJECT
      }
      Self::Name(_) => {
        const SUBJECT: SubjectPath = SubjectPath::new(&[TermName::new("name")]);
        SUBJECT
      }
      Self::CoverTitle(_) => {
        const SUBJECT: SubjectPath =
          SubjectPath::new(&[TermName::new("cover"), TermName::new("title")]);
        SUBJECT
      }
      Self::CoverDuration(_) => {
        const SUBJECT: SubjectPath =
          SubjectPath::new(&[TermName::new("cover"), TermName::new("duration")]);
        SUBJECT
      }
      Self::Id(_) => {
        const SUBJECT: SubjectPath = SubjectPath::new(&[TermName::new("id")]);
        SUBJECT
      }
    }
  }
}

/// Every variant, and the subject it names. A subject is its SEGMENTS, and the dotted rendering is
/// prose over them — which is what lets a refusal name `cover.title` without that string being the
/// identity anything is compared by.
///
/// The whole enum is walked rather than a sample, because the fixture's point is that these five
/// subjects are FIVE: a rule collapsing any pair of them would still pass a test that named two.
#[test]
fn every_variant_names_its_own_subject_and_renders_it_dotted() {
  use crate::OrderDirection::{Asc, Desc};

  let rendered: Vec<String> = [
    MediaOrderBy::Title(Asc),
    MediaOrderBy::Name(Asc),
    MediaOrderBy::CoverTitle(Asc),
    MediaOrderBy::CoverDuration(Asc),
    MediaOrderBy::Id(Asc),
  ]
  .into_iter()
  .map(|term| term.subject_path().to_string())
  .collect();
  assert_eq!(
    rendered,
    ["title", "name", "cover.title", "cover.duration", "id"],
    "five variants, five subjects — a nested one renders its segments dotted"
  );

  // The DIRECTION is no part of the subject: one column ordered two ways is one subject named
  // twice, which is exactly what the decode refuses.
  assert_eq!(
    MediaOrderBy::Title(Asc).subject_path(),
    MediaOrderBy::Title(Desc).subject_path()
  );

  // And a shared segment is not a shared subject, at either end of the path.
  assert_ne!(
    MediaOrderBy::CoverTitle(Asc).subject_path(),
    MediaOrderBy::CoverDuration(Asc).subject_path()
  );
  assert_ne!(
    MediaOrderBy::CoverTitle(Asc).subject_path(),
    MediaOrderBy::Title(Asc).subject_path()
  );
}