Skip to main content

web/pages/report/
activity_groups.rs

1//! The activity report's `groups.N.name`, `groups.N.filter` and
2//! `groups.N.flip` rows, as the editor shows them.
3
4use askama::Template;
5use forms::{FieldError, FormValues, address};
6
7const FLIP_ON: &str = "yes";
8
9#[derive(Default)]
10pub struct GroupRow {
11    pub name: String,
12    pub filter: String,
13    pub flip: bool,
14    pub name_errors: Vec<String>,
15    pub filter_errors: Vec<String>,
16    pub flip_errors: Vec<String>,
17}
18
19impl GroupRow {
20    fn from_cells(index: usize, row: &forms::Row, errors: &[FieldError]) -> Self {
21        let cell = |key: &str| row.get(key).map_or("", String::as_str);
22        let problems = |key: &str| {
23            let at = address::cell("groups", index, key);
24            errors
25                .iter()
26                .filter(|error| error.field == at)
27                .map(|error| error.message.clone())
28                .collect()
29        };
30        Self {
31            name: cell("name").trim().to_owned(),
32            filter: cell("filter").to_owned(),
33            flip: cell("flip") == FLIP_ON,
34            name_errors: problems("name"),
35            filter_errors: problems("filter"),
36            flip_errors: problems("flip"),
37        }
38    }
39}
40
41/// The rows the form carries, each marked with the errors at its cells.
42#[must_use]
43pub fn group_rows(values: &FormValues, errors: &[FieldError]) -> Vec<GroupRow> {
44    let rows = values.rows("groups").unwrap_or_default();
45    rows.iter()
46        .enumerate()
47        .map(|(index, row)| GroupRow::from_cells(index, row, errors))
48        .collect()
49}
50
51const CELLS: [&str; 3] = ["name", "filter", "flip"];
52
53/// Whether the editor marks `error` on one of the cells it renders.
54#[must_use]
55pub fn is_marked(values: &FormValues, error: &FieldError) -> bool {
56    let rows = values.rows("groups").map_or(0, |rows| rows.len());
57    (0..rows).any(|index| {
58        CELLS
59            .iter()
60            .any(|key| error.field == address::cell("groups", index, key))
61    })
62}
63
64/// `oob` swaps the editor in beside a re-rendered form.
65#[derive(Template)]
66#[template(path = "components/report/activity_groups.html")]
67pub struct GroupsEditor {
68    pub groups: Vec<GroupRow>,
69    pub oob: bool,
70}
71
72#[cfg(test)]
73#[path = "activity_groups_tests.rs"]
74mod tests;