ClaudeParser

Struct ClaudeParser 

Source
pub struct ClaudeParser { /* private fields */ }
Expand description

Claude event parser

Note: This parser is designed for single-threaded use only. The internal state uses Rc<RefCell<>> for convenience, not for thread safety. Do not share this parser across threads.

Implementations§

Source§

impl ClaudeParser

Source

pub fn new(colors: Colors, verbosity: Verbosity) -> Self

Create a new ClaudeParser with the given colors and verbosity.

§Arguments
  • colors - Colors for terminal output
  • verbosity - Verbosity level for output
§Returns

A new ClaudeParser instance

§Example
use ralph_workflow::json_parser::ClaudeParser;
use ralph_workflow::logger::Colors;
use ralph_workflow::config::Verbosity;

let parser = ClaudeParser::new(Colors::new(), Verbosity::Normal);
Source

pub fn with_printer( colors: Colors, verbosity: Verbosity, printer: SharedPrinter, ) -> Self

Create a new ClaudeParser with a custom printer.

§Arguments
  • colors - Colors for terminal output
  • verbosity - Verbosity level for output
  • printer - Shared printer for output
§Returns

A new ClaudeParser instance

Source

pub fn with_display_name(self, display_name: &str) -> Self

Set the display name for this parser.

§Arguments
  • display_name - The name to display in output
§Returns

Self for builder pattern chaining

Source

pub fn printer(&self) -> SharedPrinter

Get a shared reference to the printer.

This allows tests, monitoring, and other code to access the printer after parsing to verify output content, check for duplicates, or capture output for analysis.

§Returns

A clone of the shared printer reference (Rc<RefCell<dyn Printable>>)

§Example
use ralph_workflow::json_parser::{ClaudeParser, printer::TestPrinter};
use std::rc::Rc;
use std::cell::RefCell;

let printer = Rc::new(RefCell::new(TestPrinter::new()));
let parser = ClaudeParser::with_printer(colors, verbosity, Rc::clone(&printer));

// Parse events...

// Now access the printer to verify output
let printer_ref = parser.printer().borrow();
assert!(!printer_ref.has_duplicate_consecutive_lines());
Source

pub fn streaming_metrics(&self) -> StreamingQualityMetrics

Get streaming quality metrics from the current session.

This provides insight into the deduplication and streaming quality of the parsing session, including:

  • Number of snapshot repairs (when the agent sent accumulated content as a delta)
  • Number of large deltas (potential protocol violations)
  • Total deltas processed

Useful for testing, monitoring, and debugging streaming behavior.

§Returns

A copy of the streaming quality metrics from the internal StreamingSession.

§Example
use ralph_workflow::json_parser::{ClaudeParser, printer::TestPrinter};
use std::rc::Rc;
use std::cell::RefCell;

let printer = Rc::new(RefCell::new(TestPrinter::new()));
let parser = ClaudeParser::with_printer(colors, verbosity, Rc::clone(&printer));

// Parse events...

// Verify deduplication logic triggered
let metrics = parser.streaming_metrics();
assert!(metrics.snapshot_repairs_count > 0, "Snapshot repairs should occur");
Source

pub fn parse_event(&self, line: &str) -> Option<String>

Parse and display a single Claude JSON event

Returns Some(formatted_output) for valid events, or None for:

  • Malformed JSON (logged at debug level)
  • Unknown event types
  • Empty or whitespace-only output
Source

pub fn parse_stream<R: BufRead>(&self, reader: R) -> Result<()>

Parse a stream of Claude NDJSON events

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.