Lines
100 %
Functions
86.84 %
Branches
//! Report forms submitted through `submit-form`, drawn by `report-chart`.
use forms::{Form, FormValues, escape_str, write_payload};
use nomiscript::Value;
use plotting::kitty::encode_png_apc;
use rpc::Session;
use rpc::chart::{ChartFormat, ChartLook};
use crate::CommandError;
use crate::eval::Rendered;
use crate::forms::map_references;
use crate::render::reparse_list;
use crate::transaction::eval_text;
/// The width and height, in pixels, of a raster or vector chart.
pub type ChartSize = (u32, u32);
/// How a chart is drawn: its `kind` (`bar`, `line` or `stacked`), format, size and look.
#[derive(Debug, Clone, Copy)]
pub struct ChartRequest<'a> {
pub kind: &'a str,
pub format: ChartFormat,
pub size: ChartSize,
pub look: ChartLook,
}
/// The `:background` argument of a chart: `0xRRGGBB`, or `-1` for a transparent PNG.
fn background_arg(background: Option<[u8; 3]>) -> i32 {
background.map_or(-1, |[r, g, b]| {
i32::from(r) << 16 | i32::from(g) << 8 | i32::from(b)
})
/// The `report-chart` call for `form`'s `values`.
#[must_use]
pub fn chart_call(form: &Form, values: &FormValues, request: &ChartRequest) -> String {
let (width, height) = request.size;
format!(
"(report-chart {} {} :kind {} :format {} :width {width} :height {height} \
:text-px {} :background {})",
escape_str(form.name),
escape_str(&write_payload(form, values)),
escape_str(request.kind),
escape_str(request.format.name()),
request.look.text_px,
background_arg(request.look.background),
)
/// The chart of a report form, as the text, SVG or PNG literal `report-chart` replies with.
///
/// # Errors
/// [`CommandError::Argument`] for an unknown commodity,
/// [`CommandError::FormInvalid`] with one line per rejected field, or the
/// server error.
pub async fn chart(
session: &mut Session,
form: &Form,
values: &FormValues,
request: &ChartRequest<'_>,
) -> Result<String, CommandError> {
eval_mapped(session, form, values, |mapped| {
chart_call(form, mapped, request)
.await
async fn eval_mapped(
call: impl FnOnce(&FormValues) -> String,
let mapped = map_references(session, form, values).await?;
eval_text(session, &call(&mapped)).await
/// The `report-csv` call for the category report form's `values`.
pub fn csv_call(form: &Form, values: &FormValues) -> String {
format!("(report-csv {})", escape_str(&write_payload(form, values)))
/// The category report as the CSV text `report-csv` replies with.
/// As [`chart`].
pub async fn csv(
eval_mapped(session, form, values, |mapped| csv_call(form, mapped)).await
/// The PNG bytes of a report form's chart.
/// As [`chart`], or a reply that is not a bytes literal.
pub async fn chart_png(
kind: &str,
size: ChartSize,
) -> Result<Vec<u8>, CommandError> {
let request = ChartRequest {
kind,
format: ChartFormat::Png,
size,
look: ChartLook::default(),
};
let literal = chart(session, form, values, &request).await?;
png_from_literal(&literal)
/// The PNG bytes of the `#"…"` literal a PNG `report-chart` replies with.
/// [`CommandError::Command`] when `literal` is not a bytes literal.
pub fn png_from_literal(literal: &str) -> Result<Vec<u8>, CommandError> {
match reparse_list(literal) {
Ok(Value::Bytes(png)) => Ok(png),
Ok(other) => Err(CommandError::Command(format!(
"the chart reply is not a PNG: {}",
nomiscript::format_value(&other)
))),
Err(e) => Err(CommandError::Command(format!(
"the chart reply is not a PNG: {e}"
/// The chart for a terminal: the kitty-graphics PNG when `kitty`, else text.
/// As [`chart_png`] and [`chart`].
pub async fn terminal_chart(
kitty: bool,
) -> Result<Rendered, CommandError> {
if kitty {
let png = chart_png(session, form, values, kind, size).await?;
return Ok(Rendered::Bytes(encode_png_apc(&png).into_bytes()));
format: ChartFormat::Text,
let text = chart(session, form, values, &request).await?;
Ok(Rendered::Lines(text.lines().map(str::to_string).collect()))
#[cfg(test)]
mod literal_tests;
mod tests;