Skip to main content

CliEmitter

Struct CliEmitter 

Source
pub struct CliEmitter<W: Write> { /* private fields */ }
Expand description

Where a CliEmitter sends its events, selected by --output-to.

The stream an event lands on follows the program’s consumption mode, not the event’s shape (see the spec’s CLI Event Framing):

  • OutputTo::Split (the default) is finite one-shot mode: result goes to stdout, while error/progress/log go to stderr. stdout therefore carries only successful payloads, so a shell capture or pipe never mistakes a failure for data.
  • OutputTo::Stdout / OutputTo::Stderr are event-stream mode: every event, including error, is collapsed onto that one stream so a consumer reading it in order (kind-branching) sees preserved ordering.

A command is an event stream when it produces more than one caller-needed output over time — a chunked payload, or an address the caller must act on before the command can report its outcome. Such a command defaults to OutputTo::Stdout and rejects an explicit split, because splitting it would strand the caller’s data on the diagnostic stream. If a kind:"progress" event carries a payload the caller must read, the command is an event stream that has not declared itself. Stateful emitter for structured CLI executions.

The output format, redaction policy, and stream routing are fixed when the emitter is created. Emitting after a terminal event, emitting a repeated terminal event, and writer failures all return explicit errors.

Routing follows the consumption mode (OutputTo):

  • CliEmitter::finite / CliEmitter::finite_with — finite one-shot: result → the primary writer (stdout), error/progress/log → the diagnostic writer (stderr). This is the recommended default for a one-shot CLI, so shell capture and pipelines never treat a failure as data.
  • CliEmitter::stream — event stream: every event, including error, goes to the single writer, preserving interleaved ordering.
  • CliEmitter::from_output_to builds either shape from a parsed OutputTo selector.

Implementations§

Source§

impl<W: Write> CliEmitter<W>

Source

pub fn new(writer: W, format: OutputFormat) -> Self

Create an event-stream emitter: every event goes to writer.

Alias for CliEmitter::stream. Use CliEmitter::finite for a one-shot command that should split result/error across stdout/stderr.

Source

pub fn with_options( writer: W, format: OutputFormat, output_options: OutputOptions, ) -> Self

Create an event-stream emitter with custom output options.

Source

pub fn stream(writer: W, format: OutputFormat) -> Self

Create an event-stream emitter: every event, including error, goes to the single writer, preserving interleaved ordering. Pick this when the consumer reads one ordered stream and branches on kind.

Source

pub fn finite_with( result_writer: W, diagnostic: impl Write + 'static, format: OutputFormat, ) -> Self

Create a finite one-shot emitter with explicit sinks: result goes to result_writer, while error/progress/log go to diagnostic.

Source

pub fn finite_with_options( result_writer: W, diagnostic: impl Write + 'static, format: OutputFormat, output_options: OutputOptions, ) -> Self

Create a finite one-shot emitter with explicit sinks and output options.

Source

pub fn with_strict_protocol(self) -> Self

Require the AFDATA recommended strict profile for every emitted event.

Source

pub fn emit(&mut self, event: Event) -> Result<(), CliEmitterError>

Emit a typed Event (unified entry for all event kinds).

Accepts only SDK-constructed Event; for dynamic JSON, use emit_validated_value.

Source

pub fn emit_validated_value( &mut self, value: Value, ) -> Result<(), CliEmitterError>

Emit and validate dynamic JSON, then apply redaction/formatting/write.

Runs strict validation first, ensuring the dynamic JSON is safe.

Source

pub fn emit_result(&mut self, payload: Value) -> Result<(), CliEmitterError>

Convenience: build and emit a result event.

Source

pub fn emit_error( &mut self, code: &str, message: &str, ) -> Result<(), CliEmitterError>

Convenience: build and emit an error event.

Source

pub fn emit_progress(&mut self, message: &str) -> Result<(), CliEmitterError>

Convenience: build and emit a progress event.

Source

pub fn emit_log( &mut self, level: LogLevel, message: &str, ) -> Result<(), CliEmitterError>

Convenience: build and emit a log event.

Source

pub fn finish(&mut self, event: Event, success_code: u8) -> u8

Emit event as the terminal event and resolve the outcome to a process exit code, so a one-shot CLI need not hand-roll the emit-then-exit dance.

A successful write returns success_code; a broken pipe (the reader hung up) returns 0; any other write or validation failure returns 4. A library never calls process::exit itself — return this code from main (std::process::ExitCode::from(code)).

Source

pub fn finish_result(&mut self, payload: Value) -> u8

Convenience over CliEmitter::finish: emit a result payload and return 0 on success.

For an error, build it with json_error (.hint(…), .retryable(…), .field(…) as needed) and pass the event to CliEmitter::finish with the desired exit code — the builder is the error type, so no separate error-emitting convenience is needed.

Source

pub fn into_inner(self) -> W

Access the underlying writer.

Source§

impl CliEmitter<Stdout>

Source

pub fn finite(format: OutputFormat) -> Self

Create a finite one-shot emitter wired to the process streams: resultstdout, error/progress/logstderr. The recommended default for a one-shot CLI.

Source

pub fn finite_options( format: OutputFormat, output_options: OutputOptions, ) -> Self

Create a finite one-shot emitter wired to the process streams, with custom output options.

Source§

impl CliEmitter<Box<dyn Write>>

Source

pub fn from_output_to(selector: OutputTo, format: OutputFormat) -> Self

Build an emitter from a parsed OutputTo selector, wired to the process streams: Split is finite mode (result → stdout, everything else → stderr); Stdout/Stderr are event-stream mode onto that stream.

Source

pub fn from_output_to_with( selector: OutputTo, format: OutputFormat, output_options: OutputOptions, ) -> Self

As CliEmitter::from_output_to, with custom output options.

Auto Trait Implementations§

§

impl<W> !RefUnwindSafe for CliEmitter<W>

§

impl<W> !Send for CliEmitter<W>

§

impl<W> !Sync for CliEmitter<W>

§

impl<W> !UnwindSafe for CliEmitter<W>

§

impl<W> Freeze for CliEmitter<W>
where W: Freeze,

§

impl<W> Unpin for CliEmitter<W>
where W: Unpin,

§

impl<W> UnsafeUnpin for CliEmitter<W>
where W: UnsafeUnpin,

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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 = 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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more