64 lines
1.3 KiB
Rust
64 lines
1.3 KiB
Rust
use std::str::FromStr;
|
|
|
|
#[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize)]
|
|
pub enum Lang {
|
|
Rus,
|
|
#[allow(dead_code)]
|
|
Eng,
|
|
}
|
|
|
|
impl Default for Lang {
|
|
fn default() -> Self {
|
|
Lang::Rus
|
|
}
|
|
}
|
|
|
|
impl FromStr for Lang {
|
|
type Err = ();
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
match s {
|
|
"Rus" | "rus" => Ok(Lang::Rus),
|
|
"Eng" | "eng" => Ok(Lang::Eng),
|
|
_ => Err(()),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize)]
|
|
pub struct Ingredient {
|
|
pub key: String,
|
|
pub lang: Lang,
|
|
pub name: String,
|
|
}
|
|
|
|
impl From<&db::data::Ingredient> for Ingredient {
|
|
fn from(db: &db::data::Ingredient) -> Self {
|
|
Self::try_from((db, Lang::Rus)).unwrap()
|
|
}
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub enum IngredientValidError {
|
|
TranslationNotFound,
|
|
}
|
|
|
|
impl TryFrom<(&db::data::Ingredient, Lang)> for Ingredient {
|
|
type Error = IngredientValidError;
|
|
|
|
fn try_from((db, lang): (&db::data::Ingredient, Lang)) -> Result<Self, Self::Error> {
|
|
let key = db.key.to_string();
|
|
|
|
let name = match lang {
|
|
Lang::Rus => Some(db.translates.ru),
|
|
Lang::Eng => db.translates.en,
|
|
};
|
|
|
|
if let Some(name) = name.map(String::from) {
|
|
Ok(Self { key, lang, name })
|
|
} else {
|
|
Err(IngredientValidError::TranslationNotFound)
|
|
}
|
|
}
|
|
}
|