Skip to main content

StreamGrammar

Struct StreamGrammar 

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

Incremental validator for exactly one Client event stream.

Implementations§

Source§

impl StreamGrammar

Source

pub fn new() -> Self

Creates a validator before the first event.

Source

pub fn accept(&mut self, event: &Event) -> Result<(), StreamError>

Validates one event without taking ownership from its consumer.

§Errors

A rejected event means the provider’s stream is not a well-formed sequence, which the caller must treat as a failed generation rather than as a recoverable gap: the accumulated state is no longer a faithful picture of what the provider sent.

  • EventAfterDone for anything at all after the terminal event. Checked before the event’s own shape, because after a terminal there is no lifecycle left to be consistent with.
  • EmptyItemId for an event with no identity, and EmptyToolName for a tool event that does not name a tool. Neither can be correlated with anything.
  • DuplicateItemId if an identity that already completed is opened again. Identities are single-use for the whole stream, so a reused one is a provider defect rather than a second item.
  • InvalidLifecycle if the event does not fit the identity’s active state — content deltas for a tool call, a tool final for an identity opened as content, or a content kind that changed mid-item.
  • FinalPayloadMismatch if a final payload is not exactly what the deltas accumulated. This is the check that makes incremental output trustworthy: a consumer that rendered the deltas has already shown the user something, so a final that disagrees cannot be silently preferred.
  • UnfinishedItems if the terminal event arrives while any identity is still open.
§Deltas are optional

A final may open its own item. Event::ContentFinal for an identity no delta ever opened, and Event::ToolCallFinal whose call accumulated no ToolCallArgumentsDelta — whether or not a ToolCallStart announced it — are both well-formed, and the final’s payload is the item. A provider that answers in one frame is not required to synthesise a delta equal to its own payload, which is the workaround every adapter for such a provider would otherwise have to invent.

Nothing about the mismatch rule is relaxed by this. What FinalPayloadMismatch protects is a consumer that has already rendered something, so it applies exactly when deltas were sent: a final that contradicts a non-empty accumulation is still rejected. With no deltas there is nothing rendered for the final to contradict.

Identities are spent either way. A single-frame final inserts its identity, so reusing it afterwards is DuplicateItemId exactly as reusing a streamed one is.

Source

pub fn finish(self) -> Result<(), StreamError>

Verifies that the producer ended after exactly one terminal event.

§Errors

MissingTerminal if the stream ended without one. This is the one violation accept cannot report, because it is the absence of an event rather than a bad one: a stream that simply stops is indistinguishable, event by event, from one that is still going.

Trait Implementations§

Source§

impl Debug for StreamGrammar

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Reports the validator’s position in the stream, not the stream.

The open map accumulates the exact text and argument bytes each item has produced so far, so it is counted rather than shown; the counts and the terminal flag are what a grammar violation is actually diagnosed from.

Source§

impl Default for StreamGrammar

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.