From d097f363131ddb5be865a26f8cc675eb40750111 Mon Sep 17 00:00:00 2001 From: jude Date: Sun, 30 Aug 2020 00:16:33 +0100 Subject: [PATCH] starting on time parser port from python --- src/commands/moderation_cmds.rs | 12 ++++ src/main.rs | 1 + src/time_parser.rs | 121 ++++++++++++++++++++++++++++++++ 3 files changed, 134 insertions(+) create mode 100644 src/time_parser.rs diff --git a/src/commands/moderation_cmds.rs b/src/commands/moderation_cmds.rs index b91042c..1bdc745 100644 --- a/src/commands/moderation_cmds.rs +++ b/src/commands/moderation_cmds.rs @@ -143,3 +143,15 @@ async fn prefix(ctx: &Context, msg: &Message, args: String) -> CommandResult { Ok(()) } + +#[command] +async fn pause(ctx: &Context, msg: &Message, args: String) -> CommandResult { + let pool = ctx.data.read().await + .get::().cloned().expect("Could not get SQLPool from data"); + + let channel = ChannelData::from_channel(msg.channel(&ctx).await.unwrap(), pool.clone()).await.unwrap(), + + channel.commit_changes(pool).await; + + Ok(()) +} diff --git a/src/main.rs b/src/main.rs index fcb78b7..32e0b23 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ extern crate lazy_static; mod models; mod framework; mod commands; +mod time_parser; use serenity::{ client::{ diff --git a/src/time_parser.rs b/src/time_parser.rs new file mode 100644 index 0000000..6ca16a5 --- /dev/null +++ b/src/time_parser.rs @@ -0,0 +1,121 @@ +use std::time::{ + SystemTime, + UNIX_EPOCH, +}; + +use chrono_tz::Tz; + +enum ParseType { + Explicit, + Displacement, +} + +struct TimeParser { + timezone: Tz, + inverted: bool, + time_string: String, + parse_type: ParseType, +} + +impl TimeParser { + pub fn new(input: String, timezone: Tz) -> Self { + let inverted = if input.starts_with("-") { + true + } + else { + false + }; + + let parse_type = if input.contains("/") || input.contains(":") { + ParseType::Explicit + } + else { + ParseType::Displacement + }; + + Self { + timezone, + inverted, + time_string: input.trim_start_matches("-").to_string(), + parse_type, + } + } + + pub fn timestamp(&self) -> i32 { + + } + + pub fn displacement(&self) -> i32 { + + } + + fn process(&self) -> i32 { + match self.parse_type { + ParseType::Explicit => { + self.process_explicit() + }, + + ParseType::Displacement => { + let now = SystemTime::now(); + let since_epoch = now + .duration_since(UNIX_EPOCH) + .expect("Time calculated as going backwards. Very bad"); + + since_epoch.as_secs() + self.process_displacement() + }, + } + } + + fn process_explicit(&self) -> i32 { + + 0 + } + + fn process_displacement(&self) -> i32 { + let mut current_buffer = "0".to_string(); + + let mut seconds = 0; + let mut minutes = 0; + let mut hours = 0; + let mut days = 0; + + for character in self.time_string.chars() { + match character { + + 's' => { + seconds = current_buffer.parse::().unwrap(); + current_buffer = String::from("0"); + }, + + 'm' => { + minutes = current_buffer.parse::().unwrap(); + current_buffer = String::from("0"); + }, + + 'h' => { + hours = current_buffer.parse::().unwrap(); + current_buffer = String::from("0"); + }, + + 'd' => { + days = current_buffer.parse::().unwrap(); + current_buffer = String::from("0"); + }, + + c => { + if c.is_digit(10) { + current_buffer += c.as_str(); + } + else { + // raise exception + } + }, + } + } + + let full = seconds + (minutes * 60) + (hours * 3600) + (days * 86400) + current_buffer.parse::() * + if self.inverted { -1 } else { 1 }; + + full + } +}