From 8d58f0ade28e07c0d5d885478ba211fce7c05305 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 7 Sep 2018 02:06:52 +0200 Subject: add filtering by outcome --- src/main.rs | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 3246864..31e29ad 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,10 +42,13 @@ struct Opt { path: PathBuf, /// The fields which should be searched. - /// Possible values: all, account, character #[structopt(short = "f", long = "fields", default_value = "all")] field: SearchField, + /// Only display fights with the given outcome. + #[structopt(short = "o", long = "outcome")] + outcome: Option, + /// The regular expression to search for. #[structopt(name = "EXPR")] expression: Regex, @@ -124,6 +127,18 @@ pub enum FightOutcome { Wipe, } +impl FromStr for FightOutcome { + type Err = &'static str; + + fn from_str(s: &str) -> Result { + match s { + "success" => Ok(FightOutcome::Success), + "wipe" | "fail" => Ok(FightOutcome::Wipe), + _ => Err("Must be success or wipe"), + } + } +} + fn main() { let opt = Opt::from_args(); let result = grep(&opt); @@ -183,6 +198,7 @@ fn search_log(entry: &DirEntry, opt: &Opt) -> Result, RuntimeE return Ok(None); }; + let mut candidate_good = false; for player in log.players() { match player.name() { AgentName::Player { @@ -193,14 +209,24 @@ fn search_log(entry: &DirEntry, opt: &Opt) -> Result, RuntimeE if (opt.field.search_account() && opt.expression.is_match(account_name)) || (opt.field.search_character() && opt.expression.is_match(character_name)) { - return Ok(Some(extract_info(entry, &log))); + candidate_good = true; } } _ => unreachable!(), } } - Ok(None) + if !candidate_good { + return Ok(None); + } + + let result = extract_info(entry, &log); + + match opt.outcome { + Some(o) if o == result.outcome => Ok(Some(result)), + None => Ok(Some(result)), + _ => Ok(None), + } } /// Extract human-readable information from the given log file. -- cgit v1.2.3