recipes/api/src/repo/ingredient.rs

79 lines
2.3 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

use crate::domain::ingredient::types;
#[derive(Default)]
pub struct GetIngredientOpts {
pub lang: Option<types::Lang>,
}
pub trait IngredientRepo {
fn get_ingredients(&self, opts: GetIngredientOpts) -> Vec<types::Ingredient>;
}
pub struct StaticIngredientRepo {}
impl IngredientRepo for StaticIngredientRepo {
fn get_ingredients(&self, opts: GetIngredientOpts) -> Vec<types::Ingredient> {
let langs = [opts.lang.unwrap_or_default()].repeat(db::INGREDIENTS.len());
db::INGREDIENTS
.iter()
.zip(langs)
.filter_map(|tup| TryFrom::try_from(tup).ok())
.collect::<Vec<_>>()
}
}
#[cfg(test)]
pub struct InMemoryIngredientRepo {
pub ingredients: Vec<db::data::Ingredient>,
}
#[cfg(test)]
impl InMemoryIngredientRepo {
pub fn new() -> Self {
Self {
ingredients: vec![
db::data::Ingredient {
key: "apple",
translates: db::data::IngredientTranslate {
ru: "Яблоко",
en: Some("Apple"),
},
},
db::data::Ingredient {
key: "orange",
translates: db::data::IngredientTranslate {
ru: "Апельсин",
en: None,
},
},
db::data::Ingredient {
key: "salt",
translates: db::data::IngredientTranslate {
ru: "Соль",
en: Some("Salt"),
},
},
db::data::Ingredient {
key: "sugar",
translates: db::data::IngredientTranslate {
ru: "Сахар",
en: None,
},
},
],
}
}
}
#[cfg(test)]
impl IngredientRepo for InMemoryIngredientRepo {
fn get_ingredients(&self, opts: GetIngredientOpts) -> Vec<types::Ingredient> {
let langs = [opts.lang.unwrap_or_default()].repeat(self.ingredients.len());
self.ingredients
.iter()
.zip(langs)
.filter_map(|tup| TryFrom::try_from(tup).ok())
.collect::<Vec<_>>()
}
}