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 codec house's wire faces, behind the `async-graphql` feature.
//!
//! Every codec type crosses as a SCALAR and as the SAME scalar in both seats — the shape a value
//! that is ONE WORD has, and the one [`primitives::lang`](crate::primitives::lang)'s subtags take.
//! There is no operand newtype beside any of them and no input object restating one, because
//! neither is publishing an object a scalar of the same name would collide with.
//!
//! ```text
//! scalar VideoCodec
//! scalar AudioCodec
//! scalar SubtitleCodec
//! ```
//!
//! # WIDE IN, STRICT OUT — and here the wide half is as wide as it goes
//!
//! A door reads a JSON string and hands it to the type's own `new`, so the spellings it admits are
//! exactly the ones that type documents. What this layer adds is the JSON shape, and for this house
//! that is the ONLY refusal there is: the vocabulary is open, so every string names a codec and the
//! door turns none of them away.
//!
//! That makes [`is_valid`](async_graphql::ScalarType::is_valid) a question about the JSON kind
//! alone, and it is written that way rather than as a `new(…).is_ok()` that could only ever answer
//! `true`. The subtag house shares its validator with its parser so a query that validates is one
//! that parses; the same property holds here for the narrower reason that there is nothing to
//! disagree about.
//!
//! **A NUMBER is refused**, which is worth a sentence because a codec name can look like one: `012v`
//! is a video codec and `Other` would hold any digits at all. A JSON number has no leading zeros, so
//! `012v`'s own spelling could not survive the round trip as one — and admitting bare digits while
//! refusing `012v` would be a door whose grammar depended on which codec a client asked about.
//!
//! # The name is SPELLED
//!
//! `#[Scalar(name = …)]` on every one of the three, as the subtag doors and `SoundEventCode` both
//! take it: a wire name is a schema commitment, and a Rust rename is not a schema change.

use async_graphql::{InputValueError, InputValueResult, Scalar, ScalarType, Value};

use super::{AudioCodec, SubtitleCodec, VideoCodec};

#[cfg(test)]
mod tests;

/// Generate one codec type's SCALAR DOOR — the JSON shape, and nothing about the codec vocabulary.
///
/// The three bodies are the same three bodies: read a string, hand it to the type's own `new`,
/// refuse everything else, and write back the canonical slug. Writing them out three times would be
/// three chances for one of them to admit a JSON shape the others do not.
///
/// **The DESCRIPTION is passed in and not generated.** async-graphql publishes the doc comment as
/// the scalar's SDL description, so it is the sentence a client reads to learn what the operand is
/// — the one part of a door that cannot be shared.
macro_rules! codec_scalar {
  ($(#[$description:meta])* $codec:ident, $name:literal) => {
    $(#[$description])*
    #[Scalar(name = $name)]
    impl ScalarType for $codec {
      #[inline]
      fn parse(value: Value) -> InputValueResult<Self> {
        match &value {
          Value::String(text) => Ok(Self::new(text)),
          _ => Err(InputValueError::expected_type(value.clone())),
        }
      }

      /// The SOLE notion of validity, and it is the JSON KIND alone.
      ///
      /// async-graphql's default answers `true` for every value, so a scalar narrowing in
      /// [`parse`](ScalarType::parse) alone ships a validator admitting what its own parser
      /// rejects. This one narrows to exactly what that parser takes — a string — which is the
      /// whole of what it refuses.
      #[inline]
      fn is_valid(value: &Value) -> bool {
        matches!(value, Value::String(_))
      }

      /// STRICT OUT: the canonical slug, whichever spelling came in.
      #[inline]
      fn to_value(&self) -> Value {
        Value::String(String::from(self.as_str()))
      }
    }
  };
}

codec_scalar! {
  /// An FFmpeg VIDEO codec name, read WIDE and written back canonical: `"H264"`, `"h264"` and
  /// `"H264"` are one operand, and it is written back as `h264`.
  ///
  /// A name this build's roster does not carry is ACCEPTED and kept losslessly — the FFmpeg table
  /// gains codecs and a generated roster is a snapshot, so a stream tagged with something newer
  /// stays indexable and stays queryable under the name it was tagged with.
  ///
  /// What is refused is a JSON value that is not a STRING. There is no spelling this door turns
  /// away.
  VideoCodec, "VideoCodec"
}

codec_scalar! {
  /// An FFmpeg AUDIO codec name, read WIDE and written back canonical: `"AAC"` and `"aac"` are one
  /// operand and come back as `aac`.
  ///
  /// A separate scalar from `VideoCodec` because it is a separate ROSTER: `h264` is not a value an
  /// audio seat should take, and one shared scalar could not say so.
  ///
  /// A name the roster does not carry is ACCEPTED, as the video door's note says. What is refused is
  /// a JSON value that is not a string.
  AudioCodec, "AudioCodec"
}

codec_scalar! {
  /// An FFmpeg SUBTITLE codec name, read WIDE and written back canonical: `"SubRip"` and `"subrip"`
  /// are one operand and come back as `subrip`.
  ///
  /// The third roster — `ass`, `subrip`, `dvb_subtitle` — and a separate scalar for the reason the
  /// audio door names.
  ///
  /// A name the roster does not carry is ACCEPTED. What is refused is a JSON value that is not a
  /// string.
  SubtitleCodec, "SubtitleCodec"
}