Calendar (WIP)

This commit is contained in:
Greg Burri 2025-01-29 14:37:25 +01:00
parent 9d3f9e9c60
commit 79a0aeb1b8
24 changed files with 613 additions and 231 deletions

View file

@ -7,3 +7,4 @@ edition = "2021"
[dependencies]
ron = "0.8"
serde = { version = "1.0", features = ["derive"] }
chrono = { version = "0.4", features = ["serde"] }

View file

@ -1,3 +1,4 @@
use chrono::NaiveDate;
use ron::ser::{to_string_pretty, PrettyConfig};
use serde::{Deserialize, Serialize};
@ -16,7 +17,7 @@ pub struct Id {
pub id: i64,
}
///// RECIPE /////
/// RECIPE ///
#[derive(Serialize, Deserialize, Clone)]
pub struct SetRecipeTitle {
@ -158,7 +159,7 @@ pub struct Ingredient {
pub quantity_unit: String,
}
///// PROFILE /////
/// PROFILE ///
#[derive(Serialize, Deserialize, Clone)]
pub struct UpdateProfile {
@ -174,3 +175,11 @@ where
// TODO: handle'unwrap'.
to_string_pretty(&ron, PrettyConfig::new()).unwrap()
}
/// Calendar ///
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct ScheduledRecipes {
// (Scheduled date, recipe title, recipe id).
pub recipes: Vec<(NaiveDate, String, i64)>,
}

View file

@ -12,3 +12,44 @@ pub fn validate_password(password: &str) -> PasswordValidation {
PasswordValidation::Ok
}
}
pub fn substitute(str: &str, pattern: &str, replacements: &[&str]) -> String {
let mut result = String::with_capacity(
(str.len() + replacements.iter().map(|s| s.len()).sum::<usize>())
.saturating_sub(pattern.len() * replacements.len()),
);
let mut i = 0;
for s in str.split(pattern) {
result.push_str(s);
if i < replacements.len() {
result.push_str(replacements[i]);
}
i += 1;
}
if i == 1 {
return str.to_string();
}
result
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_substitute() {
assert_eq!(substitute("", "", &[]), "");
assert_eq!(substitute("", "", &[""]), "");
assert_eq!(substitute("", "{}", &["a"]), "");
assert_eq!(substitute("a", "{}", &["b"]), "a");
assert_eq!(substitute("a{}", "{}", &["b"]), "ab");
assert_eq!(substitute("{}c", "{}", &["b"]), "bc");
assert_eq!(substitute("a{}c", "{}", &["b"]), "abc");
assert_eq!(substitute("{}b{}", "{}", &["a", "c"]), "abc");
assert_eq!(substitute("{}{}{}", "{}", &["a", "bc", "def"]), "abcdef");
assert_eq!(substitute("{}{}{}", "{}", &["a"]), "a");
}
}