Skip to main content

ToolIO

Trait ToolIO 

Source
pub trait ToolIO: Write + Sized {
    type OutputItem;

    // Required methods
    fn is_machine(&self) -> bool;
    fn stderr(&mut self) -> &mut dyn Write;
    fn item(&mut self, value: &Self::OutputItem) -> Result<()>
       where Self::OutputItem: Display;

    // Provided methods
    fn supports_structured_output() -> bool { ... }
    fn has_schema() -> bool { ... }
    fn try_print_schema(&mut self) -> Result<()> { ... }
    fn print(&mut self, value: impl Display) -> Result<()> { ... }
    fn line(&mut self, value: impl Display) -> Result<()> { ... }
}
Expand description

ToolIO defines the necessary functions to perform output from a tool, potentially including type-safe machine output if required.

There are three provided implementations: and [crate::SimpleWriter]. These provide either type-safe, type-safe with schema, or string-only output respectively.

Required Associated Types§

Source

type OutputItem

The type of object that is expected for the Self::item call (or any machine output writing functions that may be added by an implementation)

Required Methods§

Source

fn is_machine(&self) -> bool

Returns true if the receiver was configured to output for machines.

Source

fn stderr(&mut self) -> &mut dyn Write

Returns an error stream that errors can be written to.

Source

fn item(&mut self, value: &Self::OutputItem) -> Result<()>
where Self::OutputItem: Display,

Displays the item in whatever formatted style is most appropriate based on is_machine and the underlying implementation

Provided Methods§

Source

fn supports_structured_output() -> bool

Whether this supports structured machine output (like JSON).

Source

fn has_schema() -> bool

Is a schema of the output type available.

Source

fn try_print_schema(&mut self) -> Result<()>

Source

fn print(&mut self, value: impl Display) -> Result<()>

Writes the value to standard output without a newline.

This is a no-op if is_machine returns true.

Source

fn line(&mut self, value: impl Display) -> Result<()>

Writes the value to standard output with a newline.

This is a no-op if is_machine returns true.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<T> ToolIO for JsonWriter<T>
where T: Serialize,