diff options
author | Nora <nora.widdecke@tu-bs.de> | 2019-02-24 23:59:10 +0100 |
---|---|---|
committer | Nora <nora.widdecke@tu-bs.de> | 2019-02-24 23:59:10 +0100 |
commit | f429b5aa2a0512c46a5e5f0bfa03f17ac178dcca (patch) | |
tree | 714ea33e06a37fc69ec9a78d4881d4161feec7b2 | |
parent | 31a286e6efe3769cee31bb1e27fff82b87fffdd3 (diff) |
structopt: new
-rw-r--r-- | src/actions/new.rs | 48 | ||||
-rw-r--r-- | src/bin/khaleesi.rs | 2 | ||||
-rw-r--r-- | src/cli.rs | 22 |
3 files changed, 51 insertions, 21 deletions
diff --git a/src/actions/new.rs b/src/actions/new.rs index 8b196f6..e1c54f3 100644 --- a/src/actions/new.rs +++ b/src/actions/new.rs @@ -2,6 +2,7 @@ use crate::defaults; use crate::icalwrap::{IcalVCalendar,IcalTime,IcalTimeZone}; use crate::khline::KhLine; use crate::utils::{misc,fileutil}; +use crate::cli::New; use crate::KhResult; use crate::cursorfile; @@ -16,15 +17,12 @@ struct EventProperties { } impl EventProperties { - fn parse_from_args(args: &[&str]) -> KhResult<EventProperties> { - if args.len() < 3 { - Err("new calendar from to summary location")? - } - let calendar = EventProperties::parse_calendar(args[0])?; - let from = EventProperties::parse_from(args[1])?; - let to = EventProperties::parse_to(args[2])?; - let summary = EventProperties::parse_summary(args[3])?; - let location = EventProperties::parse_location(args[4])?; + fn parse_from_args(args: &New) -> KhResult<EventProperties> { + let calendar = EventProperties::parse_calendar(&args.calendar)?; + let from = EventProperties::parse_from(&args.from)?; + let to = EventProperties::parse_to(&args.to)?; + let summary = EventProperties::parse_summary(&args.summary)?; + let location = EventProperties::parse_location(&args.location)?; Ok(EventProperties{ calendar, from, to, summary, location }) } @@ -72,7 +70,7 @@ impl EventProperties { } } -pub fn do_new(args: &[&str]) -> KhResult<()> { +pub fn do_new(args: &New) -> KhResult<()> { let uid = misc::make_new_uid(); let ep = EventProperties::parse_from_args(args)?; @@ -206,19 +204,24 @@ mod integration { #[test] fn test_parse_from_args() { let _testdir = testutils::prepare_testdir("testdir_two_cals"); - let args = &["second", "2017-11-03T12:30:00", "2017-11-07T11:11:00", "summary text", "location text"]; - let ep = EventProperties::parse_from_args(args).unwrap(); + let args = New { calendar: "second".to_string(), + from: "2017-11-03T12:30:00".to_string(), + to: "2017-11-07T11:11:00".to_string(), + summary: "summary text".to_string(), + location: "location text".to_string() + }; + let ep = EventProperties::parse_from_args(&args).unwrap(); assert_eq!("second".to_string(), ep.calendar); assert_eq!("summary text".to_string(), ep.summary); assert_eq!("location text".to_string(), ep.location); } - #[test] - fn test_parse_from_args_neg() { - let args = &["1", "2", "3", "4"]; - let ep = EventProperties::parse_from_args(args); - assert!(ep.is_err()); - } + //#[test] + //fn test_parse_from_args_neg() { + // let args = &["1", "2", "3", "4"]; + // let ep = EventProperties::parse_from_args(args); + // assert!(ep.is_err()); + //} #[test] fn test_with_eventprops() { @@ -255,9 +258,14 @@ mod integration { testdata::setup(); let testdir = testutils::prepare_testdir("testdir_two_cals"); - let args = &["second", "2017-11-03T12:30:00", "2017-11-07T11:11:00", "summary text", "location text"]; + let args = New { calendar: "second".to_string(), + from: "2017-11-03T12:30:00".to_string(), + to: "2017-11-07T11:11:00".to_string(), + summary: "summary text".to_string(), + location: "location text".to_string() + }; - let result = do_new(args); + let result = do_new(&args); assert!(result.is_ok()); let expected = indoc!(" diff --git a/src/bin/khaleesi.rs b/src/bin/khaleesi.rs index 29280c1..13f3fbe 100644 --- a/src/bin/khaleesi.rs +++ b/src/bin/khaleesi.rs @@ -52,7 +52,7 @@ fn main_internal(args: &cli::CommandLine, config: &Config) -> KhResult<()> { list::list_by_args(&x.args.iter().map(|x| x.as_ref()).collect::<Vec<&str>>()) } // "modify" => modify::do_modify(args), - // "new" => new::do_new(args), + cli::Command::New(x) => new::do_new(x), // "select" => select::select_by_args(args), // "seq" => seq::action_seq(args), // "pretty" => prettyprint::prettyprint(), @@ -38,6 +38,9 @@ pub enum Command { /// Select from the sequence #[structopt(name = "list")] List(List), + /// Create new event + #[structopt(name = "new")] + New(New), } #[derive(Debug, StructOpt)] @@ -78,3 +81,22 @@ pub struct List { #[structopt(name = "args")] pub args: Vec<String>, } + +#[derive(Debug, StructOpt)] +pub struct New { + /// the calendar + #[structopt(name = "calendar")] + pub calendar: String, + /// from + #[structopt(name = "from")] + pub from: String, + /// to + #[structopt(name = "to")] + pub to: String, + /// summary + #[structopt(name = "summary")] + pub summary: String, + /// location + #[structopt(name = "location")] + pub location: String, +} |