lemma 0.11.4

A pure, declarative language for business rules.
Documentation
//! Everything the CLI prints. A reader that stops early or a full disk is not a crash.

use anyhow::Result;
use std::fmt;
use std::io::{self, ErrorKind, Write};

/// Writes a command's result to stdout. A reader that stopped reading
/// (`lemma show … | head`) has what it wants: the process ends with success.
pub fn print(text: fmt::Arguments<'_>) -> Result<()> {
    let mut stdout = io::stdout().lock();
    match stdout.write_fmt(text).and_then(|()| stdout.flush()) {
        Ok(()) => Ok(()),
        Err(error) if error.kind() == ErrorKind::BrokenPipe => std::process::exit(0),
        Err(error) => Err(anyhow::anyhow!("Error writing output: {error}")),
    }
}

/// Writes a message to stdout and goes on when nobody can read it.
pub fn announce(text: fmt::Arguments<'_>) {
    unread(writeln!(io::stdout().lock(), "{text}"));
}

/// Writes a message to stderr and goes on when nobody can read it: the work and the
/// exit code do not depend on it.
pub fn note(text: fmt::Arguments<'_>) {
    unread(writeln!(io::stderr().lock(), "{text}"));
}

/// There is nowhere left to report a message that could not be written.
fn unread(written: io::Result<()>) {
    drop(written);
}