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