use serde::{Deserialize, Serialize};
pub use byob_types::routine::{MAX_ID_CHARS, MAX_ROUTINES, Missed, Report, Routine, RunCause, RunRecord, RunStatus, Tell, Trigger, id_ok};
use crate::ClientMsg;
pub const CAP_ROUTINES: &str = "routines";
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct RoutineView {
pub routine: Routine,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub schedule: Vec<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub next_run: Option<i64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub runs_per_month: Option<u32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub last_run: Option<RunRecord>,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RoutineEventKind {
#[default]
Reported,
Waiting,
Failed,
#[serde(other)]
Other,
}
pub(crate) fn check_sizes(msg: &ClientMsg) -> Result<(), String> {
let id = |s: &str| if id_ok(s) { Ok(()) } else { Err(format!("{:?} isn't a routine id", s.chars().take(80).collect::<String>())) };
match msg {
ClientMsg::SaveRoutine { routine } => routine.clone().validated().map(|_| ()),
ClientMsg::DeleteRoutine { id: i }
| ClientMsg::RunRoutine { id: i }
| ClientMsg::SetRoutineEnabled { id: i, .. }
| ClientMsg::ListRoutineRuns { id: i } => id(i),
_ => Ok(()),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ServerMsg;
fn loom() -> Routine {
Routine {
id: "loom".into(),
name: "Loom deals".into(),
instructions: "Find deals on a new floor loom.".into(),
when: vec![Trigger::Schedule { rule: "FREQ=DAILY".into(), at: "08:00".into(), zone: Some("Europe/Berlin".into()), start: None }],
..Routine::default()
}
}
#[test]
fn routine_messages_round_trip_and_are_checked() {
let msgs = vec![
ClientMsg::SaveRoutine { routine: loom() },
ClientMsg::SaveRoutine { routine: Routine { id: String::new(), ..loom() } },
ClientMsg::DeleteRoutine { id: "loom".into() },
ClientMsg::RunRoutine { id: "loom".into() },
ClientMsg::SetRoutineEnabled { id: "loom".into(), enabled: false },
ClientMsg::ListRoutineRuns { id: "loom".into() },
];
for m in msgs {
assert!(m.check_sizes().is_ok(), "{}", m.to_json());
assert_eq!(ClientMsg::from_json(&m.to_json()).unwrap(), m);
}
assert!(ClientMsg::RunRoutine { id: "../x".into() }.check_sizes().is_err());
assert!(ClientMsg::SaveRoutine { routine: Routine { instructions: "x".repeat(9000), ..loom() } }.check_sizes().is_err());
let server = vec![
ServerMsg::Routines {
items: vec![RoutineView {
routine: loom(),
schedule: vec!["Every day at 8:00 AM".into()],
next_run: Some(5),
runs_per_month: Some(30),
last_run: None,
}],
},
ServerMsg::RoutineRuns {
routine: "loom".into(),
runs: vec![RunRecord { chat: Some("c".into()), status: RunStatus::Reported, ..RunRecord::default() }],
},
ServerMsg::RoutineEvent {
routine: "loom".into(),
name: "Loom deals".into(),
chat: Some("c".into()),
kind: RoutineEventKind::Reported,
title: "Schacht Wolf Pup, 20% off".into(),
body: "At The Woolery until Sunday.".into(),
},
];
for m in server {
let json = serde_json::to_string(&m).unwrap();
assert_eq!(ServerMsg::from_json(&json).unwrap(), m, "{json}");
}
let e = ServerMsg::from_json(r#"{"type":"routine_event","routine":"r","name":"R","kind":"exploded","title":"t"}"#).unwrap();
assert!(matches!(e, ServerMsg::RoutineEvent { kind: RoutineEventKind::Other, .. }));
}
}