This commit is contained in:
2021-09-18 13:40:30 +01:00
parent a9c91bee93
commit a0974795e1
8 changed files with 276 additions and 263 deletions

View File

@ -21,7 +21,6 @@ use crate::{
reminder::{content::Content, errors::ReminderError, helper::generate_uid, Reminder},
user_data::UserData,
},
time_parser::TimeParser,
SQLPool,
};
@ -133,7 +132,8 @@ INSERT INTO reminders (
self.set_by
)
.execute(&self.pool)
.await;
.await
.unwrap();
Ok(Reminder::from_uid(&self.pool, self.uid).await.unwrap())
}
@ -147,11 +147,9 @@ INSERT INTO reminders (
pub struct MultiReminderBuilder<'a> {
scopes: Vec<ReminderScope>,
utc_time: NaiveDateTime,
utc_time_parser: Option<TimeParser>,
timezone: Tz,
interval: Option<i64>,
expires: Option<NaiveDateTime>,
expires_parser: Option<TimeParser>,
content: Content,
set_by: Option<u32>,
ctx: &'a Context,
@ -163,11 +161,9 @@ impl<'a> MultiReminderBuilder<'a> {
MultiReminderBuilder {
scopes: vec![],
utc_time: Utc::now().naive_utc(),
utc_time_parser: None,
timezone: Tz::UTC,
interval: None,
expires: None,
expires_parser: None,
content: Content::new(),
set_by: None,
ctx,
@ -187,12 +183,6 @@ impl<'a> MultiReminderBuilder<'a> {
self
}
pub fn time_parser(mut self, parser: TimeParser) -> Self {
self.utc_time_parser = Some(parser);
self
}
pub fn expires<T: Into<i64>>(mut self, time: Option<T>) -> Self {
if let Some(t) = time {
self.expires = Some(NaiveDateTime::from_timestamp(t.into(), 0));
@ -203,12 +193,6 @@ impl<'a> MultiReminderBuilder<'a> {
self
}
pub fn expires_parser(mut self, parser: Option<TimeParser>) -> Self {
self.expires_parser = parser;
self
}
pub fn author(mut self, user: UserData) -> Self {
self.set_by = Some(user.id);
self.timezone = user.timezone();
@ -226,33 +210,13 @@ impl<'a> MultiReminderBuilder<'a> {
self.scopes = scopes;
}
pub async fn build(mut self) -> (HashSet<ReminderError>, HashSet<ReminderScope>) {
pub async fn build(self) -> (HashSet<ReminderError>, HashSet<ReminderScope>) {
let pool = self.ctx.data.read().await.get::<SQLPool>().cloned().unwrap();
let mut errors = HashSet::new();
let mut ok_locs = HashSet::new();
if let Some(expire_parser) = self.expires_parser {
if let Ok(expires) = expire_parser.timestamp() {
self.expires = Some(NaiveDateTime::from_timestamp(expires, 0));
} else {
errors.insert(ReminderError::InvalidExpiration);
return (errors, ok_locs);
}
}
if let Some(time_parser) = self.utc_time_parser {
if let Ok(time) = time_parser.timestamp() {
self.utc_time = NaiveDateTime::from_timestamp(time, 0);
} else {
errors.insert(ReminderError::InvalidTime);
return (errors, ok_locs);
}
}
if self.interval.map_or(false, |i| (i as i64) < *MIN_INTERVAL) {
errors.insert(ReminderError::ShortInterval);
} else if self.interval.map_or(false, |i| (i as i64) > *MAX_TIME) {

View File

@ -39,8 +39,8 @@ pub enum ReminderError {
DiscordError(String),
}
impl ReminderError {
pub fn display(&self, is_natural: bool) -> String {
impl ToString for ReminderError {
fn to_string(&self) -> String {
match self {
ReminderError::LongTime => {
"That time is too far in the future. Please specify a shorter time.".to_string()
@ -50,40 +50,20 @@ impl ReminderError {
max_time = *MAX_TIME / 86_400
),
ReminderError::PastTime => {
"Please ensure the time provided is in the future. If the time should be in \
the future, please be more specific with the definition."
.to_string()
"Please ensure the time provided is in the future. If the time should be in the future, please be more specific with the definition.".to_string()
}
ReminderError::ShortInterval => format!(
"Please ensure the interval provided is longer than {min_interval} seconds",
min_interval = *MIN_INTERVAL
),
ReminderError::InvalidTag => {
"Couldn't find a location by your tag. Your tag must be either a channel or \
a user (not a role)"
.to_string()
"Couldn't find a location by your tag. Your tag must be either a channel or a user (not a role)".to_string()
}
ReminderError::InvalidTime => {
if is_natural {
"Your time failed to process. Please make it as clear as possible, for example `\"16th of july\"` \
or `\"in 20 minutes\"`"
.to_string()
} else {
"Make sure the time you have provided is in the format of [num][s/m/h/d][num][s/m/h/d] etc. or \
`day/month/year-hour:minute:second`"
.to_string()
}
"Your time failed to process. Please make it as clear as possible, for example `\"16th of july\"` or `\"in 20 minutes\"`".to_string()
}
ReminderError::InvalidExpiration => {
if is_natural {
"Your expiration time failed to process. Please make it as clear as possible, for example `\"16th \
of july\"` or `\"in 20 minutes\"`"
.to_string()
} else {
"Make sure the expiration time you have provided is in the format of [num][s/m/h/d][num][s/m/h/d] \
etc. or `day/month/year-hour:minute:second`"
.to_string()
}
"Your expiration time failed to process. Please make it as clear as possible, for example `\"16th of july\"` or `\"in 20 minutes\"`".to_string()
}
ReminderError::DiscordError(s) => format!("A Discord error occurred: **{}**", s),
}