Parse user-specefied reminder dates

Currently only matching YYYY-MM-DD
This commit is contained in:
FliegendeWurst 2020-06-14 18:50:03 +02:00
parent 883ae081bb
commit 43e399dfe6
2 changed files with 42 additions and 5 deletions

View File

@ -8,6 +8,7 @@ use thiserror::Error;
use std::collections::HashMap; use std::collections::HashMap;
use std::env; use std::env;
use std::sync::Arc; use std::sync::Arc;
use chrono::{DateTime, Datelike, Timelike, Local};
pub mod ical_parsing; pub mod ical_parsing;
@ -61,6 +62,8 @@ pub enum Error {
Mime(#[from] mime::FromStrError), Mime(#[from] mime::FromStrError),
#[error("chrono parsing error: {0}")] #[error("chrono parsing error: {0}")]
Chrono(#[from] chrono::format::ParseError), Chrono(#[from] chrono::format::ParseError),
#[error("integer parsing error: {0}")]
Integer(#[from] std::num::ParseIntError),
#[error("ical parsing error: {0}")] #[error("ical parsing error: {0}")]
Ical(#[from] ical_parsing::Error), Ical(#[from] ical_parsing::Error),
#[error("internal error: {0}")] #[error("internal error: {0}")]
@ -75,3 +78,25 @@ pub async fn send_message<S: Into<String>>(msg: S) -> Result<(), Error> {
API.send(SendMessage::new(*OWNER, msg.into())).await?; API.send(SendMessage::new(*OWNER, msg.into())).await?;
Ok(()) Ok(())
} }
pub fn parse_time<S: AsRef<str>>(s: S) -> Result<DateTime<Local>, Error> {
let s = s.as_ref();
// YYYY-MM-DD format
let parens = s.split('-').collect::<Vec<_>>();
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"))
}
}

View File

@ -82,12 +82,24 @@ async fn process_one(update: Update, context: &mut Context) -> Result<(), Error>
*reminder_start = Local::now(); *reminder_start = Local::now();
return Ok(()); return Ok(());
} else if !reminder_text.is_empty() { } else if !reminder_text.is_empty() {
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(); *reminder_text = data.to_owned();
let mut edit = EditMessageText::new(*OWNER, *reminder_msg, format!("in {}: {}", format_time(*reminder_time), reminder_text)); let mut edit = EditMessageText::new(*OWNER, *reminder_msg, format!("in {}: {}", format_time(*reminder_time), reminder_text));
edit.reply_markup(get_keyboard()); edit.reply_markup(get_keyboard());
API.send(edit).await?; API.send(edit).await?;
return Ok(()); return Ok(());
} }
}
let is_url = false; //Url::parse(&data).is_ok(); // TODO: read this data from the Telegram json data (utf16 idxes..) 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 { let formatted_text = if is_url {
format!("<ul><li><a href=\"{}\">{}</a></li></ul>", data, data) format!("<ul><li><a href=\"{}\">{}</a></li></ul>", data, data)