use super::{ObjectRef, VerbError, VerbGate, VerbIntent, find_by_title, words};
use crate::origin::Caller;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SnoozeText {
pub title: String,
pub tomorrow: bool,
}
pub fn parse_snooze(args: &str) -> Option<SnoozeText> {
let args = args.trim();
let (title, tomorrow) = match args.rsplit_once(char::is_whitespace) {
Some((title, last)) if last.eq_ignore_ascii_case("tomorrow") => (title.trim_end(), true),
_ if args.eq_ignore_ascii_case("tomorrow") => ("", true),
_ => (args, false),
};
(!title.is_empty()).then(|| SnoozeText { title: title.to_string(), tomorrow })
}
pub async fn snooze_text(
gate: &VerbGate,
caller: &Caller,
args: &str,
) -> Result<String, VerbError> {
let Some(typed) = parse_snooze(args) else {
return Err(VerbError::invalid(
"/snooze takes a title from Coming up: /snooze <title> [tomorrow]",
));
};
let items = gate.coming_up(caller).await?;
let item = find_by_title(&items, &typed.title)?;
let refused = |why: &str| VerbError::refused(format!("can't snooze {}: {why}", item.title));
if let Some(why) = &item.why_not {
return Err(refused(why));
}
let Some(choice) = item.choices.iter().find(|choice| choice.tomorrow == typed.tomorrow) else {
let why = item.ends.map_or_else(
|| words::NOT_ONE_TIME.to_string(),
|ends| words::ends_at(&gate.registry().scope(caller.user_id(), None).time_text(ends)),
);
return Err(refused(&why));
};
let intent = VerbIntent::new("snooze", serde_json::json!({ "to": choice.to }))
.on(ObjectRef::coming_up(&item.id));
match gate.call(caller, intent).await {
Ok(_) => Ok(format!("✓ {}", words::snoozed(&item.title, &choice.at_text))),
Err(VerbError::Refused(why)) => Err(refused(&why)),
Err(error) => Err(error),
}
}
pub fn problem_line(error: &VerbError) -> String {
match error {
VerbError::Ambiguous { said, matches } => {
let head = words::things_match(matches.len(), said);
let mut chars = head.chars();
let head: String = match chars.next() {
Some(first) => first.to_lowercase().chain(chars).collect(),
None => head,
};
let list: Vec<String> = matches.iter().map(|title| format!(" {title}")).collect();
format!("{head} — type the full title:\n{}", list.join("\n"))
}
other => other.to_string(),
}
}
#[cfg(feature = "tui")]
pub struct SnoozeCommand {
reach: Option<(VerbGate, Caller)>,
}
#[cfg(feature = "tui")]
impl SnoozeCommand {
pub fn new(gate: VerbGate, caller: Caller) -> Self {
Self { reach: Some((gate, caller)) }
}
pub fn remote() -> Self {
Self { reach: None }
}
}
#[cfg(feature = "tui")]
#[async_trait::async_trait]
impl crate::tui::TerminalCommand for SnoozeCommand {
fn name(&self) -> &str {
"snooze"
}
fn description(&self) -> &str {
words::SNOOZE_SOMETHING
}
async fn run(
&self,
args: &str,
_context: &crate::tui::CommandContext,
) -> adk_core::Result<String> {
let Some((gate, caller)) = &self.reach else {
return Err(adk_core::AdkError::from(VerbError::NotHere(
words::NOT_FROM_A_REMOTE_TERMINAL.to_string(),
)));
};
snooze_text(gate, caller, args).await.map_err(|error| {
let line = problem_line(&error);
let mut error = adk_core::AdkError::from(error);
error.message = line;
error
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_title_and_tomorrow_are_read_from_what_was_typed() {
let cases: &[(&str, Option<(&str, bool)>)] = &[
("News digest", Some(("News digest", false))),
(" News digest ", Some(("News digest", false))),
("news digest tomorrow", Some(("news digest", true))),
("News digest TOMORROW", Some(("News digest", true))),
("Tomorrow's plan", Some(("Tomorrow's plan", false))),
("tomorrow", None),
("", None),
];
for (typed, expected) in cases {
let expected =
expected.map(|(title, tomorrow)| SnoozeText { title: title.into(), tomorrow });
assert_eq!(parse_snooze(typed), expected, "{typed:?}");
}
}
}