Lines
100 %
Functions
50 %
Branches
//! Query parameters the report endpoints no longer read.
use axum::{
body::Body, extract::Request, middleware::Next, response::IntoResponse, response::Response,
};
use crate::response::WebError;
const GROUPS_HINT: &str = "use groups.N.name, groups.N.filter and groups.N.flip";
/// Retired parameter and what replaces it.
const RETIRED: [(&str, &str); 4] = [
("tag_filters", "use filter"),
("tag_filter_mode", "use filter"),
("groups", GROUPS_HINT),
("groups_mode", GROUPS_HINT),
];
/// Names only the balance report retired.
const RETIRED_BALANCE: [(&str, &str); 3] = [
("target_commodity_id", "use target"),
("date_from", "use from"),
("as_of", "use as-of"),
/// Names only the activity report retired.
const RETIRED_ACTIVITY: [(&str, &str); 4] = [
("date_to", "use to"),
("period_grouping", "use grouping"),
/// Names only the category report retired.
const RETIRED_CATEGORY: [(&str, &str); 8] = [
("tag_name", "use tag"),
("sort_order", "use sort"),
("include_uncategorized", "use include-uncategorized"),
("exclude_uncategorized", "use include-uncategorized"),
const BALANCE_PATH: &str = "/report/balance";
const ACTIVITY_PATH: &str = "/report/activity";
const CATEGORY_PATH: &str = "/report/category-breakdown";
fn retired_in(path: &str, query: &str) -> Option<(String, &'static str)> {
let own: &[(&str, &str)] = match path {
p if p.contains(BALANCE_PATH) => &RETIRED_BALANCE,
p if p.contains(ACTIVITY_PATH) => &RETIRED_ACTIVITY,
p if p.contains(CATEGORY_PATH) => &RETIRED_CATEGORY,
_ => &[],
serde_urlencoded::from_str::<Vec<(String, String)>>(query)
.ok()?
.into_iter()
.find_map(|(name, _)| {
let (_, hint) = RETIRED
.iter()
.chain(own)
.find(|(retired, _)| *retired == name)?;
Some((name, *hint))
})
}
/// Answers an old bookmark with an error: ignoring the parameter would run the
/// report unfiltered.
pub async fn reject_retired_filter(req: Request<Body>, next: Next) -> Response {
match req
.uri()
.query()
.and_then(|query| retired_in(req.uri().path(), query))
{
Some((name, hint)) => {
WebError::bad_request(format!("{name} is no longer supported; {hint}")).into_response()
None => next.run(req).await,
#[cfg(test)]
#[path = "retired_tests.rs"]
mod tests;