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 enum kind's four operators, and the storage form its crossing converts into.

use bson::doc;

use super::MongoEnumComparison;
use crate::{
  At, ComplexComparison, EnumFilter, FieldName, Mongo, RecordAddress, ToDatabaseComparison,
  comparison::mongo::MongoConjunction,
};

const ROW: RecordAddress = RecordAddress::new(FieldName::new("media"), &[FieldName::new("id")]);

/// A variant, and the text this schema stores it as — the shape an enum column actually has, where
/// the storage form is something the enum's own code writes.
#[derive(Debug, Clone, Copy, PartialEq)]
enum Codec {
  H264,
  Prores,
}

impl Codec {
  const fn stored(self) -> &'static str {
    match self {
      Self::H264 => "h264",
      Self::Prores => "prores",
    }
  }
}

/// Equality and membership, merged under one key — and the operands are the STORED text rather than
/// the variant, which is what the declared converter is for.
#[test]
fn a_declared_converter_stores_the_variants_own_form() {
  let comparison = MongoEnumComparison::<String>::try_from_filter_with(
    EnumFilter::new()
      .with_eq(Codec::H264)
      .with_not_in(vec![Codec::Prores]),
    |variant: Codec| Ok::<_, core::convert::Infallible>(variant.stored().to_owned()),
  )
  .expect("the converter above cannot fail");

  assert_eq!(comparison.conjuncts(), 2);

  let frame = At::new(&ROW).frame(FieldName::new("codec"));
  let filter = comparison
    .apply(frame.at(), MongoConjunction::new())
    .expect("a MongoDB lowering cannot refuse")
    .into_document();

  assert_eq!(
    filter,
    doc! { "codec": { "$eq": "h264", "$nin": ["prores"] } }
  );
}

/// The projection an expansion writes for an enum field, resolved.
#[test]
fn the_codegen_projection_resolves_for_an_enum_field() {
  type Crossed = <EnumFilter<Codec> as ToDatabaseComparison<String, Mongo>>::Comparison;

  let comparison: Crossed = MongoEnumComparison::<String>::new();
  assert!(comparison.is_empty());
}