From 43e399dfe6318b10f148a326caa72c977333b2ed Mon Sep 17 00:00:00 2001 From: FliegendeWurst <2012gdwu@web.de> Date: Sun, 14 Jun 2020 18:50:03 +0200 Subject: [PATCH] Parse user-specefied reminder dates Currently only matching YYYY-MM-DD --- src/lib.rs | 25 +++++++++++++++++++++++++ src/main.rs | 22 +++++++++++++++++----- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 99aa219..bf2dab5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,6 +8,7 @@ use thiserror::Error; use std::collections::HashMap; use std::env; use std::sync::Arc; +use chrono::{DateTime, Datelike, Timelike, Local}; pub mod ical_parsing; @@ -61,6 +62,8 @@ pub enum Error { Mime(#[from] mime::FromStrError), #[error("chrono parsing error: {0}")] Chrono(#[from] chrono::format::ParseError), + #[error("integer parsing error: {0}")] + Integer(#[from] std::num::ParseIntError), #[error("ical parsing error: {0}")] Ical(#[from] ical_parsing::Error), #[error("internal error: {0}")] @@ -75,3 +78,25 @@ pub async fn send_message>(msg: S) -> Result<(), Error> { API.send(SendMessage::new(*OWNER, msg.into())).await?; Ok(()) } + +pub fn parse_time>(s: S) -> Result, Error> { + let s = s.as_ref(); + // YYYY-MM-DD format + let parens = s.split('-').collect::>(); + if parens.len() == 3 { + let year = parens[0].parse()?; + let month = parens[1].parse()?; + let day = parens[2].parse()?; + // TODO: construct this datetime in a more elegant way + let dt = Local::now() + .with_year(year).unwrap() + .with_month(month).unwrap() + .with_day(day).unwrap() + .with_hour(0).unwrap() + .with_minute(0).unwrap() + .with_second(0).unwrap(); + Ok(dt) + } else { + Err(error("unsupported format")) + } +} diff --git a/src/main.rs b/src/main.rs index 697a724..9b079f9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -82,11 +82,23 @@ async fn process_one(update: Update, context: &mut Context) -> Result<(), Error> *reminder_start = Local::now(); return Ok(()); } else if !reminder_text.is_empty() { - *reminder_text = data.to_owned(); - let mut edit = EditMessageText::new(*OWNER, *reminder_msg, format!("in {}: {}", format_time(*reminder_time), reminder_text)); - edit.reply_markup(get_keyboard()); - API.send(edit).await?; - return Ok(()); + if data.starts_with("time ") && data.len() > 5 { + let time = parse_time(&data[5..]); + match time { + Ok(time) => { + *reminder_start = time; + send_message(format!("got time {}", reminder_start.format("%Y-%m-%d %H:%M"))).await + }, + Err(e) => send_message(format!("{:?}", e)).await, + }?; + return Ok(()); + } else { + *reminder_text = data.to_owned(); + let mut edit = EditMessageText::new(*OWNER, *reminder_msg, format!("in {}: {}", format_time(*reminder_time), reminder_text)); + edit.reply_markup(get_keyboard()); + API.send(edit).await?; + return Ok(()); + } } let is_url = false; //Url::parse(&data).is_ok(); // TODO: read this data from the Telegram json data (utf16 idxes..) let formatted_text = if is_url {