ingraph-derive 0.1.0

The attribute and derive macros for ingraph: the words a record, node, key, filter and vocabulary declaration is written in.
Documentation
//! Reporting every violation a pass can still see, rather than the first one it hits.
//!
//! Hand-rolled attribute parsing loses accumulation first: the natural shape is `?` at every step,
//! which turns a declaration with four mistakes into four compiles. [`Errors`] is the alternative —
//! a pass records refusals as it goes and returns them together, so one compile reports the whole
//! declaration.
//!
//! Part of the layer every `ingraph` derive parses on; `Record` is its first consumer.

/// Every rule a pass violated, not just the first one it hit.
#[derive(Default)]
pub(crate) struct Errors(Option<syn::Error>);

impl Errors {
  /// Record a refusal and keep going.
  pub(crate) fn push(&mut self, error: syn::Error) {
    match &mut self.0 {
      Some(accumulated) => accumulated.combine(error),
      slot => *slot = Some(error),
    }
  }

  /// Close the pass on an error that STOPS it, keeping whatever came before.
  ///
  /// For the refusals after which nothing else is checkable — a declaration with no named fields
  /// has nothing left to say about its fields.
  pub(crate) fn seal(self, terminal: syn::Error) -> syn::Error {
    match self.0 {
      Some(mut accumulated) => {
        accumulated.combine(terminal);
        accumulated
      }
      None => terminal,
    }
  }

  /// Close the pass on WHAT IS ALREADY RECORDED, for a stop whose own sentence is one of them.
  ///
  /// [`seal`](Self::seal)'s twin for the guard that stops because the declaration could not be READ
  /// to the end. The reader's own failure is already an error here, and the refusal the guard would
  /// otherwise have carried is an ABSENCE claim over tokens nobody read — so a terminal sentence
  /// beside the failure would be one mistake reported twice, the second time falsely.
  ///
  /// `nothing` is what a pass that recorded none says. No caller reaches it today, every stop of
  /// this shape following a failure that was pushed; it is the honest answer the type asks for
  /// rather than a panic at a case the callers happen to exclude.
  pub(crate) fn stopped(self, nothing: syn::Error) -> syn::Error {
    self.0.unwrap_or(nothing)
  }

  /// The model, or everything that was wrong with the declaration it came from.
  pub(crate) fn into_result<T>(self, model: T) -> syn::Result<T> {
    self.0.map_or(Ok(model), Err)
  }
}