pub mod json;
pub mod ndjson;
pub mod text;
pub mod tsv;
use super::envelope::{Envelope, Row, Target};
use crate::cli::globals::OutputFormat;
use crate::error::{Failure, Result};
use serde_json::Value;
use std::io::Write;
pub(super) fn column_names(elements: &[Value]) -> Vec<String> {
let mut names: Vec<String> = Vec::new();
for element in elements {
let Some(fields) = element.as_object() else {
continue;
};
for name in fields.keys() {
if !names.iter().any(|seen| seen == name) {
names.push(name.clone());
}
}
}
names
}
pub const LINE_ENDING: char = '\n';
pub trait Formatter {
fn name(&self) -> &'static str;
fn write_envelope(&mut self, writer: &mut dyn Write, envelope: &Envelope) -> Result<()>;
fn begin_stream(
&mut self,
writer: &mut dyn Write,
head: &Envelope,
columns: &[String],
) -> Result<()>;
fn write_row(&mut self, writer: &mut dyn Write, row: &Row, line: &mut String) -> Result<()>;
fn end_stream(
&mut self,
writer: &mut dyn Write,
tail: &Envelope,
emitted: u64,
gaps: &[crate::output::envelope::Gap],
applied: Option<&crate::output::envelope::Applied>,
) -> Result<()>;
}
pub const END_OF_STREAM_KEY: &str = "end_of_stream";
pub const GAPS_KEY: &str = "gaps";
fn gaps_as_json(gaps: &[crate::output::envelope::Gap]) -> Result<String> {
if gaps.is_empty() {
return Ok(String::new());
}
let rendered = to_compact(&gaps)?;
Ok(format!(r#","{GAPS_KEY}":{rendered}"#))
}
fn causes_of(gaps: &[crate::output::envelope::Gap]) -> String {
gaps.iter()
.map(|gap| gap.cause.as_str())
.collect::<Vec<_>>()
.join(",")
}
pub const ROW_COUNT_KEY: &str = "row_count";
#[must_use]
pub fn destination_text(target: &Target) -> String {
let mut text = String::new();
if let Some(profile) = &target.profile {
text.push_str(profile);
text.push('@');
}
if let Some(socket) = &target.socket {
text.push_str(socket);
} else if let Some(host) = &target.host {
text.push_str(host);
if let Some(port) = target.port {
text.push(':');
text.push_str(&port.to_string());
}
}
if let Some(database) = &target.database {
text.push('/');
text.push_str(database);
}
text
}
fn destination_of(tail: &Envelope) -> Option<String> {
let text = destination_text(tail.target.as_ref()?);
(!text.is_empty()).then_some(text)
}
#[must_use]
pub fn for_format(format: OutputFormat) -> Box<dyn Formatter> {
match format {
OutputFormat::Json => Box::new(json::Json::default()),
OutputFormat::Ndjson => Box::new(ndjson::Ndjson),
OutputFormat::Tsv => Box::new(tsv::Tsv::default()),
OutputFormat::Text => Box::new(text::Text::default()),
}
}
pub fn emit(writer: &mut dyn Write, text: &str) -> Result<()> {
writer
.write_all(text.as_bytes())
.map_err(|error| classify(&error))
}
pub fn emit_line(writer: &mut dyn Write, text: &str) -> Result<()> {
emit(writer, text)?;
writer
.write_all(&[LINE_ENDING as u8])
.map_err(|error| classify(&error))
}
pub(crate) fn classify(error: &std::io::Error) -> Failure {
if error.kind() == std::io::ErrorKind::BrokenPipe {
return Failure::new(
crate::error::Category::ChannelClosed,
"output.broken_pipe",
"the output channel was closed by the reader",
);
}
Failure::new(
crate::error::Category::Server,
"output.write_failed",
"the answer could not be written",
)
.with_cause(error.to_string())
}
pub fn to_compact(value: &impl serde::Serialize) -> Result<String> {
serde_json::to_string(value).map_err(|error| {
Failure::new(
crate::error::Category::Server,
"output.not_serialisable",
"the answer could not be serialised",
)
.with_cause(error.to_string())
})
}
pub fn to_document(
value: &impl serde::Serialize,
) -> Result<serde_json::Map<String, serde_json::Value>> {
let document = serde_json::to_value(value).map_err(|error| {
Failure::new(
crate::error::Category::Server,
"output.not_serialisable",
"the answer could not be serialised",
)
.with_cause(error.to_string())
})?;
match document {
serde_json::Value::Object(fields) => Ok(fields),
_ => Err(Failure::new(
crate::error::Category::Server,
"output.not_a_document",
"the answer is not an object and cannot carry a closing record",
)),
}
}
#[cfg(test)]
mod tests {
use super::*;
use indexmap::IndexMap;
use serde_json::json;
pub(super) fn sample_row(name: &str, size: i64) -> Row {
let mut row = IndexMap::new();
row.insert("name".to_owned(), json!(name));
row.insert("size".to_owned(), json!(size));
row
}
pub(super) fn bare_tail() -> Envelope {
Envelope::new("query run", json!({}))
}
pub(super) fn tail_from_a_destination() -> Envelope {
bare_tail().from_destination(
Target {
profile: Some("producao".to_owned()),
host: Some("192.0.2.10".to_owned()),
port: Some(3306),
socket: None,
database: Some("loja".to_owned()),
},
crate::output::envelope::TargetSource::Argv,
)
}
pub(super) fn stream_through(formatter: &mut dyn Formatter, rows: &[Row]) -> String {
stream_through_from(formatter, rows, &bare_tail())
}
pub(super) fn stream_through_from(
formatter: &mut dyn Formatter,
rows: &[Row],
tail: &Envelope,
) -> String {
let mut out = Vec::new();
let columns = vec!["name".to_owned(), "size".to_owned()];
formatter
.begin_stream(&mut out, tail, &columns)
.expect("opens");
let mut line = String::new();
for row in rows {
formatter
.write_row(&mut out, row, &mut line)
.expect("writes");
}
let count = u64::try_from(rows.len()).expect("fits");
formatter
.end_stream(&mut out, tail, count, &[], None)
.expect("closes");
String::from_utf8(out).expect("utf8")
}
pub(super) fn stream_through_cut(formatter: &mut dyn Formatter, rows: &[Row]) -> String {
let tail = bare_tail();
let mut out = Vec::new();
let columns = vec!["name".to_owned(), "size".to_owned()];
formatter
.begin_stream(&mut out, &tail, &columns)
.expect("opens");
let mut line = String::new();
for row in rows {
formatter
.write_row(&mut out, row, &mut line)
.expect("writes");
}
let count = u64::try_from(rows.len()).expect("fits");
let cut = crate::output::envelope::Applied {
output_truncated: true,
..crate::output::envelope::Applied::default()
};
formatter
.end_stream(&mut out, &tail, count, &[], Some(&cut))
.expect("closes");
String::from_utf8(out).expect("utf8")
}
#[test]
fn no_shape_claims_a_cut_set_is_whole() {
let rows = vec![sample_row("alpha", 1)];
for shape in [OutputFormat::Ndjson, OutputFormat::Tsv] {
let written = stream_through_cut(&mut *for_format(shape), &rows);
assert!(
!written.contains(END_OF_STREAM_KEY),
"{shape:?} closed a cut flow: {written}"
);
}
let read_by_a_person = stream_through_cut(&mut *for_format(OutputFormat::Text), &rows);
assert!(
read_by_a_person.contains("cut"),
"the shape a person reads said nothing about the cut: {read_by_a_person}"
);
}
#[test]
fn a_whole_set_still_gets_the_record_that_declares_it_whole() {
let rows = vec![sample_row("alpha", 1)];
for shape in [OutputFormat::Ndjson, OutputFormat::Tsv, OutputFormat::Text] {
let written = stream_through(&mut *for_format(shape), &rows);
assert!(
written.contains(END_OF_STREAM_KEY),
"{shape:?} lost its closing record: {written}"
);
assert!(
!written.contains("cut"),
"{shape:?} reported a cut that never happened: {written}"
);
}
}
#[test]
fn every_published_shape_has_a_formatter_with_its_own_name() {
let shapes = [
OutputFormat::Json,
OutputFormat::Ndjson,
OutputFormat::Tsv,
OutputFormat::Text,
];
let mut names: Vec<&str> = shapes
.into_iter()
.map(|shape| for_format(shape).name())
.collect();
let before = names.len();
names.sort_unstable();
names.dedup();
assert_eq!(
names.len(),
before,
"each shape must name itself distinctly"
);
}
#[test]
fn a_line_never_carries_the_platform_convention() {
let mut out = Vec::new();
emit_line(&mut out, "one").expect("writes");
assert_eq!(out, b"one\n", "a line ends with a single line feed");
}
#[test]
fn a_closed_reader_maps_to_the_published_broken_pipe_failure() {
struct Closed;
impl Write for Closed {
fn write(&mut self, _: &[u8]) -> std::io::Result<usize> {
Err(std::io::Error::new(
std::io::ErrorKind::BrokenPipe,
"closed",
))
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
let failure = emit(&mut Closed, "anything").expect_err("must fail");
assert_eq!(failure.code(), "output.broken_pipe");
assert_eq!(failure.category(), crate::error::Category::ChannelClosed);
assert_ne!(failure.category(), crate::error::Category::Limit);
assert_eq!(failure.exit_number().code(), 141);
}
#[test]
fn both_doors_onto_the_channel_answer_a_closed_reader_alike() {
let closed = std::io::Error::new(std::io::ErrorKind::BrokenPipe, "closed");
let shaped = classify(&closed);
let enveloped = crate::output::sink::classify_for_proof(&closed);
assert_eq!(shaped.code(), enveloped.code());
assert_eq!(shaped.category(), enveloped.category());
assert_eq!(shaped.exit_number().code(), enveloped.exit_number().code());
}
}