mariadb-cli 0.1.1

One-shot agent-native command line client for MariaDB servers
Documentation
//! The primary channel: data only, in the published shape.
//!
//! Diagnostics, progress and warnings never reach this channel. Nothing here
//! writes to the process output directly; a sink does that once, at the end.

pub mod checksum;
pub mod destination;
pub mod envelope;
pub mod format;
pub mod ops;
pub mod sink;
pub mod stream;

pub use envelope::{Applied, Envelope, Gap, Row, SCHEMA_VERSION};
pub use ops::{Filter, Pipeline};
pub use sink::write_line;

use std::io::Write;

/// A writer that remembers how much really went through it.
///
/// # Why the count is taken here and nowhere else
///
/// A caller that has to budget its own context needs the size of the answer it
/// actually received (FR-139), and the only honest place to learn that is the
/// last thing every byte passes on its way out. Everything above this — the
/// ceiling per cell, the ceiling on the whole answer, the shape chosen for the
/// primary channel — measures something else: a value before it was rendered,
/// a payload before it was serialised, a row before it was widened into a
/// column. Each of those numbers is useful and none of them is this one, and a
/// declaration built from any of them would disagree with the channel it
/// claims to describe. A declaration that disagrees is worse than none, so it
/// is taken from the bytes themselves.
///
/// The count accumulates across the whole invocation, so a continuous flow of
/// a million lines is declared as one total rather than as a million readings
/// nobody added up.
pub struct Counted<W: Write> {
    inner: W,
    bytes: u64,
    lines: u64,
}

impl<W: Write> Counted<W> {
    /// Wraps a writer, counting from nothing.
    pub const fn new(inner: W) -> Self {
        Self {
            inner,
            bytes: 0,
            lines: 0,
        }
    }

    /// How many bytes have reached the channel.
    #[must_use]
    pub const fn bytes(&self) -> u64 {
        self.bytes
    }

    /// How many lines have reached the channel.
    ///
    /// A line is counted by its ending, which is the single line feed every
    /// formatter writes and the only one any of them writes. An answer whose
    /// last byte is not a line ending is therefore counted as the whole lines
    /// it completed, which is what a reader counting endings would also find.
    #[must_use]
    pub const fn lines(&self) -> u64 {
        self.lines
    }
}

impl<W: Write> Write for Counted<W> {
    fn write(&mut self, buffer: &[u8]) -> std::io::Result<usize> {
        // Only the prefix the writer accepted is counted. A short write is
        // ordinary, and counting the whole buffer for one would declare bytes
        // that never left.
        let written = self.inner.write(buffer)?;
        let taken = &buffer[..written];
        self.bytes += written as u64;
        // Allowed: the faster count clippy names lives in a crate of its own,
        // and a dependency added to count line endings in a buffer this
        // process already holds would cost more than it saves. The buffer is
        // one write's worth, never a file.
        #[allow(clippy::naive_bytecount)]
        {
            self.lines += taken
                .iter()
                .filter(|byte| **byte == format::LINE_ENDING as u8)
                .count() as u64;
        }
        Ok(written)
    }

    fn flush(&mut self) -> std::io::Result<()> {
        self.inner.flush()
    }
}