Lines
96.15 %
Functions
60.71 %
Branches
100 %
//! The 422 of a report whose query is invalid.
use askama::Template;
use axum::http::{HeaderMap, StatusCode};
use axum::response::{Html, IntoResponse, Response};
use forms::Form;
use crate::form_invalid::form_invalid;
use crate::response::WebError;
/// The id of the element a report page loads its content into.
pub const SHELL_ID: &str = "report-shell";
#[derive(Template)]
#[template(path = "components/report/filters.html")]
struct Filters<'a> {
form: String,
form_id: String,
shown: &'a [(String, String)],
editor: String,
}
/// Renders the rows editor beside the form; `true` swaps it in out of band.
pub type Editor<'a> = Option<&'a dyn Fn(bool) -> Result<String, WebError>>;
fn editor_html(editor: Editor, oob: bool) -> Result<String, WebError> {
editor.map_or_else(|| Ok(String::new()), |render| render(oob))
fn loads_shell(headers: &HeaderMap) -> bool {
headers
.get("hx-trigger")
.and_then(|value| value.to_str().ok())
== Some(SHELL_ID)
/// The refused report: the form replaces itself on a loaded table, but a
/// page that loads the table has no form yet, so it gets the form inside its
/// own filters wrapper, which also carries the `shown` table and chart
/// controls the corrected submit must not lose.
pub fn report_invalid(
headers: &HeaderMap,
form: &Form,
form_html: String,
shown: &[(String, String)],
editor: Editor,
) -> Result<Response, WebError> {
if !loads_shell(headers) {
return Ok(form_invalid(form, form_html + &editor_html(editor, true)?));
let html = Filters {
form: form_html,
form_id: format!("nf-{}", form.name),
shown,
editor: editor_html(editor, false)?,
.render()
.map_err(|e| WebError::internal("Report filters render failed", e))?;
Ok((StatusCode::UNPROCESSABLE_ENTITY, Html(html)).into_response())
#[cfg(test)]
#[path = "invalid_tests.rs"]
mod tests;