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/logto 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, includingerror, goes to the one writer, preserving interleaved ordering for a consumer that reads in order and branches onkind.
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>
impl<W: Write> CliEmitter<W>
Sourcepub fn stream(writer: W, format: OutputFormat) -> Self
pub fn stream(writer: W, format: OutputFormat) -> Self
Event-stream mode: every event, including error, goes to writer.
Sourcepub fn finite(
result_writer: W,
diagnostic: impl Write + 'static,
format: OutputFormat,
) -> Self
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.
Sourcepub fn with_options(self, output_options: OutputOptions) -> Self
pub fn with_options(self, output_options: OutputOptions) -> Self
Render with these redaction and formatting options.
Sourcepub fn set_options(&mut self, output_options: OutputOptions)
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.
Sourcepub fn with_strict_protocol(self) -> Self
pub fn with_strict_protocol(self) -> Self
Require the AFDATA recommended strict profile for every emitted event.
Sourcepub const fn format(&self) -> OutputFormat
pub const fn format(&self) -> OutputFormat
The format events are rendered in.
Sourcepub fn emit(&mut self, event: Event) -> Result<(), CliEmitterError>
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.
Sourcepub fn emit_validated_value(
&mut self,
value: Value,
) -> Result<(), CliEmitterError>
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.
Sourcepub fn emit_result(&mut self, payload: Value) -> Result<(), CliEmitterError>
pub fn emit_result(&mut self, payload: Value) -> Result<(), CliEmitterError>
Convenience: build and emit a result event.
Sourcepub fn emit_error(
&mut self,
code: &str,
message: &str,
) -> Result<(), CliEmitterError>
pub fn emit_error( &mut self, code: &str, message: &str, ) -> Result<(), CliEmitterError>
Convenience: build and emit an error event.
Sourcepub fn emit_progress(&mut self, message: &str) -> Result<(), CliEmitterError>
pub fn emit_progress(&mut self, message: &str) -> Result<(), CliEmitterError>
Convenience: build and emit a progress event.
Sourcepub fn emit_log(
&mut self,
level: LogLevel,
message: &str,
) -> Result<(), CliEmitterError>
pub fn emit_log( &mut self, level: LogLevel, message: &str, ) -> Result<(), CliEmitterError>
Convenience: build and emit a log event.
Sourcepub fn write_raw(&mut self, bytes: &[u8]) -> Result<(), CliEmitterError>
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.
Sourcepub fn finish_raw(&mut self, bytes: &[u8]) -> u8
pub fn finish_raw(&mut self, bytes: &[u8]) -> u8
Write the invocation’s final raw bytes and settle its exit code.
Sourcepub fn finish(&mut self, event: Event, success_code: u8) -> u8
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.
Sourcepub fn finish_result(&mut self, payload: Value) -> u8
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.
Sourcepub fn finish_document_result(&mut self, payload: Value) -> DocumentResult<u8>
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.
Sourcepub fn exit_code(&mut self, intended: u8) -> u8
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)).
Sourcepub const fn write_failure(&self) -> Option<ErrorKind>
pub const fn write_failure(&self) -> Option<ErrorKind>
The kind of the first failed write, if any.
Sourcepub fn into_inner(self) -> W
pub fn into_inner(self) -> W
Access the underlying writer.
Source§impl CliEmitter<Box<dyn Write>>
impl CliEmitter<Box<dyn Write>>
Sourcepub fn from_plan(plan: &OutputPlan) -> Self
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.
Sourcepub fn from_output_to(selector: OutputTo, format: OutputFormat) -> Self
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.