diff options
author | Nora <nora.widdecke@tu-bs.de> | 2019-02-24 22:23:21 +0100 |
---|---|---|
committer | Nora <nora.widdecke@tu-bs.de> | 2019-02-24 22:23:21 +0100 |
commit | c3507cfd6eee15166824b19b0b8b8f4e7b2b9016 (patch) | |
tree | 70e2b52a5f200a7495f96945a25653020b787dfc | |
parent | 7e2b50b71b0673c6c186e171f0fbc57fa9d3c238 (diff) |
use enum for cursor movement directions
-rw-r--r-- | src/actions/cursor.rs | 8 | ||||
-rw-r--r-- | src/cli.rs | 19 |
2 files changed, 22 insertions, 5 deletions
diff --git a/src/actions/cursor.rs b/src/actions/cursor.rs index f23a1ff..1d3ae91 100644 --- a/src/actions/cursor.rs +++ b/src/actions/cursor.rs @@ -2,7 +2,7 @@ use crate::cursorfile; use crate::utils::stdioutils; use crate::KhResult; use crate::seqfile; -use crate::cli::Cursor; +use crate::cli::{Cursor,Direction as CursorDirection}; enum Direction { Up, @@ -15,9 +15,9 @@ pub fn do_cursor(args: &Cursor) -> KhResult<()> { } else { //println!("stdin is tty") if let Some(direction) = &args.direction { - match direction.as_str() { - "prev" => return cursor_sequence_move(&Direction::Up), - "next" => return cursor_sequence_move(&Direction::Down), + match direction { + CursorDirection::Prev => return cursor_sequence_move(&Direction::Up), + CursorDirection::Next => return cursor_sequence_move(&Direction::Down), &_ => {} } }; @@ -45,7 +45,24 @@ pub struct Agenda { pub struct Cursor { /// Move the cursor on the selection. Uses "next" and "prev". #[structopt(name = "direction")] - pub direction: Option<String>, + pub direction: Option<Direction>, +} + +#[derive(Debug, StructOpt)] +pub enum Direction { + Next, + Prev, +} + +impl std::str::FromStr for Direction{ + type Err = String; + fn from_str(s: &str) -> Result<Self, <Self as std::str::FromStr>::Err> { + match s { + "prev" => Ok(Direction::Prev), + "next" => Ok(Direction::Next), + &_ => Err("Expected 'prev' or 'next'".to_string()) + } + } } #[derive(Debug, StructOpt)] |