api: add context, some style improvements

This commit is contained in:
Dmitriy Pleshevskiy 2022-05-15 15:50:09 +03:00
parent 8c0a60a4e8
commit 284ad260a4
13 changed files with 163 additions and 145 deletions

View File

@ -1,10 +1,12 @@
use super::types; use super::types;
use crate::domain::misc_types; use crate::{
use crate::repo::ingredient::IngredientRepo; domain::{Context, Lang},
repo::ingredient::IngredientRepo,
};
#[derive(Default, Debug)] #[derive(Default, Debug)]
pub struct RequestOpts { pub struct RequestOpts {
pub lang: Option<misc_types::Lang>, pub lang: Option<Lang>,
} }
pub enum ResponseError { pub enum ResponseError {
@ -13,10 +15,10 @@ pub enum ResponseError {
pub fn execute( pub fn execute(
repo: &impl IngredientRepo, repo: &impl IngredientRepo,
ctx: &Context,
key: String, key: String,
opts: RequestOpts,
) -> Result<types::Ingredient, ResponseError> { ) -> Result<types::Ingredient, ResponseError> {
match repo.get_ingredient_opt(key, opts.into()) { match repo.get_ingredient_opt(ctx, key) {
Some(ing) => Ok(ing), Some(ing) => Ok(ing),
_ => Err(ResponseError::NotFound), _ => Err(ResponseError::NotFound),
} }
@ -25,12 +27,15 @@ pub fn execute(
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::{domain::misc_types::Lang, repo::ingredient::InMemoryIngredientRepo}; use crate::{
domain::{Context, Lang},
repo::ingredient::InMemoryIngredientRepo,
};
#[test] #[test]
fn should_return_ingredient() { fn should_return_ingredient() {
let repo = InMemoryIngredientRepo::new(); let repo = InMemoryIngredientRepo::new();
let res = execute(&repo, String::from("apple"), RequestOpts::default()); let res = execute(&repo, &Context::default(), String::from("apple"));
match res { match res {
Ok(apple) => { Ok(apple) => {
@ -45,13 +50,7 @@ mod tests {
#[test] #[test]
fn should_return_ingredient_with_choosed_lang() { fn should_return_ingredient_with_choosed_lang() {
let repo = InMemoryIngredientRepo::new(); let repo = InMemoryIngredientRepo::new();
let res = execute( let res = execute(&repo, &Context::eng(), String::from("apple"));
&repo,
String::from("apple"),
RequestOpts {
lang: Some(Lang::Eng),
},
);
match res { match res {
Ok(apple) => { Ok(apple) => {
@ -66,13 +65,7 @@ mod tests {
#[test] #[test]
fn should_return_ingredient_with_fallback_lang() { fn should_return_ingredient_with_fallback_lang() {
let repo = InMemoryIngredientRepo::new(); let repo = InMemoryIngredientRepo::new();
let res = execute( let res = execute(&repo, &Context::eng(), String::from("orange"));
&repo,
String::from("orange"),
RequestOpts {
lang: Some(Lang::Eng),
},
);
match res { match res {
Ok(orange) => { Ok(orange) => {
@ -87,7 +80,7 @@ mod tests {
#[test] #[test]
fn should_throw_not_found_error() { fn should_throw_not_found_error() {
let repo = InMemoryIngredientRepo::new(); let repo = InMemoryIngredientRepo::new();
let res = execute(&repo, String::from("wildberries"), RequestOpts::default()); let res = execute(&repo, &Context::default(), String::from("wildberries"));
match res { match res {
Err(ResponseError::NotFound) => {} Err(ResponseError::NotFound) => {}

View File

@ -1,15 +1,18 @@
use super::types; use super::types;
use crate::domain::misc_types; use crate::domain::misc_types::Context;
use crate::repo::ingredient::IngredientRepo; use crate::repo::ingredient::IngredientRepo;
#[derive(Default, Debug)] #[derive(Default, Debug)]
pub struct RequestOpts { pub struct RequestOpts {
pub lang: Option<misc_types::Lang>,
pub keys: Option<Vec<String>>, pub keys: Option<Vec<String>>,
} }
pub fn execute(repo: &impl IngredientRepo, opts: RequestOpts) -> Vec<types::Ingredient> { pub fn execute(
repo.get_ingredients(opts.into()) repo: &impl IngredientRepo,
ctx: &Context,
opts: RequestOpts,
) -> Vec<types::Ingredient> {
repo.get_ingredients(ctx, opts.into())
} }
#[cfg(test)] #[cfg(test)]
@ -20,7 +23,7 @@ mod tests {
#[test] #[test]
fn should_return_all_ingredients() { fn should_return_all_ingredients() {
let repo = InMemoryIngredientRepo::new(); let repo = InMemoryIngredientRepo::new();
let res = execute(&repo, RequestOpts::default()); let res = execute(&repo, &Context::default(), RequestOpts::default());
match res.as_slice() { match res.as_slice() {
[banana, apple, orange, salt, sugar] => { [banana, apple, orange, salt, sugar] => {
@ -51,13 +54,7 @@ mod tests {
#[test] #[test]
fn should_return_preferred_lang_with_fallback() { fn should_return_preferred_lang_with_fallback() {
let repo = InMemoryIngredientRepo::new(); let repo = InMemoryIngredientRepo::new();
let res = execute( let res = execute(&repo, &Context::eng(), RequestOpts::default());
&repo,
RequestOpts {
lang: Some(Lang::Eng),
..RequestOpts::default()
},
);
match res.as_slice() { match res.as_slice() {
[banana, apple, orange, salt, sugar] => { [banana, apple, orange, salt, sugar] => {
@ -90,9 +87,9 @@ mod tests {
let repo = InMemoryIngredientRepo::new(); let repo = InMemoryIngredientRepo::new();
let res = execute( let res = execute(
&repo, &repo,
&Context::default(),
RequestOpts { RequestOpts {
keys: Some(vec![String::from("apple")]), keys: Some(vec![String::from("apple")]),
..RequestOpts::default()
}, },
); );

View File

@ -1,6 +1,18 @@
use std::str::FromStr; use std::str::FromStr;
#[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize)] #[derive(Debug, Default)]
pub struct Context {
pub lang: Lang,
}
#[cfg(test)]
impl Context {
pub fn eng() -> Self {
Self { lang: Lang::Eng }
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize)]
pub enum Lang { pub enum Lang {
Rus, Rus,
#[allow(dead_code)] #[allow(dead_code)]

View File

@ -1,3 +1,5 @@
pub mod ingredient; pub mod ingredient;
pub mod misc_types; pub mod misc_types;
pub mod recipe; pub mod recipe;
pub use misc_types::{Context, Lang};

View File

@ -1,23 +1,23 @@
use crate::repo::recipe::RecipeRepo; use crate::{domain::Context, repo::recipe::RecipeRepo};
use super::types; use super::types;
pub fn execute(repo: &impl RecipeRepo) -> Vec<types::Recipe> { pub fn execute(repo: &impl RecipeRepo, ctx: &Context) -> Vec<types::Recipe> {
repo.get_recipes() repo.get_recipes(ctx)
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::{ use crate::{
domain::{misc_types::Lang, recipe::types::RecipeIngredientMeasure}, domain::{recipe::types::RecipeIngredientMeasure, Lang},
repo::recipe::InMemoryRecipeRepo, repo::recipe::InMemoryRecipeRepo,
}; };
#[test] #[test]
fn should_return_all_recipes() { fn should_return_all_recipes() {
let repo = InMemoryRecipeRepo::new(); let repo = InMemoryRecipeRepo::new();
let res = execute(&repo); let res = execute(&repo, &Context::default());
match res.as_slice() { match res.as_slice() {
[salad] => { [salad] => {
@ -52,7 +52,8 @@ mod tests {
#[test] #[test]
fn should_not_return_recipes_if_ingredients_not_exist_in_db() { fn should_not_return_recipes_if_ingredients_not_exist_in_db() {
let mut repo = InMemoryRecipeRepo::new().with_no_ingredients_found(); let mut repo = InMemoryRecipeRepo::new().with_no_ingredients_found();
let res = execute(&repo); let ctx = Context::default();
let res = execute(&repo, &ctx);
match res.as_slice() { match res.as_slice() {
[salad] => { [salad] => {
@ -65,7 +66,7 @@ mod tests {
rec.ingredients.pop(); // remove wheat flour from ingredients rec.ingredients.pop(); // remove wheat flour from ingredients
} }
let res = execute(&repo); let res = execute(&repo, &ctx);
match res.as_slice() { match res.as_slice() {
[salad, no_found] => { [salad, no_found] => {
assert_eq!(salad.key, String::from("fruit_salad")); assert_eq!(salad.key, String::from("fruit_salad"));

View File

@ -1,5 +1,5 @@
use crate::domain::ingredient::{fetch_by_key, fetch_list, types}; use crate::domain::ingredient::{fetch_by_key, fetch_list, types};
use crate::domain::misc_types; use crate::domain::misc_types::{self, Context};
#[derive(Default)] #[derive(Default)]
pub struct GetIngredientOpts { pub struct GetIngredientOpts {
@ -14,42 +14,33 @@ impl From<fetch_by_key::RequestOpts> for GetIngredientOpts {
#[derive(Default)] #[derive(Default)]
pub struct GetIngredientsOpts { pub struct GetIngredientsOpts {
pub lang: Option<misc_types::Lang>,
pub keys: Option<Vec<String>>, pub keys: Option<Vec<String>>,
} }
impl From<fetch_list::RequestOpts> for GetIngredientsOpts { impl From<fetch_list::RequestOpts> for GetIngredientsOpts {
fn from(app: fetch_list::RequestOpts) -> Self { fn from(app: fetch_list::RequestOpts) -> Self {
Self { Self { keys: app.keys }
lang: app.lang,
keys: app.keys,
}
} }
} }
pub trait IngredientRepo { pub trait IngredientRepo {
fn get_ingredient_opt(&self, key: String, opts: GetIngredientOpts) fn get_ingredient_opt(&self, ctx: &Context, key: String) -> Option<types::Ingredient>;
-> Option<types::Ingredient>;
fn get_ingredients(&self, opts: GetIngredientsOpts) -> Vec<types::Ingredient>; fn get_ingredients(&self, ctx: &Context, opts: GetIngredientsOpts) -> Vec<types::Ingredient>;
} }
pub struct StaticIngredientRepo; pub struct StaticIngredientRepo;
impl IngredientRepo for StaticIngredientRepo { impl IngredientRepo for StaticIngredientRepo {
fn get_ingredient_opt( fn get_ingredient_opt(&self, ctx: &Context, key: String) -> Option<types::Ingredient> {
&self,
key: String,
opts: GetIngredientOpts,
) -> Option<types::Ingredient> {
db::INGREDIENTS db::INGREDIENTS
.iter() .iter()
.find(|ing| ing.key == &key) .find(|ing| ing.key == &key)
.map(|ing| types::Ingredient::from((ing, opts.lang.unwrap_or_default()))) .map(|ing| types::Ingredient::from((ing, ctx.lang)))
} }
fn get_ingredients(&self, opts: GetIngredientsOpts) -> Vec<types::Ingredient> { fn get_ingredients(&self, ctx: &Context, opts: GetIngredientsOpts) -> Vec<types::Ingredient> {
let langs = [opts.lang.unwrap_or_default()].repeat(db::INGREDIENTS.len()); let langs = [ctx.lang].repeat(db::INGREDIENTS.len());
db::INGREDIENTS db::INGREDIENTS
.iter() .iter()
.zip(langs) .zip(langs)
@ -111,19 +102,15 @@ impl InMemoryIngredientRepo {
#[cfg(test)] #[cfg(test)]
impl IngredientRepo for InMemoryIngredientRepo { impl IngredientRepo for InMemoryIngredientRepo {
fn get_ingredient_opt( fn get_ingredient_opt(&self, ctx: &Context, key: String) -> Option<types::Ingredient> {
&self,
key: String,
opts: GetIngredientOpts,
) -> Option<types::Ingredient> {
self.ingredients self.ingredients
.iter() .iter()
.find(|ing| ing.key == &key) .find(|ing| ing.key == &key)
.map(|ing| types::Ingredient::from((ing, opts.lang.unwrap_or_default()))) .map(|ing| types::Ingredient::from((ing, ctx.lang)))
} }
fn get_ingredients(&self, opts: GetIngredientsOpts) -> Vec<types::Ingredient> { fn get_ingredients(&self, ctx: &Context, opts: GetIngredientsOpts) -> Vec<types::Ingredient> {
let langs = [opts.lang.unwrap_or_default()].repeat(self.ingredients.len()); let langs = [ctx.lang].repeat(self.ingredients.len());
self.ingredients self.ingredients
.iter() .iter()
.zip(langs) .zip(langs)

View File

@ -1,23 +1,25 @@
use crate::domain::{misc_types::Lang, recipe::types}; use crate::domain::{recipe::types, Context};
use crate::repo; use crate::repo;
use super::ingredient::IngredientRepo; use super::ingredient::IngredientRepo;
#[cfg(test)]
use db::data as Db; use db::data as Db;
use Db::RecipeIngredientMeasure as DbRIM; #[cfg(test)]
use db::data::RecipeIngredientMeasure as DbRIM;
pub trait RecipeRepo { pub trait RecipeRepo {
fn get_recipes(&self) -> Vec<types::Recipe>; fn get_recipes(&self, ctx: &Context) -> Vec<types::Recipe>;
} }
pub struct StaticRecipeRepo; pub struct StaticRecipeRepo;
impl RecipeRepo for StaticRecipeRepo { impl RecipeRepo for StaticRecipeRepo {
fn get_recipes(&self) -> Vec<types::Recipe> { fn get_recipes(&self, ctx: &Context) -> Vec<types::Recipe> {
let ings_repo = repo::ingredient::StaticIngredientRepo; let ings_repo = repo::ingredient::StaticIngredientRepo;
let ings = ings_repo.get_ingredients(Default::default()); let ings = ings_repo.get_ingredients(ctx, Default::default());
let langs = [Lang::default()].repeat(db::RECIPES.len()); let langs = [ctx.lang].repeat(db::RECIPES.len());
db::RECIPES db::RECIPES
.iter() .iter()
.zip(langs) .zip(langs)
@ -99,11 +101,11 @@ impl InMemoryRecipeRepo {
#[cfg(test)] #[cfg(test)]
impl RecipeRepo for InMemoryRecipeRepo { impl RecipeRepo for InMemoryRecipeRepo {
fn get_recipes(&self) -> Vec<types::Recipe> { fn get_recipes(&self, ctx: &Context) -> Vec<types::Recipe> {
let ings_repo = repo::ingredient::InMemoryIngredientRepo::new(); let ings_repo = repo::ingredient::InMemoryIngredientRepo::new();
let ings = ings_repo.get_ingredients(Default::default()); let ings = ings_repo.get_ingredients(ctx, Default::default());
let langs = [Lang::default()].repeat(self.recipes.len()); let langs = [ctx.lang].repeat(self.recipes.len());
self.recipes self.recipes
.iter() .iter()
.zip(langs) .zip(langs)

34
api/src/rest/context.rs Normal file
View File

@ -0,0 +1,34 @@
use std::str::FromStr;
use crate::domain;
use super::types::QueryParams;
#[derive(Debug, Default, Clone)]
pub struct Context {
lang: Option<String>,
}
impl<'a> From<&QueryParams<'a>> for Context {
fn from(params: &QueryParams<'a>) -> Self {
params.iter().fold(Context::default(), |mut opts, p| {
match p {
("lang", val) => opts.lang = Some(String::from(*val)),
_ => {}
};
opts
})
}
}
impl From<Context> for domain::Context {
fn from(rest: Context) -> Self {
Self {
lang: rest
.lang
.and_then(|l| domain::Lang::from_str(&l).ok())
.unwrap_or_default(),
}
}
}

View File

@ -3,24 +3,21 @@ use std::str::FromStr;
use tiny_http::{Header, Response}; use tiny_http::{Header, Response};
use crate::domain;
use crate::domain::misc_types::Lang;
use crate::repo::ingredient::StaticIngredientRepo; use crate::repo::ingredient::StaticIngredientRepo;
use crate::rest::types::{QueryParams, Url}; use crate::rest::types::{QueryParams, Url};
use crate::{domain, rest};
#[derive(Default, Debug)] #[derive(Default, Debug)]
struct FetchIngredientsOpts<'a> { struct FetchIngredientsOpts<'a> {
lang: Option<&'a str>,
keys: Option<Vec<&'a str>>, keys: Option<Vec<&'a str>>,
} }
impl<'a> From<QueryParams<'a>> for FetchIngredientsOpts<'a> { impl<'a> From<&QueryParams<'a>> for FetchIngredientsOpts<'a> {
fn from(params: QueryParams<'a>) -> Self { fn from(params: &QueryParams<'a>) -> Self {
params params
.into_iter() .iter()
.fold(FetchIngredientsOpts::default(), |mut opts, p| { .fold(FetchIngredientsOpts::default(), |mut opts, p| {
match p { match p {
("lang", val) => opts.lang = Some(val),
("key", val) => { ("key", val) => {
let mut keys = opts.keys.unwrap_or_default(); let mut keys = opts.keys.unwrap_or_default();
keys.push(val); keys.push(val);
@ -36,61 +33,35 @@ impl<'a> From<QueryParams<'a>> for FetchIngredientsOpts<'a> {
impl From<FetchIngredientsOpts<'_>> for domain::ingredient::fetch_list::RequestOpts { impl From<FetchIngredientsOpts<'_>> for domain::ingredient::fetch_list::RequestOpts {
fn from(rest: FetchIngredientsOpts) -> Self { fn from(rest: FetchIngredientsOpts) -> Self {
let lang = rest.lang.and_then(|l| Lang::from_str(l).ok());
let keys = rest let keys = rest
.keys .keys
.map(|keys| keys.into_iter().map(String::from).collect()); .map(|keys| keys.into_iter().map(String::from).collect());
Self { lang, keys } Self { keys }
} }
} }
pub fn fetch_list(url: &Url) -> Response<Cursor<Vec<u8>>> { pub fn fetch_list(rest_ctx: &rest::Context, url: &Url) -> Response<Cursor<Vec<u8>>> {
use domain::ingredient::fetch_list; use domain::ingredient::fetch_list;
let opts = FetchIngredientsOpts::from(url.query_params()); let opts = FetchIngredientsOpts::from(url.query_params());
let ctx = rest_ctx.clone().into();
let repo = StaticIngredientRepo; let repo = StaticIngredientRepo;
let ingredients = fetch_list::execute(&repo, opts.into()); let ingredients = fetch_list::execute(&repo, &ctx, opts.into());
let data = serde_json::to_string(&ingredients).unwrap(); let data = serde_json::to_string(&ingredients).unwrap();
Response::from_string(data) Response::from_string(data)
.with_header(Header::from_str("content-type: application/json").unwrap()) .with_header(Header::from_str("content-type: application/json").unwrap())
} }
#[derive(Default, Debug)] pub fn fetch_by_key(rest_ctx: &rest::Context, _url: &Url, key: &str) -> Response<Cursor<Vec<u8>>> {
struct FetchIngredientOpts<'a> {
lang: Option<&'a str>,
}
impl<'a> From<QueryParams<'a>> for FetchIngredientOpts<'a> {
fn from(params: QueryParams<'a>) -> Self {
params
.into_iter()
.fold(FetchIngredientOpts::default(), |mut opts, p| {
match p {
("lang", val) => opts.lang = Some(val),
_ => {}
};
opts
})
}
}
impl From<FetchIngredientOpts<'_>> for domain::ingredient::fetch_by_key::RequestOpts {
fn from(rest: FetchIngredientOpts) -> Self {
let lang = rest.lang.and_then(|l| Lang::from_str(l).ok());
Self { lang }
}
}
pub fn fetch_by_key(url: &Url, key: &str) -> Response<Cursor<Vec<u8>>> {
use domain::ingredient::fetch_by_key; use domain::ingredient::fetch_by_key;
let opts = FetchIngredientOpts::from(url.query_params());
let repo = StaticIngredientRepo; let repo = StaticIngredientRepo;
let ctx = rest_ctx.clone().into();
// TODO: catch notfound error // TODO: catch notfound error
let ingredient = fetch_by_key::execute(&repo, key.to_string(), opts.into()).ok(); let ingredient = fetch_by_key::execute(&repo, &ctx, key.to_string()).ok();
let data = serde_json::to_string(&ingredient).unwrap(); let data = serde_json::to_string(&ingredient).unwrap();
Response::from_string(data) Response::from_string(data)

View File

@ -5,13 +5,16 @@ use tiny_http::{Header, Response};
use crate::repo::recipe::StaticRecipeRepo; use crate::repo::recipe::StaticRecipeRepo;
use crate::rest::types::Url; use crate::rest::types::Url;
use crate::{domain, rest};
pub fn fetch_list(url: &Url) -> Response<Cursor<Vec<u8>>> { pub fn fetch_list(rest_ctx: &rest::Context, _url: &Url) -> Response<Cursor<Vec<u8>>> {
use crate::domain::recipe::fetch_list; use domain::recipe::fetch_list;
let repo = StaticRecipeRepo; let repo = StaticRecipeRepo;
let ctx = rest_ctx.clone().into();
// TODO: catch notfound error // TODO: catch notfound error
let recipe = fetch_list::execute(&repo); let recipe = fetch_list::execute(&repo, &ctx);
let data = serde_json::to_string(&recipe).unwrap(); let data = serde_json::to_string(&recipe).unwrap();
Response::from_string(data) Response::from_string(data)

View File

@ -1,3 +1,6 @@
pub mod context;
pub mod ctrl; pub mod ctrl;
pub mod server; pub mod server;
pub mod types; pub mod types;
pub use context::Context;

View File

@ -17,17 +17,18 @@ pub fn start() {
handles.push(thread::spawn(move || { handles.push(thread::spawn(move || {
for rq in server.incoming_requests() { for rq in server.incoming_requests() {
let url = Url::parse(rq.url()); let url = Url::parse(rq.url());
let _ = match url.path_segments()[..] { let ctx = rest::Context::from(url.query_params());
let _ = match url.path_segments() {
["api", "ingredients"] => { ["api", "ingredients"] => {
let res = rest::ctrl::ingredient::fetch_list(&url); let res = rest::ctrl::ingredient::fetch_list(&ctx, &url);
rq.respond(res) rq.respond(res)
} }
["api", "ingredients", key] => { ["api", "ingredients", key] => {
let res = rest::ctrl::ingredient::fetch_by_key(&url, key); let res = rest::ctrl::ingredient::fetch_by_key(&ctx, &url, key);
rq.respond(res) rq.respond(res)
} }
["api", "recipes"] => { ["api", "recipes"] => {
let res = rest::ctrl::recipe::fetch_list(&url); let res = rest::ctrl::recipe::fetch_list(&ctx, &url);
rq.respond(res) rq.respond(res)
} }
_ => rq.respond(Response::from_string("Not found")), _ => rq.respond(Response::from_string("Not found")),

View File

@ -1,7 +1,7 @@
#[derive(Debug)] #[derive(Debug)]
pub struct Url<'a> { pub struct Url<'a> {
path: &'a str, path_segments: Vec<&'a str>,
query: Option<&'a str>, query_params: QueryParams<'a>,
} }
pub type QueryParam<'a> = (&'a str, &'a str); pub type QueryParam<'a> = (&'a str, &'a str);
@ -15,26 +15,38 @@ impl Url<'_> {
let path = parts.next().unwrap_or_default(); let path = parts.next().unwrap_or_default();
let query = parts.next(); let query = parts.next();
Url { path, query } Url {
path_segments: extract_path_segments(path),
query_params: extract_query_params(query),
}
} }
pub fn path_segments(&self) -> Vec<&str> { pub fn path_segments(&self) -> &[&str] {
self.path.split('/').skip(1).collect() self.path_segments.as_slice()
} }
pub fn query_params(&self) -> Vec<QueryParam> { pub fn query_params(&self) -> &QueryParams {
self.query &self.query_params
.map(|q| {
q.split('&')
.filter_map(|part| {
if let [key, val] = part.splitn(2, '=').collect::<Vec<&str>>()[..] {
Some((key, val))
} else {
None
}
})
.collect()
})
.unwrap_or_default()
} }
} }
fn extract_path_segments(path: &str) -> Vec<&str> {
path.split('/').skip(1).collect()
}
fn extract_query_params(query: Option<&str>) -> Vec<QueryParam> {
query
.clone()
.map(|q| {
q.split('&')
.filter_map(|part| {
if let [key, val] = part.splitn(2, '=').collect::<Vec<&str>>()[..] {
Some((key, val))
} else {
None
}
})
.collect()
})
.unwrap_or_default()
}