summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNora <nora.widdecke@tu-bs.de>2019-02-24 22:23:21 +0100
committerNora <nora.widdecke@tu-bs.de>2019-02-24 22:23:21 +0100
commitc3507cfd6eee15166824b19b0b8b8f4e7b2b9016 (patch)
tree70e2b52a5f200a7495f96945a25653020b787dfc
parent7e2b50b71b0673c6c186e171f0fbc57fa9d3c238 (diff)
use enum for cursor movement directions
-rw-r--r--src/actions/cursor.rs8
-rw-r--r--src/cli.rs19
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),
&_ => {}
}
};
diff --git a/src/cli.rs b/src/cli.rs
index 22a9a93..b1f147f 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -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)]