thruster/middleware/
query_params.rs1use std::collections::HashMap;
2use thruster_proc::middleware_fn;
3
4use crate::core::context::Context;
5use crate::core::{MiddlewareNext, MiddlewareResult};
6
7pub trait HasQueryParams {
8 fn set_query_params(&mut self, query_params: HashMap<String, String>);
9}
10
11#[middleware_fn(_internal)]
12pub async fn query_params<T: 'static + Context + HasQueryParams + Send>(
13 mut context: T,
14 next: MiddlewareNext<T>,
15) -> MiddlewareResult<T> {
16 let mut query_param_hash = HashMap::new();
17
18 {
19 let route: &str = &context.route();
20
21 let mut iter = route.split('?');
22 let _ = iter.next().unwrap();
24
25 if let Some(query_string) = iter.next() {
26 for query_piece in query_string.split('&') {
27 let mut query_iterator = query_piece.split('=');
28 let key = query_iterator.next().unwrap().to_owned();
29
30 match query_iterator.next() {
31 Some(val) => query_param_hash.insert(key, val.to_owned()),
32 None => query_param_hash.insert(key, "true".to_owned()),
33 };
34 }
35 }
36 }
37
38 context.set_query_params(query_param_hash);
39
40 next(context).await
41}