Lines
100 %
Functions
75 %
Branches
//! The activity report's `groups.N.name`, `groups.N.filter` and
//! `groups.N.flip` rows, as the editor shows them.
use askama::Template;
use forms::{FieldError, FormValues, address};
const FLIP_ON: &str = "yes";
#[derive(Default)]
pub struct GroupRow {
pub name: String,
pub filter: String,
pub flip: bool,
pub name_errors: Vec<String>,
pub filter_errors: Vec<String>,
pub flip_errors: Vec<String>,
}
impl GroupRow {
fn from_cells(index: usize, row: &forms::Row, errors: &[FieldError]) -> Self {
let cell = |key: &str| row.get(key).map_or("", String::as_str);
let problems = |key: &str| {
let at = address::cell("groups", index, key);
errors
.iter()
.filter(|error| error.field == at)
.map(|error| error.message.clone())
.collect()
};
Self {
name: cell("name").trim().to_owned(),
filter: cell("filter").to_owned(),
flip: cell("flip") == FLIP_ON,
name_errors: problems("name"),
filter_errors: problems("filter"),
flip_errors: problems("flip"),
/// The rows the form carries, each marked with the errors at its cells.
#[must_use]
pub fn group_rows(values: &FormValues, errors: &[FieldError]) -> Vec<GroupRow> {
let rows = values.rows("groups").unwrap_or_default();
rows.iter()
.enumerate()
.map(|(index, row)| GroupRow::from_cells(index, row, errors))
const CELLS: [&str; 3] = ["name", "filter", "flip"];
/// Whether the editor marks `error` on one of the cells it renders.
pub fn is_marked(values: &FormValues, error: &FieldError) -> bool {
let rows = values.rows("groups").map_or(0, |rows| rows.len());
(0..rows).any(|index| {
CELLS
.any(|key| error.field == address::cell("groups", index, key))
})
/// `oob` swaps the editor in beside a re-rendered form.
#[derive(Template)]
#[template(path = "components/report/activity_groups.html")]
pub struct GroupsEditor {
pub groups: Vec<GroupRow>,
pub oob: bool,
#[cfg(test)]
#[path = "activity_groups_tests.rs"]
mod tests;