I'm using rocket to build a search method that finds values from my redis cache. A redis cache entry looks like this:
{
"key": "40:a3:cc:a3:dc:43",
"value": {
"token": "2a256356",
"change": "new",
"data": {
{
"dns": "www.google.de",
"mac": "40:a3:cc:a3:dc:43",
"src": "10.42.0.177",
"dst": "10.42.0.1"
},
{
...
},
}
}
}
I'm currently using the get()
function to find the values from my JSON object like this:
pub fn filter(search: SearchParams, id: &str) -> Vec<Data> {
let user: Data = get_user(store, id);
let SearchParams { value, done } = search;
let data = Data {
key: id.to_string(),
value: user.value.get(&value.to_lowercase()).unwrap().clone(),
done: done.unwrap()
};
let mut data_vec: Vec<Data> = Vec::new();
data_vec.push(data);
data_vec
}
Is there a "cleaner" or better way to do this? My thoughts go into a way using State
to store the user and then filtering the value with .iter().filter()
. But this does not work. If I try to use this then I always get the complete JSON object instead of the specific searched keys.
store
.lock()
.unwrap()
.iter()
.filter(|data|{
data.value.get(&value.to_lowercase()).is_some()
&& done.map(|done| done == user.done).unwrap_or(true)
})
.cloned()
.collect();