Skip to main content

CliEmitter

Struct CliEmitter 

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

Stateful emitter for structured CLI executions.

An emitter is one of two consumption modes (OutputTo selects between them, see the spec’s CLI Event Framing):

  • CliEmitter::finite — finite one-shot: result (and raw bytes) go to the result writer, error/progress/log to the diagnostic writer. The result stream then carries only successful payloads, so a shell capture or pipe never mistakes a failure for data.
  • CliEmitter::stream — event stream: every event, including error, goes to the one writer, preserving interleaved ordering for a consumer that reads in order and branches on kind.

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.

CliEmitter::from_output_to and CliEmitter::from_plan build either mode wired to the process streams. Options are chained: with_options, with_strict_protocol.

Emitting after a terminal event or emitting a second terminal event is an error. A failed write is returned and remembered, so whoever owns the process exit can settle it once with exit_code: a closed reader is success, any other failure is exit 4 with output_write_failed on the diagnostic stream.

Implementations§

Source§

impl<W: Write> CliEmitter<W>

Source

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

Event-stream mode: every event, including error, goes to writer.

Source

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

Finite one-shot mode: result and raw bytes go to result_writer, error/progress/log go to diagnostic.

Source

pub fn with_options(self, output_options: OutputOptions) -> Self

Render with these redaction and formatting options.

Source

pub fn set_options(&mut self, output_options: OutputOptions)

Change the options of an emitter someone else built — the one a CLI entry hands to its handler, for a command that decides its redaction policy from its own arguments.

Source

pub fn with_strict_protocol(self) -> Self

Require the AFDATA recommended strict profile for every emitted event.

Source

pub const fn format(&self) -> OutputFormat

The format events are rendered in.

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 write_raw(&mut self, bytes: &[u8]) -> Result<(), CliEmitterError>

Write bytes that are not an event to the result writer, unchanged.

Not everything a CLI emits is an event. --docs renders a Markdown reference, --output plain help a human catalog, shell bash a sourceable script, and a raw-output command its own bytes: their consumer is > or source, so a protocol envelope would be wrong. They still go through the emitter, so the routing and the exit policy for a failed write are the same ones every event gets.

Source

pub fn finish_raw(&mut self, bytes: &[u8]) -> u8

Write the invocation’s final raw bytes and settle its exit code.

Source

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

Emit event as the terminal event and resolve the process exit code.

A refused event — a protocol violation or a second terminal event — is the command’s own failure and returns 1. Otherwise this is exit_code with success_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.

Source

pub fn finish_document_result(&mut self, payload: Value) -> DocumentResult<u8>

Emit a result whose source can carry native non-finite numbers. YAML preserves these numbers and plain displays their spellings. JSON returns a document error before emitting anything.

Source

pub fn exit_code(&mut self, intended: u8) -> u8

The exit code a command that meant to exit intended must use, given everything this emitter has written.

No failed write: intended. A closed reader (tool | head): 0, silently — reading part of the output is not a failure. Any other failed write: 4, after reporting output_write_failed once as an error event — on the diagnostic sink in finite mode, on the process’s stderr in event-stream mode, whose only writer may be the one that failed. That report is best effort; a stderr that cannot be written leaves only the code. A library never calls process::exit itself — return this from main (std::process::ExitCode::from(code)).

Source

pub const fn write_failure(&self) -> Option<ErrorKind>

The kind of the first failed write, if any.

Source

pub fn into_inner(self) -> W

Access the underlying writer.

Source§

impl CliEmitter<Box<dyn Write>>

Source

pub fn from_plan(plan: &OutputPlan) -> Self

The emitter a resolved plan asks for, strict, wired to the process streams. A raw-output plan gets finite mode with JSON diagnostics: its bytes go to stdout through write_raw, its failures to stderr.

Source

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

Build either mode 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.

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> Forget for CliEmitter<W>

§

impl<W> Freeze for CliEmitter<W>
where W: Freeze, *const dyn Write is !null: Freeze, PhantomData<dyn Write>: Freeze, *const u8 is !null: Freeze, PhantomData<String>: Freeze,

§

impl<W> Move for CliEmitter<W>

§

impl<W> Unpin for CliEmitter<W>
where W: Unpin, Box<dyn Write>: Unpin, *const u8 is !null: Unpin,

§

impl<W> UnsafeUnpin for CliEmitter<W>

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 = !

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.
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