Toast message when scheduling a recipe

This commit is contained in:
Greg Burri 2025-02-05 15:44:48 +01:00
parent fbef990022
commit ccb1248da3
11 changed files with 168 additions and 66 deletions

View file

@ -183,6 +183,12 @@ pub struct ScheduleRecipe {
pub servings: u32,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub enum ScheduleRecipeResult {
Ok,
RecipeAlreadyScheduledAtThisDate,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct ScheduledRecipe {
pub recipe_id: i64,

View file

@ -35,6 +35,18 @@ pub fn substitute(str: &str, pattern: &str, replacements: &[&str]) -> String {
result
}
/// Example: substitute_with_names("{hello}, {world}!", &["{hello}", "{world"], &["Hello", "World"])
pub fn substitute_with_names(str: &str, names: &[&str], replacements: &[&str]) -> String {
let mut result = str.to_string();
for (i, name) in names.iter().enumerate() {
if i >= replacements.len() {
break;
}
result = result.replace(name, replacements[i]);
}
result
}
#[cfg(test)]
mod tests {
use super::*;
@ -52,4 +64,36 @@ mod tests {
assert_eq!(substitute("{}{}{}", "{}", &["a", "bc", "def"]), "abcdef");
assert_eq!(substitute("{}{}{}", "{}", &["a"]), "a");
}
#[test]
fn test_substitute_with_names() {
assert_eq!(
substitute_with_names("{hello}, {world}!", &["{hello}"], &["Hello", "World"]),
"Hello, {world}!"
);
assert_eq!(
substitute_with_names("{hello}, {world}!", &[], &["Hello", "World"]),
"{hello}, {world}!"
);
assert_eq!(
substitute_with_names("{hello}, {world}!", &["{hello}", "{world}"], &["Hello"]),
"Hello, {world}!"
);
assert_eq!(
substitute_with_names("{hello}, {world}!", &["{hello}", "{world}"], &[]),
"{hello}, {world}!"
);
assert_eq!(
substitute_with_names(
"{hello}, {world}!",
&["{hello}", "{world}"],
&["Hello", "World"]
),
"Hello, World!"
);
}
}