diff options
author | Till Höppner | 2016-03-16 15:14:03 +0100 |
---|---|---|
committer | Till Höppner | 2016-03-16 15:14:03 +0100 |
commit | 586461676df9abd2e011fbcb1bb7c784cdb2844c (patch) | |
tree | 6c66fa86c9cb9876757b099d7460ff17c39195a5 /cli/src/lib.rs | |
parent | 363e3722256c8e91bbd78bfcb2b1d3ffc83d9c0a (diff) | |
download | ilc-586461676df9abd2e011fbcb1bb7c784cdb2844c.tar.gz ilc-586461676df9abd2e011fbcb1bb7c784cdb2844c.tar.xz ilc-586461676df9abd2e011fbcb1bb7c784cdb2844c.zip |
Add filtering functionality
Diffstat (limited to 'cli/src/lib.rs')
-rw-r--r-- | cli/src/lib.rs | 82 |
1 files changed, 79 insertions, 3 deletions
diff --git a/cli/src/lib.rs b/cli/src/lib.rs index eb2fb43..ac31636 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -11,17 +11,21 @@ extern crate env_logger; extern crate serde; extern crate serde_json; extern crate glob; +extern crate regex; use ilc_base::{Context, Decode, Encode}; +use ilc_ops::convert::{Filter, Operator, Subject}; use ilc_format_weechat::Weechat; use ilc_format_energymech::Energymech; -use clap::{App, AppSettings, Arg, ArgMatches, SubCommand}; +use clap::{App, AppSettings, Arg, ArgGroup, ArgMatches, SubCommand}; use chrono::{FixedOffset, NaiveDate}; use glob::glob; +use regex::Regex; + use std::str::FromStr; use std::fs::File; use std::path::{Path, PathBuf}; @@ -30,6 +34,7 @@ use std::io::{self, BufRead, BufReader, BufWriter, Write}; use std::process; use std::error::Error; + mod chain; mod stats; @@ -121,7 +126,32 @@ pub fn main(cli: Cli) { .setting(AppSettings::AllowLeadingHyphen)) .subcommand(SubCommand::with_name("convert") .about("Convert from a source to a target format") - .setting(AppSettings::AllowLeadingHyphen)) + .setting(AppSettings::AllowLeadingHyphen) + .arg(Arg::with_name("subject") + .takes_value(true) + .requires("operator") + .long("if") + .possible_values(&["nick", "time", "type", "text"])) + .arg(Arg::with_name("op_not").long("not")) + .arg(Arg::with_name("op_exactly") + .takes_value(true) + .long("exactly")) + .arg(Arg::with_name("op_contains") + .takes_value(true) + .long("contains")) + .arg(Arg::with_name("op_greater") + .takes_value(true) + .long("greater")) + .arg(Arg::with_name("op_less").takes_value(true).long("less")) + .arg(Arg::with_name("op_matches") + .takes_value(true) + .long("matches")) + .group(ArgGroup::with_name("operator").args(&["op_exactly", + "op_contains", + "op_equal", + "op_greater", + "op_less", + "op_matches"]))) .subcommand(SubCommand::with_name("stats") .about("Analyse the activity of users by certain metrics") .setting(AppSettings::AllowLeadingHyphen)) @@ -157,11 +187,57 @@ pub fn main(cli: Cli) { } ("convert", Some(args)) => { let e = Environment(&args); + let subject = match args.value_of("subject") { + Some("nick") => Some(Subject::Nick), + Some("time") => Some(Subject::Time), + Some("type") => Some(Subject::Type), + Some("text") => Some(Subject::Text), + _ => None, + }; + + let op = { + if args.is_present("operator") { + if let Some(sub) = args.value_of("op_exactly") { + Some(Operator::Exactly(sub.into())) + } else if let Some(sub) = args.value_of("op_contains") { + Some(Operator::Contains(sub.into())) + } else if let Some(sub) = args.value_of("op_matches") { + match Regex::new(sub) { + Ok(regex) => Some(Operator::Matches(regex)), + Err(e) => error(Box::new(e)), + } + } else { + // must be numeric operator if not op_exactly, op_contains, or op_matches + // unwrap is safe because of .is_present("operator") earlier + let num = match args.value_of("operator").unwrap().parse::<i64>() { + Ok(n) => n, + Err(e) => error(Box::new(e)), + }; + + if args.is_present("op_equal") { + Some(Operator::Equal(num)) + } else if args.is_present("op_greater") { + Some(Operator::Greater(num)) + } else if args.is_present("op_less") { + Some(Operator::Less(num)) + } else { + None + } + } + } else { + None + } + }; + + let filter = subject.and_then(|s| op.map(|o| Filter(s, o))); + ilc_ops::convert::convert(&e.context(), &mut e.input(), &mut *e.decoder(), &mut *e.output(), - &*e.encoder()) + &*e.encoder(), + filter, + args.is_present("op_not")) } ("stats", Some(args)) => { let e = Environment(&args); |