recipes/api/src/domain/recipe/types.rs

58 lines
1.4 KiB
Rust

use crate::domain::misc_types::Lang;
#[derive(Debug, Clone, Serialize)]
pub struct Recipe {
key: String,
lang: Lang,
name: String,
instructions: Vec<String>,
ingredients: Vec<RecipeIngredient>,
}
impl From<&db::data::Recipe> for Recipe {
fn from(db: &db::data::Recipe) -> Self {
Self::try_from((db, Lang::Rus)).unwrap()
}
}
impl From<(&db::data::Recipe, Lang)> for Recipe {
fn from((db, lang): (&db::data::Recipe, Lang)) -> Self {
let tr = &db.translates;
let ctr = match lang {
Lang::Rus => &tr.rus,
_ => unimplemented!(),
};
Self {
key: db.key.to_string(),
lang: if ctr.name == tr.rus.name {
Lang::Rus
} else {
lang
},
name: ctr.name.to_string(),
instructions: ctr.instructions.iter().copied().map(String::from).collect(),
ingredients: vec![],
}
}
}
#[derive(Debug, Clone, Serialize)]
pub struct RecipeIngredient {
key: String,
measure: RecipeIngredientMeasure,
}
#[derive(Debug, Clone, Serialize)]
#[serde(tag = "measure", content = "value")]
pub enum RecipeIngredientMeasure {
#[serde(rename = "g")]
Gram(u32),
#[serde(rename = "kg")]
KiloGram(u32),
#[serde(rename = "ml")]
MilliLiter(u32),
#[serde(rename = "l")]
Liter(u32),
}