grippo 0.3.0

Grok, regex, CEL and scrubbing on typed primitives. Up to 9 times faster than the stock Rust regex.
Documentation
// Project:   grippo
// File:      src/parse/trace.rs
// Purpose:   What each hop did with one record, for a caller working out why
// Language:  Rust
//
// License:   Apache-2.0
// Copyright: (c) 2026 HYPERI PTY LIMITED

use super::ParseError;

/// Where a step's input came from.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Input {
    /// The record.
    Record,
    /// The body its parent handed on.
    Body,
    /// A value its parent wrote, by the name the parent's parser gave it.
    Field(String),
}

/// A parser error, without its payload.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Failure {
    /// [`ParseError::Malformed`].
    Malformed {
        /// Byte offset into the parser's input.
        offset: usize,
        /// What is wrong there.
        reason: &'static str,
    },
    /// [`ParseError::Limit`].
    Limit {
        /// Which bound.
        what: &'static str,
    },
    /// Any other error.
    Other,
}

/// What one step came to.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Event {
    /// The parser took the input.
    Parsed,
    /// The hop's fallback parser took the input.
    FallbackParsed,
    /// The parser returned an error and its fields were dropped.
    Error(Failure),
    /// The hop had to parse its input, nothing did, and it was left raw.
    Unparsed,
    /// The hop had to parse its input, nothing did, and the record was rejected.
    Rejected,
    /// The hop reads a body and its parent handed none on.
    NoBody,
    /// The hop reads a field its parent didn't write.
    NoField,
    /// A bound stopped the work here.
    Truncated,
    /// `auto*` stopped at `max_depth` with this input still detectable.
    DepthLimit,
}

/// One entry of a [`Trace`].
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Step {
    /// The hop, counted from 0.
    pub hop: usize,
    /// The step whose parser produced this input: an index into the trace.
    pub parent: Option<usize>,
    /// Where the input came from.
    pub input: Input,
    /// The parser, for a step that ran one.
    pub parser: Option<String>,
    /// What it came to.
    pub event: Event,
    /// Whether fields this step wrote are in the sink: `true` for a parser
    /// that took its input, until a reject drops the record.
    pub kept: bool,
}

/// What each hop did with one record, for working out why a chain parsed the
/// way it did.
///
/// Steps are in the order they ran: a parser's body, then its values in
/// write order. A parser that returned `NoMatch` leaves no step. Tracing
/// allocates, and [`super::ParsePlan::parse`] doesn't.
#[derive(Debug, Clone, Default)]
pub struct Trace {
    pub(super) steps: Vec<Step>,
}

impl Trace {
    /// An empty trace.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// The steps of the last record traced.
    #[must_use]
    pub fn steps(&self) -> &[Step] {
        &self.steps
    }
}

impl Failure {
    pub(super) const fn of(error: &ParseError) -> Self {
        match error {
            ParseError::Malformed { offset, reason } => Self::Malformed {
                offset: *offset,
                reason,
            },
            ParseError::Limit { what } => Self::Limit { what },
            ParseError::FrameTooLong { .. } => Self::Limit {
                what: "framing.max_bytes",
            },
            ParseError::Other(_) => Self::Other,
        }
    }
}