diff options
| -rw-r--r-- | src/csl.rs | 2 | ||||
| -rw-r--r-- | src/filters.rs | 7 | ||||
| -rw-r--r-- | src/main.rs | 17 | 
3 files changed, 25 insertions, 1 deletions
| @@ -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<Item=Self>; @@ -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<NaiveDateTime>, +    /// 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<Weekday>, +      /// Print more debugging information to stderr.      #[structopt(long = "debug")]      debug: bool, @@ -206,6 +215,11 @@ fn parse_time_arg(input: &str) -> Result<NaiveDateTime, &'static str> {      Err("unknown time format")  } +fn try_from_str_simple_error<T: FromStr>(input: &str) -> Result<T, String> +{ +    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<Option<LogResult>, 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 { | 
