Skip to main content

web/pages/report/
retired.rs

1//! Query parameters the report endpoints no longer read.
2
3use axum::{
4    body::Body, extract::Request, middleware::Next, response::IntoResponse, response::Response,
5};
6
7use crate::response::WebError;
8
9const GROUPS_HINT: &str = "use groups.N.name, groups.N.filter and groups.N.flip";
10
11/// Retired parameter and what replaces it.
12const 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.
20const 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.
27const 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.
35const 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
46const BALANCE_PATH: &str = "/report/balance";
47const ACTIVITY_PATH: &str = "/report/activity";
48const CATEGORY_PATH: &str = "/report/category-breakdown";
49
50fn retired_in(path: &str, query: &str) -> Option<(String, &'static str)> {
51    let own: &[(&str, &str)] = match path {
52        p if p.contains(BALANCE_PATH) => &RETIRED_BALANCE,
53        p if p.contains(ACTIVITY_PATH) => &RETIRED_ACTIVITY,
54        p if p.contains(CATEGORY_PATH) => &RETIRED_CATEGORY,
55        _ => &[],
56    };
57    serde_urlencoded::from_str::<Vec<(String, String)>>(query)
58        .ok()?
59        .into_iter()
60        .find_map(|(name, _)| {
61            let (_, hint) = RETIRED
62                .iter()
63                .chain(own)
64                .find(|(retired, _)| *retired == name)?;
65            Some((name, *hint))
66        })
67}
68
69/// Answers an old bookmark with an error: ignoring the parameter would run the
70/// report unfiltered.
71pub async fn reject_retired_filter(req: Request<Body>, next: Next) -> Response {
72    match req
73        .uri()
74        .query()
75        .and_then(|query| retired_in(req.uri().path(), query))
76    {
77        Some((name, hint)) => {
78            WebError::bad_request(format!("{name} is no longer supported; {hint}")).into_response()
79        }
80        None => next.run(req).await,
81    }
82}
83
84#[cfg(test)]
85#[path = "retired_tests.rs"]
86mod tests;