pub const CONTEXT_KEY: &str = "reminder_message";
pub fn context_for_message(message: &str) -> String {
serde_json::json!({ CONTEXT_KEY: message.trim() }).to_string()
}
pub fn message_from_context(context: Option<&str>) -> Option<String> {
let value: serde_json::Value = serde_json::from_str(context?).ok()?;
let message = value.get(CONTEXT_KEY)?.as_str()?.trim();
(!message.is_empty()).then(|| message.to_string())
}
pub fn fire_text_for_goal(context: Option<&str>) -> Option<String> {
message_from_context(context).map(|message| format!("⏰ Reminder: {message}"))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn only_the_typed_message_makes_a_goal_a_reminder() {
let context = context_for_message("Check the oven.");
assert_eq!(
fire_text_for_goal(Some(&context)).as_deref(),
Some("⏰ Reminder: Check the oven.")
);
assert_eq!(fire_text_for_goal(None), None);
assert_eq!(fire_text_for_goal(Some("{\"other\":1}")), None);
assert_eq!(fire_text_for_goal(Some("not json")), None);
}
}