249 lines
5.1 KiB
Rust
249 lines
5.1 KiB
Rust
use chrono::NaiveDate;
|
|
use ron::ser::{PrettyConfig, to_string_pretty};
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
#[derive(Serialize, Deserialize, Clone)]
|
|
pub struct SetLang {
|
|
pub lang: String,
|
|
}
|
|
|
|
/*** Generic types ***/
|
|
|
|
#[derive(Serialize, Deserialize, Clone)]
|
|
pub struct Ids {
|
|
pub ids: Vec<i64>,
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize, Clone)]
|
|
pub struct Id {
|
|
pub id: i64,
|
|
}
|
|
|
|
// A simple value.
|
|
#[derive(Serialize, Deserialize, Clone)]
|
|
pub struct Value<T> {
|
|
pub value: T,
|
|
}
|
|
|
|
// A value associated with an id.
|
|
#[derive(Serialize, Deserialize, Clone)]
|
|
pub struct KeyValue<T> {
|
|
pub id: i64,
|
|
pub value: T,
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize, Clone)]
|
|
pub struct Strings {
|
|
pub strs: Vec<String>,
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize, Clone)]
|
|
pub struct DateRange {
|
|
pub start_date: NaiveDate,
|
|
pub end_date: NaiveDate,
|
|
}
|
|
|
|
/*** Recipe ***/
|
|
|
|
#[derive(Serialize, Deserialize, Clone)]
|
|
pub struct SetRecipeTitle {
|
|
pub recipe_id: i64,
|
|
pub title: String,
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize, Clone)]
|
|
pub struct SetRecipeDescription {
|
|
pub recipe_id: i64,
|
|
pub description: String,
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize, Clone)]
|
|
pub struct SetRecipeServings {
|
|
pub recipe_id: i64,
|
|
pub servings: Option<u32>,
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize, Clone)]
|
|
pub struct SetRecipeEstimatedTime {
|
|
pub recipe_id: i64,
|
|
pub estimated_time: Option<u32>,
|
|
}
|
|
|
|
#[repr(u32)]
|
|
#[derive(Serialize, Deserialize, Clone, Copy, PartialEq, Debug)]
|
|
pub enum Difficulty {
|
|
Unknown = 0,
|
|
Easy = 1,
|
|
Medium = 2,
|
|
Hard = 3,
|
|
}
|
|
|
|
impl TryFrom<u32> for Difficulty {
|
|
type Error = &'static str;
|
|
fn try_from(value: u32) -> Result<Self, Self::Error> {
|
|
Ok(match value {
|
|
1 => Self::Easy,
|
|
2 => Self::Medium,
|
|
3 => Self::Hard,
|
|
_ => Self::Unknown,
|
|
})
|
|
}
|
|
}
|
|
|
|
impl From<Difficulty> for u32 {
|
|
fn from(value: Difficulty) -> Self {
|
|
value as u32
|
|
}
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize, Clone)]
|
|
pub struct SetRecipeDifficulty {
|
|
pub recipe_id: i64,
|
|
pub difficulty: Difficulty,
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize, Clone)]
|
|
pub struct SetRecipeLanguage {
|
|
pub recipe_id: i64,
|
|
pub lang: String,
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize, Clone)]
|
|
pub struct SetIsPublic {
|
|
pub recipe_id: i64,
|
|
pub is_public: bool,
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize, Clone)]
|
|
pub struct SetGroupName {
|
|
pub group_id: i64,
|
|
pub name: String,
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize, Clone)]
|
|
pub struct SetGroupComment {
|
|
pub group_id: i64,
|
|
pub comment: String,
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize, Clone)]
|
|
pub struct SetStepAction {
|
|
pub step_id: i64,
|
|
pub action: String,
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize, Clone)]
|
|
pub struct SetIngredientName {
|
|
pub ingredient_id: i64,
|
|
pub name: String,
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize, Clone)]
|
|
pub struct SetIngredientComment {
|
|
pub ingredient_id: i64,
|
|
pub comment: String,
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize, Clone)]
|
|
pub struct SetIngredientQuantity {
|
|
pub ingredient_id: i64,
|
|
pub quantity: Option<f64>,
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize, Clone)]
|
|
pub struct SetIngredientUnit {
|
|
pub ingredient_id: i64,
|
|
pub unit: String,
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize, Clone)]
|
|
pub struct Tags {
|
|
pub recipe_id: i64,
|
|
pub tags: Vec<String>,
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
|
pub struct Group {
|
|
pub id: i64,
|
|
pub name: String,
|
|
pub comment: String,
|
|
pub steps: Vec<Step>,
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
|
pub struct Step {
|
|
pub id: i64,
|
|
pub action: String,
|
|
pub ingredients: Vec<Ingredient>,
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
|
pub struct Ingredient {
|
|
pub id: i64,
|
|
pub name: String,
|
|
pub comment: String,
|
|
pub quantity_value: Option<f64>,
|
|
pub quantity_unit: String,
|
|
}
|
|
|
|
/*** PROFILE ***/
|
|
|
|
#[derive(Serialize, Deserialize, Clone)]
|
|
pub struct UpdateProfile {
|
|
pub name: Option<String>,
|
|
pub email: Option<String>,
|
|
pub password: Option<String>,
|
|
}
|
|
|
|
/*** Calendar ***/
|
|
|
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
|
pub struct ScheduledRecipes {
|
|
// (Scheduled date, recipe title, recipe id).
|
|
pub recipes: Vec<(NaiveDate, String, i64)>,
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
|
pub struct ScheduleRecipe {
|
|
pub recipe_id: i64,
|
|
pub date: NaiveDate,
|
|
pub servings: u32,
|
|
pub add_ingredients_to_shopping_list: bool,
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
|
pub enum ScheduleRecipeResult {
|
|
Ok,
|
|
RecipeAlreadyScheduledAtThisDate,
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
|
pub struct RemoveScheduledRecipe {
|
|
pub recipe_id: i64,
|
|
pub date: NaiveDate,
|
|
pub remove_ingredients_from_shopping_list: bool,
|
|
}
|
|
|
|
/*** Shopping list ***/
|
|
|
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
|
pub struct ShoppingListItem {
|
|
pub id: i64,
|
|
pub name: String,
|
|
pub quantity_value: Option<f64>,
|
|
pub quantity_unit: String,
|
|
pub recipe_id: Option<i64>,
|
|
pub recipe_title: Option<String>,
|
|
pub date: Option<NaiveDate>,
|
|
pub is_checked: bool,
|
|
}
|
|
|
|
/*** Misc ***/
|
|
|
|
pub fn to_string<T>(ron: T) -> String
|
|
where
|
|
T: Serialize,
|
|
{
|
|
// TODO: handle'unwrap'.
|
|
to_string_pretty(&ron, PrettyConfig::new()).unwrap()
|
|
}
|