Skip to content

Commit

Permalink
fix(clap):re-introduce UploadProtocol,fix CallType
Browse files Browse the repository at this point in the history
* CallType now represents either Upload or Standard calls, whereas
  the Upload variant is represented by the UploadProtocol enum.
  That way it's clear what happens, and we don't mix orthogonal concepts
  in one enumeration just for convenience.

All tested APIs seem to build, verified

* upload
* download
* request structures
* parameters
* scopes
* config-dir
* debug[-auth]

Fixes #81
  • Loading branch information
Byron committed Apr 30, 2015
1 parent b039b38 commit d0ce221
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/mako/cli/lib/argparse.mako
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
elif isinstance(v, basestring):
v = '"%s"' % v
elif isinstance(v, list):
v = 'vec![%s]' % ','.join('CallType::Upload%s' % p.capitalize() for p in v)
v = 'vec![%s]' % ','.join('UploadProtocol::%s' % p.capitalize() for p in v)
return 'Some(%s)' % v
%>\
<%def name="grammar(c)">\
Expand Down Expand Up @@ -214,7 +214,7 @@ let arg_data = [
% if not mc.media_params:
## Make sure the type is set, even though we don't have any protocol information
% if loop.first:
None::${'<Vec<CallType>>'}\
None::${'<Vec<UploadProtocol>>'}\
% else:
None\
% endif
Expand Down
8 changes: 3 additions & 5 deletions src/mako/cli/lib/engine.mako
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@
if (ptype not in POD_TYPES or ptype is None or p.get('repeated', False)) and ptype is not None:
borrow = '&'
return borrow
STANDARD = 'standard-request'
%>\
<%def name="new(c)">\
<%
hub_type_name = 'api::' + hub_type(c.schemas, util.canonical_name())
%>\
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};
input_file_from_opts, input_mime_from_opts, FieldCursor, FieldError, CallType, UploadProtocol};
use std::default::Default;
use std::str::FromStr;
Expand Down Expand Up @@ -256,7 +254,7 @@ ${value_unwrap}\
}
% endif # handle call parameters
% if mc.media_params:
let protocol = CallType::from(${req_value(MODE_ARG)});
let protocol = CallType::Upload(UploadProtocol::from(${req_value(MODE_ARG)}));
let mut input_file = input_file_from_opts(${req_value(FILE_ARG)}, err);
let mime_type = input_mime_from_opts(${req_value(MIME_ARG)}, err);
% else:
Expand All @@ -279,7 +277,7 @@ if dry_run {
match match protocol {
% if mc.media_params:
% for p in mc.media_params:
CallType::Upload${p.protocol.capitalize()} => call.${upload_action_fn(api.terms.upload_action, p.type.suffix)}(input_file.unwrap(), mime_type.unwrap()),
CallType::Upload(UploadProtocol::${p.protocol.capitalize()}) => call.${upload_action_fn(api.terms.upload_action, p.type.suffix)}(input_file.unwrap(), mime_type.unwrap()),
% endfor
CallType::Standard => unreachable!()
% else:
Expand Down
30 changes: 21 additions & 9 deletions src/rust/cli/cmn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,39 @@ use std::default::Default;
const FIELD_SEP: char = '.';

pub enum CallType {
UploadSimple,
UploadResumable,
Upload(UploadProtocol),
Standard,
}

pub enum UploadProtocol {
Simple,
Resumable,
}

impl AsRef<str> for UploadProtocol {
fn as_ref(&self) -> &str {
match *self {
UploadProtocol::Simple => "simple",
UploadProtocol::Resumable => "resumable"
}
}
}

impl AsRef<str> for CallType {
fn as_ref(&self) -> &str {
match *self {
CallType::UploadSimple => "simple",
CallType::UploadResumable => "resumable",
CallType::Upload(ref proto) => proto.as_ref(),
CallType::Standard => "standard-request"
}
}
}

impl<'a> From<&'a str> for CallType {
fn from(this: &'a str) -> CallType {
impl<'a> From<&'a str> for UploadProtocol {
fn from(this: &'a str) -> UploadProtocol {
match this {
"simple" => CallType::UploadSimple,
"resumable" => CallType::UploadResumable,
_ => unreachable!(),
"simple" => UploadProtocol::Simple,
"resumable" => UploadProtocol::Resumable,
_ => panic!("We don't expect to see anything else here, the CLI parser takes care")
}
}
}
Expand Down

0 comments on commit d0ce221

Please sign in to comment.