mod format_num;
mod hints;
mod plan;
mod quiet;
mod stats;
mod table;
use crate::query::types::QueryResponse;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum QueryOutputFormat {
#[default]
Full,
Json,
Stats,
Plan,
Quiet,
Table,
}
impl std::str::FromStr for QueryOutputFormat {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_ascii_lowercase().as_str() {
"full" => Ok(Self::Full),
"json" => Ok(Self::Json),
"stats" => Ok(Self::Stats),
"plan" => Ok(Self::Plan),
"quiet" => Ok(Self::Quiet),
"table" => Ok(Self::Table),
other => Err(format!(
"unknown output format {other:?}; expected full, json, stats, plan, quiet, or table"
)),
}
}
}
pub fn format_query_response(
response: &QueryResponse,
format: QueryOutputFormat,
) -> Result<String, String> {
match format {
QueryOutputFormat::Full => {
serde_json::to_string_pretty(response).map_err(|e| e.to_string())
}
QueryOutputFormat::Json => serde_json::to_string(response).map_err(|e| e.to_string()),
QueryOutputFormat::Stats => stats::format_stats_json(response),
QueryOutputFormat::Plan => plan::format_plan_json(response),
QueryOutputFormat::Quiet => quiet::format_quiet_line(response),
QueryOutputFormat::Table => table::format_table_text(response),
}
}
#[must_use]
pub fn format_query_stderr_hints(response: &QueryResponse) -> Option<String> {
hints::format_catalog_miss_hint(response)
}