From 54e29430b3a668e9e98d3fc6e1a107fd36af8af4 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sat, 18 May 2019 01:37:50 +0200 Subject: add support for weekday filtering --- src/csl.rs | 2 ++ src/filters.rs | 7 +++++++ src/main.rs | 17 ++++++++++++++++- 3 files changed, 25 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/csl.rs b/src/csl.rs index 83f2e14..fb11841 100644 --- a/src/csl.rs +++ b/src/csl.rs @@ -4,6 +4,7 @@ use std::str::FromStr; use std::fmt; use super::{SearchField, FightOutcome}; +use chrono::Weekday; pub trait Variants: Copy { type Output: Iterator; @@ -33,6 +34,7 @@ macro_rules! variants { variants! { SearchField => Account, Character } variants! { FightOutcome => Success, Wipe } +variants! { Weekday => Mon, Tue, Wed, Thu, Fri, Sat, Sun } /// The character that delimits items from each other. const DELIMITER: char = ','; diff --git a/src/filters.rs b/src/filters.rs index ef8bfad..d8d43ea 100644 --- a/src/filters.rs +++ b/src/filters.rs @@ -2,6 +2,8 @@ use evtclib::{AgentName, Log}; use super::{SearchField, LogResult, Opt}; +use chrono::Datelike; + /// Do filtering based on the character or account name. pub fn filter_name(log: &Log, opt: &Opt) -> bool { for player in log.players() { @@ -28,6 +30,11 @@ pub fn filter_outcome(result: &LogResult, opt: &Opt) -> bool { opt.outcome.contains(&result.outcome) } +/// Do filtering based on the weekday of the fight. +pub fn filter_weekday(result: &LogResult, opt: &Opt) -> bool { + opt.weekdays.contains(&result.time.weekday()) +} + /// Do filtering based on encounter time. pub fn filter_time(result: &LogResult, opt: &Opt) -> bool { let after_ok = match opt.after { diff --git a/src/main.rs b/src/main.rs index c1c2bcb..099262c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,7 +15,7 @@ use std::io::{self, BufReader}; use std::path::PathBuf; use std::str::FromStr; -use chrono::{Duration, NaiveDateTime}; +use chrono::{Duration, NaiveDateTime, Weekday}; use num_traits::cast::FromPrimitive; use regex::Regex; use structopt::StructOpt; @@ -113,6 +113,15 @@ pub struct Opt { )] before: Option, + /// Only show logs from the given weekdays. + #[structopt( + short = "w", + long = "weekdays", + default_value = "*", + parse(try_from_str = "try_from_str_simple_error") + )] + weekdays: CommaSeparatedList, + /// Print more debugging information to stderr. #[structopt(long = "debug")] debug: bool, @@ -206,6 +215,11 @@ fn parse_time_arg(input: &str) -> Result { Err("unknown time format") } +fn try_from_str_simple_error(input: &str) -> Result +{ + T::from_str(input).map_err(|_| format!("'{}' is an invalid value", input)) +} + fn main() { let opt = Opt::from_args(); @@ -283,6 +297,7 @@ fn search_log(entry: &DirEntry, opt: &Opt) -> Result, RuntimeE let take_log = filters::filter_name(&log, opt) == !opt.invert && filters::filter_outcome(&info, opt) + && filters::filter_weekday(&info, opt) && filters::filter_time(&info, opt); if take_log { -- cgit v1.2.3