1
//! Query parameters the report endpoints no longer read.
2

            
3
use axum::{
4
    body::Body, extract::Request, middleware::Next, response::IntoResponse, response::Response,
5
};
6

            
7
use crate::response::WebError;
8

            
9
const GROUPS_HINT: &str = "use groups.N.name, groups.N.filter and groups.N.flip";
10

            
11
/// Retired parameter and what replaces it.
12
const RETIRED: [(&str, &str); 4] = [
13
    ("tag_filters", "use filter"),
14
    ("tag_filter_mode", "use filter"),
15
    ("groups", GROUPS_HINT),
16
    ("groups_mode", GROUPS_HINT),
17
];
18

            
19
/// Names only the balance report retired.
20
const RETIRED_BALANCE: [(&str, &str); 3] = [
21
    ("target_commodity_id", "use target"),
22
    ("date_from", "use from"),
23
    ("as_of", "use as-of"),
24
];
25

            
26
/// Names only the activity report retired.
27
const RETIRED_ACTIVITY: [(&str, &str); 4] = [
28
    ("target_commodity_id", "use target"),
29
    ("date_from", "use from"),
30
    ("date_to", "use to"),
31
    ("period_grouping", "use grouping"),
32
];
33

            
34
/// Names only the category report retired.
35
const RETIRED_CATEGORY: [(&str, &str); 8] = [
36
    ("target_commodity_id", "use target"),
37
    ("date_from", "use from"),
38
    ("date_to", "use to"),
39
    ("period_grouping", "use grouping"),
40
    ("tag_name", "use tag"),
41
    ("sort_order", "use sort"),
42
    ("include_uncategorized", "use include-uncategorized"),
43
    ("exclude_uncategorized", "use include-uncategorized"),
44
];
45

            
46
const BALANCE_PATH: &str = "/report/balance";
47
const ACTIVITY_PATH: &str = "/report/activity";
48
const CATEGORY_PATH: &str = "/report/category-breakdown";
49

            
50
233
fn retired_in(path: &str, query: &str) -> Option<(String, &'static str)> {
51
233
    let own: &[(&str, &str)] = match path {
52
233
        p if p.contains(BALANCE_PATH) => &RETIRED_BALANCE,
53
187
        p if p.contains(ACTIVITY_PATH) => &RETIRED_ACTIVITY,
54
116
        p if p.contains(CATEGORY_PATH) => &RETIRED_CATEGORY,
55
15
        _ => &[],
56
    };
57
233
    serde_urlencoded::from_str::<Vec<(String, String)>>(query)
58
233
        .ok()?
59
        .into_iter()
60
440
        .find_map(|(name, _)| {
61
440
            let (_, hint) = RETIRED
62
440
                .iter()
63
440
                .chain(own)
64
3201
                .find(|(retired, _)| *retired == name)?;
65
193
            Some((name, *hint))
66
440
        })
67
233
}
68

            
69
/// Answers an old bookmark with an error: ignoring the parameter would run the
70
/// report unfiltered.
71
109
pub async fn reject_retired_filter(req: Request<Body>, next: Next) -> Response {
72
109
    match req
73
109
        .uri()
74
109
        .query()
75
109
        .and_then(|query| retired_in(req.uri().path(), query))
76
    {
77
        Some((name, hint)) => {
78
93
            WebError::bad_request(format!("{name} is no longer supported; {hint}")).into_response()
79
        }
80
16
        None => next.run(req).await,
81
    }
82
109
}
83

            
84
#[cfg(test)]
85
#[path = "retired_tests.rs"]
86
mod tests;