aboutsummaryrefslogtreecommitdiff
path: root/argparse/src/generic.rs
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2018-12-18 00:39:15 +0100
committerDaniel Mueller <deso@posteo.net>2018-12-21 18:41:19 -0800
commitd137415a69007a90569ebbf38a92424fba60b997 (patch)
tree08bdb1fc4f06bca29d36fdfc9e04c8c5c0086119 /argparse/src/generic.rs
parent170e1df9d96f628af8cf8107f29227b90ae9350b (diff)
downloadnitrocli-d137415a69007a90569ebbf38a92424fba60b997.tar.gz
nitrocli-d137415a69007a90569ebbf38a92424fba60b997.tar.bz2
Add argparse 0.2.2 as a dependency
This patch adds the crate rust-argparse [0] in version 0.2.2 as a dependency, as discussed in issue #4. [0] https://github.com/tailhook/rust-argparse Import subrepo argparse/:argparse at 0de60a5e6d9ee1a3570d6089afd3ccd6ed7480c5
Diffstat (limited to 'argparse/src/generic.rs')
-rw-r--r--argparse/src/generic.rs133
1 files changed, 133 insertions, 0 deletions
diff --git a/argparse/src/generic.rs b/argparse/src/generic.rs
new file mode 100644
index 0000000..1b1b7dc
--- /dev/null
+++ b/argparse/src/generic.rs
@@ -0,0 +1,133 @@
+use std::cell::RefCell;
+use std::str::FromStr;
+use std::rc::Rc;
+
+use super::{StoreConst, Store, StoreOption, List, Collect, PushConst};
+use super::action::Action;
+use super::action::{TypedAction, IFlagAction, IArgAction, IArgsAction};
+use super::action::ParseResult;
+use super::action::ParseResult::{Parsed, Error};
+use super::action::Action::{Flag, Single, Push, Many};
+
+pub struct StoreConstAction<'a, T: 'a> {
+ pub value: T,
+ pub cell: Rc<RefCell<&'a mut T>>,
+}
+
+pub struct PushConstAction<'a, T: 'a> {
+ pub value: T,
+ pub cell: Rc<RefCell<&'a mut Vec<T>>>,
+}
+
+pub struct StoreAction<'a, T: 'a> {
+ pub cell: Rc<RefCell<&'a mut T>>,
+}
+
+pub struct StoreOptionAction<'a, T: 'a> {
+ cell: Rc<RefCell<&'a mut Option<T>>>,
+}
+
+pub struct ListAction<'a, T: 'a> {
+ cell: Rc<RefCell<&'a mut Vec<T>>>,
+}
+
+impl<T: 'static + Clone> TypedAction<T> for StoreConst<T> {
+ fn bind<'x>(&self, cell: Rc<RefCell<&'x mut T>>) -> Action<'x> {
+ let StoreConst(ref val) = *self;
+ return Flag(Box::new(StoreConstAction { cell: cell, value: val.clone() }));
+ }
+}
+
+impl<T: 'static + Clone> TypedAction<Vec<T>> for PushConst<T> {
+ fn bind<'x>(&self, cell: Rc<RefCell<&'x mut Vec<T>>>) -> Action<'x> {
+ let PushConst(ref val) = *self;
+ return Flag(Box::new(PushConstAction { cell: cell, value: val.clone() }));
+ }
+}
+
+impl<T: 'static + FromStr> TypedAction<T> for Store {
+ fn bind<'x>(&self, cell: Rc<RefCell<&'x mut T>>) -> Action<'x> {
+ return Single(Box::new(StoreAction { cell: cell }));
+ }
+}
+
+impl<T: 'static + FromStr> TypedAction<Option<T>> for StoreOption {
+ fn bind<'x>(&self, cell: Rc<RefCell<&'x mut Option<T>>>) -> Action<'x> {
+ return Single(Box::new(StoreOptionAction { cell: cell }));
+ }
+}
+
+impl<T: 'static + FromStr + Clone> TypedAction<Vec<T>> for List {
+ fn bind<'x>(&self, cell: Rc<RefCell<&'x mut Vec<T>>>) -> Action<'x> {
+ return Many(Box::new(ListAction { cell: cell }));
+ }
+}
+
+impl<T: 'static + FromStr + Clone> TypedAction<Vec<T>> for Collect {
+ fn bind<'x>(&self, cell: Rc<RefCell<&'x mut Vec<T>>>) -> Action<'x> {
+ return Push(Box::new(ListAction { cell: cell }));
+ }
+}
+
+impl<'a, T: Clone> IFlagAction for StoreConstAction<'a, T> {
+ fn parse_flag(&self) -> ParseResult {
+ let mut targ = self.cell.borrow_mut();
+ **targ = self.value.clone();
+ return Parsed;
+ }
+}
+
+impl<'a, T: Clone> IFlagAction for PushConstAction<'a, T> {
+ fn parse_flag(&self) -> ParseResult {
+ let mut targ = self.cell.borrow_mut();
+ targ.push(self.value.clone());
+ return Parsed;
+ }
+}
+
+impl<'a, T: FromStr> IArgAction for StoreAction<'a, T> {
+ fn parse_arg(&self, arg: &str) -> ParseResult {
+ match FromStr::from_str(arg) {
+ Ok(x) => {
+ **self.cell.borrow_mut() = x;
+ return Parsed;
+ }
+ Err(_) => {
+ return Error(format!("Bad value {}", arg));
+ }
+ }
+ }
+}
+
+impl<'a, T: FromStr> IArgAction for StoreOptionAction<'a, T> {
+ fn parse_arg(&self, arg: &str) -> ParseResult {
+ match FromStr::from_str(arg) {
+ Ok(x) => {
+ **self.cell.borrow_mut() = Some(x);
+ return Parsed;
+ }
+ Err(_) => {
+ return Error(format!("Bad value {}", arg));
+ }
+ }
+ }
+}
+
+impl<'a, T: FromStr + Clone> IArgsAction for ListAction<'a, T> {
+ fn parse_args(&self, args: &[&str]) -> ParseResult {
+ let mut result = vec!();
+ for arg in args.iter() {
+ match FromStr::from_str(*arg) {
+ Ok(x) => {
+ result.push(x);
+ }
+ Err(_) => {
+ return Error(format!("Bad value {}", arg));
+ }
+ }
+ }
+ **self.cell.borrow_mut() = result;
+ return Parsed;
+ }
+}
+