refactor(CLI): use arg_enum! clap-rs macro

That way, we get a better (case-insensitive) implementation of `FromStr`
which reduces the amount of code we have to maintain.

Closes #101
[skip ci]
This commit is contained in:
Sebastian Thiel
2015-05-06 11:58:37 +02:00
parent 294da41a30
commit 2485343caa
3 changed files with 9 additions and 18 deletions

View File

@@ -32,7 +32,7 @@
%>\
use cmn::{InvalidOptionsError, CLIError, JsonTokenStorage, arg_from_str, writer_from_opts, parse_kv_arg,
input_file_from_opts, input_mime_from_opts, FieldCursor, FieldError, CallType, UploadProtocol,
protocol_from_str};
calltype_from_str};
use std::default::Default;
use std::str::FromStr;
@@ -272,7 +272,7 @@ ${value_unwrap}\
% endif # handle call parameters
% if mc.media_params:
let vals = opt.values_of("${MODE_ARG}").unwrap();
let protocol = protocol_from_str(vals[0], [${', '.join('"%s"' % mp.protocol for mp in mc.media_params)}].iter().map(|&v| v.to_string()).collect(), err);
let protocol = calltype_from_str(vals[0], [${', '.join('"%s"' % mp.protocol for mp in mc.media_params)}].iter().map(|&v| v.to_string()).collect(), err);
let mut input_file = input_file_from_opts(vals[1], err);
let mime_type = input_mime_from_opts(${opt_value(MIME_ARG, default=DEFAULT_MIME)}, err);
% else:

View File

@@ -15,6 +15,7 @@
#![feature(plugin, exit_status)]
#![allow(unused_variables, unused_imports, dead_code, unused_mut)]
#[macro_use]
extern crate clap;
extern crate yup_oauth2 as oauth2;
extern crate yup_hyper_mock as mock;

View File

@@ -37,9 +37,11 @@ pub enum CallType {
Standard,
}
pub enum UploadProtocol {
Simple,
Resumable,
arg_enum!{
pub enum UploadProtocol {
Simple,
Resumable
}
}
impl AsRef<str> for UploadProtocol {
@@ -60,18 +62,6 @@ impl AsRef<str> for CallType {
}
}
impl FromStr for UploadProtocol {
type Err = String;
fn from_str(s: &str) -> Result<UploadProtocol, String> {
match s {
"simple" => Ok(UploadProtocol::Simple),
"resumable" => Ok(UploadProtocol::Resumable),
_ => Err(format!("Protocol '{}' is unknown", s)),
}
}
}
#[derive(Clone, Default)]
pub struct FieldCursor(Vec<String>);
@@ -210,7 +200,7 @@ pub fn parse_kv_arg<'a>(kv: &'a str, err: &mut InvalidOptionsError, for_hashmap:
}
}
pub fn protocol_from_str(name: &str, valid_protocols: Vec<String>, err: &mut InvalidOptionsError) -> CallType {
pub fn calltype_from_str(name: &str, valid_protocols: Vec<String>, err: &mut InvalidOptionsError) -> CallType {
CallType::Upload(
match UploadProtocol::from_str(name) {
Ok(up) => up,