Merge branch 'next'

This commit is contained in:
Sebastian Thiel
2015-06-19 13:25:58 +02:00
26 changed files with 693 additions and 1439 deletions

View File

@@ -1,4 +1,6 @@
language: rust
rust:
- stable
script:
- make docs-all gen-all-cli cargo-api ARGS=test
env:

View File

@@ -10,7 +10,6 @@ To find a library of your interest, you might want to proceed looking at the [AP
* first-class documentation with cross-links and complete code-examples
* support all features, including downloads and resumable uploads
* safety and resilience are built-in, allowing you to create highly available tools on top of it. For example, you can trigger retries for all operations that may temporarily fail, e.g. due to network outage.
* *(soon)* Feature-complete command line tool to interact with each API.
# Live-Development
@@ -46,8 +45,9 @@ To generate the APIs yourself, you will need to meet the following prerequisites
* As [*mako*][mako] is a python program, you will need python installed on your system to run it. Some other programs we call depend on python being present as well.
* **an internet connection and wget**
* Make will download all other prerequisites automatically into hidden directories within this repository, which requires it to make some downloads via wget.
* **Rust beta/nightly**
* Due to the reliance on unstable dependencies, a beta or nightly build of the Rust toolchain is required. [Multirust][multirust] is recommended to support multiple toolchains on a per-project basis.
* **Rust Stable**
* This project compiles on *stable* Rust only. You might consider using [Multirust][multirust] to control
the toolchain on a per-project basis
## Using Make

View File

@@ -18,13 +18,15 @@ make:
- source: README.md
- source: ../LICENSE.md
- source: ../Cargo.toml
- source: lib.rs.in
output_dir: src
- source: lib.rs
output_dir: src
- source: build.rs
output_dir: src
cargo:
build_version: "0.1.7"
build_version: "0.1.8"
build_script: src/build.rs
keywords: [protocol, web, api]
dependencies:
- url = "*"
- serde = ">= 0.3.0"
- serde_macros = "*"
- json-tools = ">= 0.3.0"

View File

@@ -22,12 +22,10 @@ make:
- source: main.rs
output_dir: src
cargo:
build_version: "0.2.0"
build_version: "0.3.0"
keywords: [cli]
is_executable: YES
dependencies:
- strsim = "*"
- yup-hyper-mock = "*"
- serde = ">= 0.3.0"
- serde_macros = "*"
- yup-hyper-mock = ">=1.0.0"
- clap = ">= 0.9.1"

View File

@@ -17,14 +17,20 @@ keywords = ["discovery", "google", "cli"]
name = "discovery1"
[dependencies]
hyper = ">= 0.4.0"
hyper = ">= 0.5.0"
mime = "*"
yup-oauth2 = "*"
strsim = "*"
yup-hyper-mock = "*"
serde = ">= 0.3.0"
serde_macros = "*"
clap = "*"
strsim = "*"
yup-hyper-mock = ">=1.0.0"
clap = ">= 0.9.1"
# to adapt to hyper changes ...
[dependencies.yup-oauth2]
version = "*"
git = "https://github.com/Byron/yup-oauth2"
rev = "598f5ed496077e9edca36ff95e2ab352c5b22d0f"
[dependencies.google-discovery1]
path = "../discovery1"

View File

@@ -2,6 +2,7 @@
// DO NOT EDIT
use oauth2::{ApplicationSecret, ConsoleApplicationSecret, TokenStorage, Token};
use serde::json;
use serde::json::value::Value;
use mime::Mime;
use clap::{App, SubCommand};
use strsim;
@@ -19,11 +20,38 @@ use std::default::Default;
const FIELD_SEP: char = '.';
pub enum ComplexType {
Pod,
Vec,
Map,
}
// Null,
// Bool(bool),
// I64(i64),
// U64(u64),
// F64(f64),
// String(String),
pub enum JsonType {
Boolean,
Int,
Uint,
Float,
String,
}
pub struct JsonTypeInfo {
pub jtype: JsonType,
pub ctype: ComplexType,
}
// Based on @erickt user comment. Thanks for the idea !
// Remove all keys whose values are null from given value (changed in place)
pub fn remove_json_null_values(value: &mut json::value::Value) {
pub fn remove_json_null_values(value: &mut Value) {
match *value {
json::value::Value::Object(ref mut map) => {
Value::Object(ref mut map) => {
let mut for_removal = Vec::new();
for (key, mut value) in map.iter_mut() {
@@ -96,6 +124,14 @@ impl ToString for FieldCursor {
}
}
impl From<&'static str> for FieldCursor {
fn from(value: &'static str) -> FieldCursor {
let mut res = FieldCursor::default();
res.set(value).unwrap();
res
}
}
impl FieldCursor {
pub fn set(&mut self, value: &str) -> Result<(), CLIError> {
if value.len() == 0 {
@@ -201,6 +237,78 @@ impl FieldCursor {
}
}
pub fn set_json_value(&self, mut object: &mut Value,
value: &str, type_info: JsonTypeInfo,
err: &mut InvalidOptionsError,
orig_cursor: &FieldCursor) {
assert!(self.0.len() > 0);
for field in &self.0[..self.0.len()-1] {
let tmp = object;
object =
match *tmp {
Value::Object(ref mut mapping) => {
mapping.entry(field.to_owned()).or_insert(
Value::Object(Default::default())
)
},
_ => panic!("We don't expect non-object Values here ...")
};
}
match *object {
Value::Object(ref mut mapping) => {
let field = &self.0[self.0.len()-1];
let to_jval =
|value: &str, jtype: JsonType, err: &mut InvalidOptionsError|
-> Value {
match jtype {
JsonType::Boolean =>
Value::Bool(arg_from_str(value, err, &field, "boolean")),
JsonType::Int =>
Value::I64(arg_from_str(value, err, &field, "int")),
JsonType::Uint =>
Value::U64(arg_from_str(value, err, &field, "uint")),
JsonType::Float =>
Value::F64(arg_from_str(value, err, &field, "float")),
JsonType::String =>
Value::String(value.to_owned()),
}
};
match type_info.ctype {
ComplexType::Pod => {
if mapping.insert(field.to_owned(), to_jval(value, type_info.jtype, err)).is_some() {
err.issues.push(CLIError::Field(FieldError::Duplicate(orig_cursor.to_string())));
}
},
ComplexType::Vec => {
match *mapping.entry(field.to_owned())
.or_insert(Value::Array(Default::default())) {
Value::Array(ref mut values) => values.push(to_jval(value, type_info.jtype, err)),
_ => unreachable!()
}
},
ComplexType::Map => {
let (key, value) = parse_kv_arg(value, err, true);
let jval = to_jval(value.unwrap_or(""), type_info.jtype, err);
match *mapping.entry(field.to_owned())
.or_insert(Value::Object(Default::default())) {
Value::Object(ref mut value_map) => {
if value_map.insert(key.to_owned(), jval).is_some() {
err.issues.push(CLIError::Field(FieldError::Duplicate(orig_cursor.to_string())));
}
}
_ => unreachable!()
}
}
}
},
_ => unreachable!()
}
}
pub fn num_fields(&self) -> usize {
self.0.len()
}
@@ -268,15 +376,15 @@ pub fn writer_from_opts(arg: Option<&str>) -> Result<Box<Write>, io::Error> {
}
pub fn arg_from_str<T>(arg: &str, err: &mut InvalidOptionsError,
arg_name: &'static str,
arg_type: &'static str) -> T
pub fn arg_from_str<'a, T>(arg: &str, err: &mut InvalidOptionsError,
arg_name: &'a str,
arg_type: &'a str) -> T
where T: FromStr + Default,
<T as FromStr>::Err: fmt::Display {
match FromStr::from_str(arg) {
Err(perr) => {
err.issues.push(
CLIError::ParseError(arg_name, arg_type, arg.to_string(), format!("{}", perr))
CLIError::ParseError(arg_name.to_owned(), arg_type.to_owned(), arg.to_string(), format!("{}", perr))
);
Default::default()
},
@@ -411,6 +519,7 @@ pub enum FieldError {
PopOnEmpty(String),
TrailingFieldSep(String),
Unknown(String, Option<String>, Option<String>),
Duplicate(String),
Empty,
}
@@ -437,6 +546,8 @@ impl fmt::Display for FieldError {
};
writeln!(f, "Field '{}' does not exist.{}", field, suffix)
},
FieldError::Duplicate(ref cursor)
=> writeln!(f, "Value at '{}' was already set", cursor),
FieldError::Empty
=> writeln!(f, "Field names must not be empty."),
}
@@ -447,7 +558,7 @@ impl fmt::Display for FieldError {
#[derive(Debug)]
pub enum CLIError {
Configuration(ConfigurationError),
ParseError(&'static str, &'static str, String, String),
ParseError(String, String, String, String),
UnknownParameter(String, Vec<&'static str>),
InvalidUploadProtocol(String, Vec<String>),
InvalidKeyValueSyntax(String, bool),
@@ -465,7 +576,7 @@ impl fmt::Display for CLIError {
CLIError::Field(ref err) => write!(f, "Field -> {}", err),
CLIError::InvalidUploadProtocol(ref proto_name, ref valid_names)
=> writeln!(f, "'{}' is not a valid upload protocol. Choose from one of {}.", proto_name, valid_names.connect(", ")),
CLIError::ParseError(arg_name, type_name, ref value, ref err_desc)
CLIError::ParseError(ref arg_name, ref type_name, ref value, ref err_desc)
=> writeln!(f, "Failed to parse argument '{}' with value '{}' as {} with error: {}.",
arg_name, value, type_name, err_desc),
CLIError::UnknownParameter(ref param_name, ref possible_values) => {

View File

@@ -22,7 +22,7 @@ mod cmn;
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,
calltype_from_str, remove_json_null_values};
calltype_from_str, remove_json_null_values, ComplexType, JsonType, JsonTypeInfo};
use std::default::Default;
use std::str::FromStr;
@@ -61,9 +61,11 @@ impl<'n, 'a> Engine<'n, 'a> {
}
}
if !found {
err.issues.push(CLIError::UnknownParameter(key.to_string(),
Vec::new() + &self.gp + &[]
));
err.issues.push(CLIError::UnknownParameter(key.to_string(),
{let mut v = Vec::new();
v.extend(self.gp.iter().map(|v|*v));
v.extend([].iter().map(|v|*v));
v } ));
}
}
}
@@ -114,9 +116,11 @@ impl<'n, 'a> Engine<'n, 'a> {
}
}
if !found {
err.issues.push(CLIError::UnknownParameter(key.to_string(),
Vec::new() + &self.gp + &["name", "preferred"]
));
err.issues.push(CLIError::UnknownParameter(key.to_string(),
{let mut v = Vec::new();
v.extend(self.gp.iter().map(|v|*v));
v.extend(["name", "preferred"].iter().map(|v|*v));
v } ));
}
}
}
@@ -332,7 +336,8 @@ fn main() {
(_ , &Some(f)) => f,
_ => unreachable!(),
};
let mut arg = Arg::with_name(arg_name_str);
let mut arg = Arg::with_name(arg_name_str)
.empty_values(false);
if let &Some(short_flag) = flag {
arg = arg.short(short_flag);
}

View File

@@ -12,13 +12,17 @@ homepage = "https://developers.google.com/discovery/"
documentation = "http://byron.github.io/google-apis-rs/google_discovery1"
license = "MIT"
keywords = ["discovery", "google", "protocol", "web", "api"]
build = "src/build.rs"
[dependencies]
hyper = ">= 0.4.0"
hyper = ">= 0.5.0"
mime = "*"
serde = ">= 0.3.0"
yup-oauth2 = "*"
url = "*"
serde = ">= 0.3.0"
serde_macros = "*"
json-tools = ">= 0.3.0"
[build-dependencies]
syntex = { version = "*" }
serde_codegen = { version = "*" }

View File

@@ -290,7 +290,7 @@ impl Display for Error {
writeln!(f, "The media size {} exceeds the maximum allowed upload size of {}"
, resource_size, max_size),
Error::MissingAPIKey => {
writeln!(f, "The application's API key was not found in the configuration").ok();
(writeln!(f, "The application's API key was not found in the configuration")).ok();
writeln!(f, "It is used as there are no Scopes defined for this method.")
},
Error::BadRequest(ref err) => {
@@ -424,8 +424,8 @@ impl<'a> Read for MultiPartReader<'a> {
(n, true, _) if n > 0 => {
let (headers, reader) = self.raw_parts.remove(0);
let mut c = Cursor::new(Vec::<u8>::new());
write!(&mut c, "{}--{}{}{}{}", LINE_ENDING, BOUNDARY, LINE_ENDING,
headers, LINE_ENDING).unwrap();
(write!(&mut c, "{}--{}{}{}{}", LINE_ENDING, BOUNDARY, LINE_ENDING,
headers, LINE_ENDING)).unwrap();
c.seek(SeekFrom::Start(0)).unwrap();
self.current_part = Some((c, reader));
}
@@ -471,7 +471,7 @@ impl<'a> Read for MultiPartReader<'a> {
}
}
header!{
header!(
#[doc="The `X-Upload-Content-Type` header."]
(XUploadContentType, "X-Upload-Content-Type") => [Mime]
@@ -484,7 +484,7 @@ header!{
)));
}
}
);
#[derive(Clone, PartialEq, Debug)]
pub struct Chunk {
@@ -494,7 +494,7 @@ pub struct Chunk {
impl fmt::Display for Chunk {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "{}-{}", self.first, self.last).ok();
(write!(fmt, "{}-{}", self.first, self.last)).ok();
Ok(())
}
}
@@ -549,7 +549,7 @@ impl HeaderFormat for ContentRange {
Some(ref c) => try!(c.fmt(fmt)),
None => try!(fmt.write_str("*"))
}
write!(fmt, "/{}", self.total_length).ok();
(write!(fmt, "/{}", self.total_length)).ok();
Ok(())
}
}
@@ -668,12 +668,14 @@ impl<'a, A> ResumableUploadHelper<'a, A>
if self.delegate.cancel_chunk_upload(&range_header) {
return None
}
match self.client.post(self.url)
.header(range_header)
.header(ContentType(self.media_type.clone()))
.header(UserAgent(self.user_agent.to_string()))
.body(&mut section_reader)
.send() {
// workaround https://github.com/rust-lang/rust/issues/22252
let res = self.client.post(self.url)
.header(range_header)
.header(ContentType(self.media_type.clone()))
.header(UserAgent(self.user_agent.to_string()))
.body(&mut section_reader)
.send();
match res {
Ok(mut res) => {
if res.status == StatusCode::PermanentRedirect {
continue
@@ -700,4 +702,28 @@ impl<'a, A> ResumableUploadHelper<'a, A>
}
}
}
}
}
use serde::json::value::Value;
// Copy of src/rust/cli/cmn.rs
// TODO(ST): Allow sharing common code between program types
pub fn remove_json_null_values(value: &mut Value) {
match *value {
Value::Object(ref mut map) => {
let mut for_removal = Vec::new();
for (key, mut value) in map.iter_mut() {
if value.is_null() {
for_removal.push(key.clone());
} else {
remove_json_null_values(&mut value);
}
}
for key in &for_removal {
map.remove(key);
}
}
_ => {}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -17,14 +17,30 @@ keywords = ["groupsmigration", "google", "cli"]
name = "groupsmigration1"
[dependencies]
hyper = ">= 0.4.0"
mime = "*"
yup-oauth2 = "*"
strsim = "*"
yup-hyper-mock = "*"
serde = ">= 0.3.0"
serde_macros = "*"
strsim = "*"
clap = "*"
[dependencies.yup-hyper-mock]
version = "*"
git = "https://github.com/Byron/yup-hyper-mock"
rev = "ee56de4dead136b3ca5a3eda6ca7057f9074e261"
# Needed for latest fix in macros !
[dependencies.hyper]
version = ">= 0.4.0"
git = "https://github.com/hyperium/hyper"
rev = "871f37a5605d433e5699ed2f16631001d86d7805"
# to adapt to hyper changes ...
[dependencies.yup-oauth2]
version = "*"
git = "https://github.com/Byron/yup-oauth2"
rev = "94d5b7c2cac02ad67da8504504364b3081a9a866"
[dependencies.google-groupsmigration1]
path = "../groupsmigration1"

View File

@@ -13,6 +13,15 @@ If data-structures are requested, these will be returned as pretty-printed JSON,
Everything else about the *Groups Migration* API can be found at the
[official documentation site](https://developers.google.com/google-apps/groups-migration/).
# Downloads
You can download the pre-compiled 64bit binaries for the following platforms:
* ![icon](http://megaicons.net/static/img/icons_sizes/6/140/16/ubuntu-icon.png) [ubuntu](http://dl.byronimo.de/google.rs/cli/0.2.0/ubuntu/groupsmigration1.tar.gz)
* ![icon](http://hydra-media.cursecdn.com/wow.gamepedia.com/a/a2/Apple-icon-16x16.png?version=25ddd67ac3dd3b634478e3978b76cb74) [osx](http://dl.byronimo.de/google.rs/cli/0.2.0/osx/groupsmigration1.tar.gz)
Find the source code [on github](https://github.com/Byron/google-apis-rs/tree/master/gen/groupsmigration1-cli).
# Usage
This documentation was generated from the *Groups Migration* API at revision *20140416*. The CLI is at version *0.2.0*.

View File

@@ -61,9 +61,11 @@ impl<'n, 'a> Engine<'n, 'a> {
}
}
if !found {
err.issues.push(CLIError::UnknownParameter(key.to_string(),
Vec::new() + &self.gp + &[]
));
err.issues.push(CLIError::UnknownParameter(key.to_string(),
{let mut v = Vec::new();
v.extend(self.gp.iter().map(|v|*v));
v.extend([].iter().map(|v|*v));
v } ));
}
}
}

View File

@@ -15,10 +15,22 @@ keywords = ["groupsmigration", "google", "protocol", "web", "api"]
[dependencies]
hyper = ">= 0.4.0"
mime = "*"
yup-oauth2 = "*"
url = "*"
serde = ">= 0.3.0"
serde_macros = "*"
url = "*"
json-tools = ">= 0.3.0"
# Needed for latest fix in macros !
[dependencies.hyper]
version = ">= 0.4.0"
git = "https://github.com/hyperium/hyper"
rev = "871f37a5605d433e5699ed2f16631001d86d7805"
# to adapt to hyper changes ...
[dependencies.yup-oauth2]
version = "*"
git = "https://github.com/Byron/yup-oauth2"
rev = "94d5b7c2cac02ad67da8504504364b3081a9a866"

View File

@@ -668,12 +668,14 @@ impl<'a, A> ResumableUploadHelper<'a, A>
if self.delegate.cancel_chunk_upload(&range_header) {
return None
}
match self.client.post(self.url)
.header(range_header)
.header(ContentType(self.media_type.clone()))
.header(UserAgent(self.user_agent.to_string()))
.body(&mut section_reader)
.send() {
// workaround https://github.com/rust-lang/rust/issues/22252
let res = self.client.post(self.url)
.header(range_header)
.header(ContentType(self.media_type.clone()))
.header(UserAgent(self.user_agent.to_string()))
.body(&mut section_reader)
.send();
match res {
Ok(mut res) => {
if res.status == StatusCode::PermanentRedirect {
continue

View File

@@ -16,6 +16,9 @@ homepage = "${documentationLink}"
documentation = "${cargo.doc_base_url}/${to_extern_crate_name(util.crate_name())}"
license = "${copyright.license_abbrev}"
keywords = ["${name[:20]}", ${", ".join(estr(cargo.keywords))}]
% if cargo.get('build_script'):
build = "${cargo.build_script}"
% endif
% if cargo.get('is_executable', False):
[[bin]]
@@ -23,12 +26,19 @@ name = "${util.program_name()}"
% endif
[dependencies]
hyper = ">= 0.5.0"
mime = "*"
hyper = ">= 0.5.2"
## Must match the one hyper uses, otherwise there are duplicate similarly named `Mime` structs
mime = "0.0.11"
serde = ">= 0.4.1"
yup-oauth2 = "*"
% for dep in cargo.get('dependencies', list()):
${dep}
% endfor
[build-dependencies]
syntex = { version = "*" }
serde_codegen = { version = "*" }
% if make.depends_on_suffix is not None:
<% api_name = util.library_name() %>\

View File

@@ -0,0 +1,17 @@
<%namespace name="util" file="../lib/util.mako"/>\
extern crate syntex;
extern crate serde_codegen;
use std::env;
use std::path::Path;
pub fn main() {
let out_dir = env::var_os("OUT_DIR").unwrap();
let src = Path::new("src/lib.rs.in");
let dst = Path::new(&out_dir).join("lib.rs");
let mut registry = syntex::Registry::new();
serde_codegen::register(&mut registry);
registry.expand("${util.crate_name()}", &src, &dst).unwrap();
}

133
src/mako/api/lib.rs.in.mako Normal file
View File

@@ -0,0 +1,133 @@
<%namespace name="lib" file="lib/lib.mako"/>\
<%namespace name="util" file="../lib/util.mako"/>\
<%namespace name="rbuild" file="lib/rbuild.mako"/>\
<%namespace name="mbuild" file="lib/mbuild.mako"/>\
<%namespace name="schema" file="lib/schema.mako"/>\
<%
from util import (new_context, rust_comment, rust_doc_comment, rust_module_doc_comment,
rb_type, hub_type, mangle_ident, hub_type_params_s, hub_type_bounds,
rb_type_params_s, find_fattest_resource, HUB_TYPE_PARAMETERS, METHODS_RESOURCE,
UNUSED_TYPE_MARKER, schema_markers)
c = new_context(schemas, resources, context.get('methods'))
hub_type = hub_type(c.schemas, util.canonical_name())
ht_params = hub_type_params_s()
default_user_agent = "google-api-rust-client/" + cargo.build_version
%>\
<%block filter="rust_comment">\
<%util:gen_info source="${self.uri}" />\
</%block>
extern crate hyper;
extern crate serde;
extern crate yup_oauth2 as oauth2;
extern crate mime;
extern crate url;
mod cmn;
use std::collections::HashMap;
use std::cell::RefCell;
use std::borrow::BorrowMut;
use std::default::Default;
use std::collections::BTreeMap;
use serde::json;
use std::io;
use std::fs;
use std::thread::sleep_ms;
pub use cmn::{MultiPartReader, ToParts, MethodInfo, Result, Error, CallBuilder, Hub, ReadSeek, Part,
ResponseResult, RequestValue, NestedType, Delegate, DefaultDelegate, MethodsBuilder,
Resource, ErrorResponse, remove_json_null_values};
// ##############
// UTILITIES ###
// ############
${lib.scope_enum()}
// ########
// HUB ###
// ######
/// Central instance to access all ${hub_type} related resource activities
///
/// # Examples
///
/// Instantiate a new hub
///
<%block filter="rust_doc_comment">\
${lib.hub_usage_example(c)}\
</%block>
pub struct ${hub_type}${ht_params} {
client: RefCell<C>,
auth: RefCell<A>,
_user_agent: String,
}
impl<'a, ${', '.join(HUB_TYPE_PARAMETERS)}> Hub for ${hub_type}${ht_params} {}
impl<'a, ${', '.join(HUB_TYPE_PARAMETERS)}> ${hub_type}${ht_params}
where ${', '.join(hub_type_bounds())} {
pub fn new(client: C, authenticator: A) -> ${hub_type}${ht_params} {
${hub_type} {
client: RefCell::new(client),
auth: RefCell::new(authenticator),
_user_agent: "${default_user_agent}".to_string(),
}
}
% for resource in sorted(c.rta_map.keys()):
pub fn ${mangle_ident(resource)}(&'a self) -> ${rb_type(resource)}${rb_type_params_s(resource, c)} {
${rb_type(resource)} { hub: &self }
}
% endfor
/// Set the user-agent header field to use in all requests to the server.
/// It defaults to `${default_user_agent}`.
///
/// Returns the previously set user-agent.
pub fn user_agent(&mut self, agent_name: String) -> String {
let prev = self._user_agent.clone();
self._user_agent = agent_name;
prev
}
}
% if c.schemas:
// ############
// SCHEMAS ###
// ##########
% for s in c.schemas.values():
% if UNUSED_TYPE_MARKER not in schema_markers(s, c, transitive=True):
${schema.new(s, c)}
% endif
% endfor
% endif
// ###################
// MethodBuilders ###
// #################
% for resource in c.rta_map:
${rbuild.new(resource, c)}
% endfor
// ###################
// CallBuilders ###
// #################
% for resource, methods in c.rta_map.iteritems():
% for method in methods:
${mbuild.new(resource, method, c)}
% endfor ## method in methods
% endfor ## resource, methods

View File

@@ -1,19 +1,9 @@
<%namespace name="lib" file="lib/lib.mako"/>\
<%namespace name="util" file="../lib/util.mako"/>\
<%namespace name="rbuild" file="lib/rbuild.mako"/>\
<%namespace name="mbuild" file="lib/mbuild.mako"/>\
<%namespace name="schema" file="lib/schema.mako"/>\
<%
from util import (new_context, rust_comment, rust_doc_comment, rust_module_doc_comment,
rb_type, hub_type, mangle_ident, hub_type_params_s, hub_type_bounds,
rb_type_params_s, find_fattest_resource, HUB_TYPE_PARAMETERS, METHODS_RESOURCE,
UNUSED_TYPE_MARKER, schema_markers)
from util import (new_context, rust_comment, rust_module_doc_comment)
c = new_context(schemas, resources, context.get('methods'))
hub_type = hub_type(c.schemas, util.canonical_name())
ht_params = hub_type_params_s()
default_user_agent = "google-api-rust-client/" + cargo.build_version
%>\
<%block filter="rust_comment">\
<%util:gen_info source="${self.uri}" />\
@@ -22,124 +12,11 @@
<%block filter="rust_module_doc_comment">\
${lib.docs(c)}
</%block>
// Unused attributes happen thanks to defined, but unused structures
// We don't warn about this, as depending on the API, some data structures or facilities are never used.
// Instead of pre-determining this, we just disable the lint. It's manually tuned to not have any
// unused imports in fully featured APIs. Same with unused_mut ... .
#![allow(unused_imports, unused_mut, dead_code)]
// Required for serde annotations
#![feature(custom_derive, custom_attribute, plugin, slice_patterns)]
#![plugin(serde_macros)]
#[macro_use]
extern crate hyper;
extern crate serde;
extern crate yup_oauth2 as oauth2;
extern crate mime;
extern crate url;
extern crate json_tools;
mod cmn;
use std::collections::HashMap;
use std::cell::RefCell;
use std::borrow::BorrowMut;
use std::default::Default;
use std::collections::BTreeMap;
use serde::json;
use std::io;
use std::fs;
use std::thread::sleep_ms;
pub use cmn::{MultiPartReader, ToParts, MethodInfo, Result, Error, CallBuilder, Hub, ReadSeek, Part, ResponseResult, RequestValue, NestedType, Delegate, DefaultDelegate, MethodsBuilder, Resource, ErrorResponse};
// ##############
// UTILITIES ###
// ############
${lib.scope_enum()}
// ########
// HUB ###
// ######
/// Central instance to access all ${hub_type} related resource activities
///
/// # Examples
///
/// Instantiate a new hub
///
<%block filter="rust_doc_comment">\
${lib.hub_usage_example(c)}\
</%block>
pub struct ${hub_type}${ht_params} {
client: RefCell<C>,
auth: RefCell<A>,
_user_agent: String,
}
impl<'a, ${', '.join(HUB_TYPE_PARAMETERS)}> Hub for ${hub_type}${ht_params} {}
impl<'a, ${', '.join(HUB_TYPE_PARAMETERS)}> ${hub_type}${ht_params}
where ${', '.join(hub_type_bounds())} {
pub fn new(client: C, authenticator: A) -> ${hub_type}${ht_params} {
${hub_type} {
client: RefCell::new(client),
auth: RefCell::new(authenticator),
_user_agent: "${default_user_agent}".to_string(),
}
}
% for resource in sorted(c.rta_map.keys()):
pub fn ${mangle_ident(resource)}(&'a self) -> ${rb_type(resource)}${rb_type_params_s(resource, c)} {
${rb_type(resource)} { hub: &self }
}
% endfor
/// Set the user-agent header field to use in all requests to the server.
/// It defaults to `${default_user_agent}`.
///
/// Returns the previously set user-agent.
pub fn user_agent(&mut self, agent_name: String) -> String {
let prev = self._user_agent.clone();
self._user_agent = agent_name;
prev
}
}
% if c.schemas:
// ############
// SCHEMAS ###
// ##########
% for s in c.schemas.values():
% if UNUSED_TYPE_MARKER not in schema_markers(s, c, transitive=True):
${schema.new(s, c)}
% endif
% endfor
% endif
// ###################
// MethodBuilders ###
// #################
% for resource in c.rta_map:
${rbuild.new(resource, c)}
% endfor
// ###################
// CallBuilders ###
// #################
% for resource, methods in c.rta_map.iteritems():
% for method in methods:
${mbuild.new(resource, method, c)}
% endfor ## method in methods
% endfor ## resource, methods
include!(concat!(env!("OUT_DIR"), "/lib.rs"));

View File

@@ -483,9 +483,6 @@ match result {
% if URL_ENCODE in special_cases:
use url::percent_encoding::{percent_encode, FORM_URLENCODED_ENCODE_SET};
% endif
% if request_value:
use json_tools::{TokenReader, Lexer, BufferType, TokenType, FilterTypedKeyValuePairs, IteratorExt};
% endif
use std::io::{Read, Seek};
use hyper::header::{ContentType, ContentLength, Authorization, UserAgent, Location};
let mut dd = DefaultDelegate;
@@ -618,16 +615,16 @@ else {
## Hanlde URI Tempates
% if replacements:
for &(find_this, param_name) in [${', '.join('("%s", "%s")' % r for r in replacements)}].iter() {
<%
replace_init = ': Option<&str> = None'
replace_assign = 'Some(value)'
url_replace_arg = 'replace_with.expect("to find substitution value in params")'
if URL_ENCODE in special_cases:
replace_init = ' = String::new()'
replace_assign = 'value.to_string()'
url_replace_arg = '&replace_with'
# end handle url encoding
%>\
<%
replace_init = ': Option<&str> = None'
replace_assign = 'Some(value)'
url_replace_arg = 'replace_with.expect("to find substitution value in params")'
if URL_ENCODE in special_cases:
replace_init = ' = String::new()'
replace_assign = 'value.to_string()'
url_replace_arg = '&replace_with'
# end handle url encoding
%>\
let mut replace_with${replace_init};
for &(name, ref value) in params.iter() {
if name == param_name {
@@ -645,12 +642,9 @@ else {
## Remove all used parameters
{
let mut indices_for_removal: Vec<usize> = Vec::with_capacity(${len(replacements)});
for param_name in [${', '.join('"%s"' % r[1] for r in replacements)}].iter() {
for (index, &(ref name, _)) in params.iter().rev().enumerate() {
if name == param_name {
indices_for_removal.push(params.len() - index - 1);
break;
}
for param_name in [${', '.join(reversed(['"%s"' % r[1] for r in replacements]))}].iter() {
if let Some(index) = params.iter().position(|t| &t.0 == param_name) {
indices_for_removal.push(index);
}
}
for &index in indices_for_removal.iter() {
@@ -668,13 +662,11 @@ else {
let mut json_mime_type = mime::Mime(mime::TopLevel::Application, mime::SubLevel::Json, Default::default());
let mut request_value_reader =
{
let json_cache = json::to_string(&self.${property(REQUEST_VALUE_PROPERTY_NAME)}).unwrap();
let mut mem_dst = io::Cursor::new(Vec::with_capacity(json_cache.len()));
io::copy(&mut Lexer::new(json_cache.bytes(), BufferType::Span)
.filter_key_value_by_type(TokenType::Null)
.reader(Some(&json_cache)), &mut mem_dst).unwrap();
mem_dst.seek(io::SeekFrom::Start(0)).unwrap();
mem_dst
let mut value = json::value::to_value(&self.${property(REQUEST_VALUE_PROPERTY_NAME)});
remove_json_null_values(&mut value);
let mut dst = io::Cursor::new(Vec::with_capacity(128));
json::to_writer(&mut dst, &value).unwrap();
dst
};
let request_size = request_value_reader.seek(io::SeekFrom::End(0)).unwrap();
request_value_reader.seek(io::SeekFrom::Start(0)).unwrap();

View File

@@ -55,15 +55,13 @@ ${struct};
markers = schema_markers(s, c, transitive=True)
# We always need Serialization support, as others might want to serialize the response, even though we will
# only deserialize it.
traits = ['Clone', 'Debug', 'Serialize']
# And since we don't know what others want to do, we implement Deserialize as well by default ...
traits = ['Clone', 'Debug', 'Serialize', 'Deserialize']
# default only works for structs, and 'variant' will be an enum
if 'variant' not in s:
traits.insert(0, 'Default')
if RESPONSE_MARKER_TRAIT in markers:
traits.append('Deserialize')
nt_markers = schema_markers(s, c, transitive=False)
allow_optionals = is_schema_with_optionals(nt_markers)

View File

@@ -59,6 +59,22 @@ JSON_TYPE_RND_MAP = {'boolean': lambda: str(bool(randint(0, 1))).lower(),
'int64' : lambda: randint(-101, -1),
'string': lambda: '%s' % choice(util.words).lower()}
JSON_TYPE_TO_ENUM_MAP = {'boolean' : 'Boolean',
'integer' : 'Int',
'number' : 'Float',
'uint32' : 'Int',
'double' : 'Float',
'float' : 'Float',
'int32' : 'Int',
'any' : 'String', # TODO: Figure out how to handle it. It's 'interface' in Go ...
'int64' : 'Int',
'uint64' : 'Uint',
'string' : 'String'}
CTYPE_TO_ENUM_MAP = {CTYPE_POD: 'Pod',
CTYPE_ARRAY: 'Vec',
CTYPE_MAP: 'Map'}
JSON_TYPE_VALUE_MAP = {'boolean': 'false',
'integer' : '-0',
'uint32' : '0',

View File

@@ -8,7 +8,8 @@
call_method_ident, POD_TYPES, opt_value, ident, JSON_TYPE_VALUE_MAP,
KEY_VALUE_ARG, to_cli_schema, SchemaEntry, CTYPE_POD, actual_json_type, CTYPE_MAP, CTYPE_ARRAY,
application_secret_path, DEBUG_FLAG, DEBUG_AUTH_FLAG, CONFIG_DIR_FLAG, req_value, MODE_ARG,
opt_values, SCOPE_ARG, CONFIG_DIR_ARG, DEFAULT_MIME, field_vec, comma_sep_fields)
opt_values, SCOPE_ARG, CONFIG_DIR_ARG, DEFAULT_MIME, field_vec, comma_sep_fields, JSON_TYPE_TO_ENUM_MAP,
CTYPE_TO_ENUM_MAP)
v_arg = '<%s>' % VALUE_ARG
SOPT = 'self.opt'
@@ -32,7 +33,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,
calltype_from_str, remove_json_null_values};
calltype_from_str, remove_json_null_values, ComplexType, JsonType, JsonTypeInfo};
use std::default::Default;
use std::str::FromStr;
@@ -322,7 +323,7 @@ if dry_run {
% if mc.response_schema:
let mut value = json::value::to_value(&output_schema);
remove_json_null_values(&mut value);
serde::json::to_writer_pretty(&mut ostream, &value).unwrap();
json::to_writer_pretty(&mut ostream, &value).unwrap();
% endif
% if track_download_flag:
} else {
@@ -344,14 +345,12 @@ if dry_run {
<%
allow_optionals_fn = lambda s: is_schema_with_optionals(schema_markers(s, c, transitive=False))
def flatten_schema_fields(schema, res, init_fn_map, fields, cur=list(), init_call=None):
def flatten_schema_fields(schema, res, fields, cur=list()):
if len(cur) == 0:
init_call = ''
cur = list()
opt_access = '.as_mut().unwrap()'
allow_optionals = allow_optionals_fn(schema)
parent_init_call = init_call
if not allow_optionals:
opt_access = ''
for fn, f in schema.fields.iteritems():
@@ -359,38 +358,20 @@ if dry_run {
fields.add(fn)
if isinstance(f, SchemaEntry):
cur[-1][0] = mangle_ident(fn)
res.append((init_call, schema, f, list(cur)))
res.append((schema, f, list(cur)))
else:
if allow_optionals:
init_fn_name = 'request_%s_init' % '_'.join(mangle_ident(t[1]) for t in cur)
init_call = init_fn_name + '(&mut request);'
struct_field = 'request.' + '.'.join('%s%s' % (mangle_ident(t[1]), opt_access) for t in cur[:-1])
if len(cur) > 1:
struct_field += '.'
struct_field += mangle_ident(cur[-1][1])
init_fn = "fn %s(request: &mut api::%s) {\n" % (init_fn_name, request_prop_type)
if parent_init_call:
pcall = parent_init_call[:parent_init_call.index('(')] + "(request)"
init_fn += " %s;\n" % pcall
init_fn += " if %s.is_none() {\n" % struct_field
init_fn += " %s = Some(Default::default());\n" % struct_field
init_fn += " }\n"
init_fn += "}\n"
init_fn_map[init_fn_name] = init_fn
# end handle init
flatten_schema_fields(f, res, init_fn_map, fields, cur, init_call)
flatten_schema_fields(f, res, fields, cur)
cur.pop()
# endfor
# end utility
schema_fields = list()
init_fn_map = dict()
fields = set()
flatten_schema_fields(request_cli_schema, schema_fields, init_fn_map, fields)
flatten_schema_fields(request_cli_schema, schema_fields, fields)
%>\
let mut ${request_prop_name} = api::${request_prop_type}::default();
let mut field_cursor = FieldCursor::default();
let mut object = json::value::Value::Object(Default::default());
for kvarg in ${opt_values(KEY_VALUE_ARG)} {
let last_errc = err.issues.len();
let (key, value) = parse_kv_arg(&*kvarg, err, false);
@@ -405,60 +386,28 @@ for kvarg in ${opt_values(KEY_VALUE_ARG)} {
}
continue;
}
% for name in sorted(init_fn_map.keys()):
${init_fn_map[name] | indent_by(4)}
% endfor
match &temp_cursor.to_string()[..] {
% for init_call, schema, fe, f in schema_fields:
let type_info =
match &temp_cursor.to_string()[..] {
% for schema, fe, f in schema_fields:
<%
ptype = actual_json_type(f[-1][1], fe.actual_property.type)
value_unwrap = 'value.unwrap_or("%s")' % JSON_TYPE_VALUE_MAP[ptype]
pname = FIELD_SEP.join(mangle_subcommand(t[1]) for t in f)
allow_optionals = True
opt_prefix = 'Some('
opt_suffix = ')'
struct_field = 'request.' + '.'.join(t[0] for t in f)
opt_init = 'if ' + struct_field + '.is_none() {\n'
opt_init += ' ' + struct_field + ' = Some(Default::default());\n'
opt_init += '}\n'
opt_access = '.as_mut().unwrap()'
if not allow_optionals_fn(schema):
opt_prefix = opt_suffix = opt_access = opt_init = ''
sname = FIELD_SEP.join(t[1] for t in f)
ptype = actual_json_type(f[-1][1], fe.actual_property.type)
jtype = 'JsonType::' + JSON_TYPE_TO_ENUM_MAP[ptype]
ctype = 'ComplexType::' + CTYPE_TO_ENUM_MAP[fe.container_type]
%>\
"${pname}" => {
% if init_call:
${init_call}
% endif
% if fe.container_type == CTYPE_POD:
${struct_field} = ${opt_prefix}\
% elif fe.container_type == CTYPE_ARRAY:
${opt_init | indent_all_but_first_by(4)}\
${struct_field}${opt_access}.push(\
% elif fe.container_type == CTYPE_MAP:
${opt_init | indent_all_but_first_by(4)}\
let (key, value) = parse_kv_arg(${value_unwrap}, err, true);
${struct_field}${opt_access}.insert(key.to_string(), \
% endif # container type handling
% if ptype != 'string':
arg_from_str(${value_unwrap}, err, "${pname}", "${ptype}")\
% else:
${value_unwrap}.to_string()\
% endif
% if fe.container_type == CTYPE_POD:
${opt_suffix}\
% else:
)\
% endif
;
},
% endfor # each nested field
_ => {
let suggestion = FieldCursor::did_you_mean(key, &${field_vec(sorted(fields))});
err.issues.push(CLIError::Field(FieldError::Unknown(temp_cursor.to_string(), suggestion, value.map(|v| v.to_string()))));
}
"${pname}" => Some(("${sname}", JsonTypeInfo { jtype: ${jtype}, ctype: ${ctype} })),
% endfor # each nested field
_ => {
let suggestion = FieldCursor::did_you_mean(key, &${field_vec(sorted(fields))});
err.issues.push(CLIError::Field(FieldError::Unknown(temp_cursor.to_string(), suggestion, value.map(|v| v.to_string()))));
None
}
};
if let Some((field_cursor_str, type_info)) = type_info {
FieldCursor::from(field_cursor_str).set_json_value(&mut object, value.unwrap(), type_info, err, &temp_cursor);
}
}
let mut ${request_prop_name}: api::${request_prop_type} = json::value::from_value(object).unwrap();
</%def>

View File

@@ -12,7 +12,6 @@
<%block filter="rust_comment">\
<%util:gen_info source="${self.uri}" />\
</%block>
#![feature(plugin, exit_status)]
#![allow(unused_variables, unused_imports, dead_code, unused_mut)]
#[macro_use]
@@ -34,18 +33,19 @@ mod cmn;
${engine.new(c)}\
fn main() {
let mut exit_status = 0i32;
${argparse.new(c) | indent_all_but_first_by(1)}\
let matches = app.get_matches();
let debug = matches.is_present("${DEBUG_FLAG}");
match Engine::new(matches) {
Err(err) => {
env::set_exit_status(err.exit_code);
exit_status = err.exit_code;
writeln!(io::stderr(), "{}", err).ok();
},
Ok(engine) => {
if let Err(doit_err) = engine.doit() {
env::set_exit_status(1);
exit_status = 1;
match doit_err {
DoitError::IoError(path, err) => {
writeln!(io::stderr(), "Failed to open output file '{}': {}", path, err).ok();
@@ -61,4 +61,6 @@ fn main() {
}
}
}
std::process::exit(exit_status);
}

View File

@@ -288,7 +288,7 @@ impl Display for Error {
writeln!(f, "The media size {} exceeds the maximum allowed upload size of {}"
, resource_size, max_size),
Error::MissingAPIKey => {
writeln!(f, "The application's API key was not found in the configuration").ok();
(writeln!(f, "The application's API key was not found in the configuration")).ok();
writeln!(f, "It is used as there are no Scopes defined for this method.")
},
Error::BadRequest(ref err) => {
@@ -422,8 +422,8 @@ impl<'a> Read for MultiPartReader<'a> {
(n, true, _) if n > 0 => {
let (headers, reader) = self.raw_parts.remove(0);
let mut c = Cursor::new(Vec::<u8>::new());
write!(&mut c, "{}--{}{}{}{}", LINE_ENDING, BOUNDARY, LINE_ENDING,
headers, LINE_ENDING).unwrap();
(write!(&mut c, "{}--{}{}{}{}", LINE_ENDING, BOUNDARY, LINE_ENDING,
headers, LINE_ENDING)).unwrap();
c.seek(SeekFrom::Start(0)).unwrap();
self.current_part = Some((c, reader));
}
@@ -469,18 +469,51 @@ impl<'a> Read for MultiPartReader<'a> {
}
}
header!{
#[doc="The `X-Upload-Content-Type` header."]
(XUploadContentType, "X-Upload-Content-Type") => [Mime]
// The following macro invocation needs to be expanded, as `include!`
// doens't support external macros
// header!{
// #[doc="The `X-Upload-Content-Type` header."]
// (XUploadContentType, "X-Upload-Content-Type") => [Mime]
xupload_content_type {
test_header!(
test1,
vec![b"text/plain"],
Some(HeaderField(
vec![Mime(TopLevel::Text, SubLevel::Plain, Vec::new())]
)));
// xupload_content_type {
// test_header!(
// test1,
// vec![b"text/plain"],
// Some(HeaderField(
// vec![Mime(TopLevel::Text, SubLevel::Plain, Vec::new())]
// )));
// }
// }
/// The `X-Upload-Content-Type` header.
///
/// Generated via rustc --pretty expanded -Z unstable-options, and manually
/// processed to be more readable.
#[derive(PartialEq, Debug, Clone)]
pub struct XUploadContentType(pub Mime);
impl ::std::ops::Deref for XUploadContentType {
type Target = Mime;
fn deref<'a>(&'a self) -> &'a Mime { &self.0 }
}
impl ::std::ops::DerefMut for XUploadContentType {
fn deref_mut<'a>(&'a mut self) -> &'a mut Mime { &mut self.0 }
}
impl Header for XUploadContentType {
fn header_name() -> &'static str { "X-Upload-Content-Type" }
fn parse_header(raw: &[Vec<u8>]) -> Option<Self> {
hyper::header::parsing::from_one_raw_str(raw).map(XUploadContentType)
}
}
impl HeaderFormat for XUploadContentType {
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
Display::fmt(&**self, f)
}
}
impl Display for XUploadContentType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&**self, f)
}
}
@@ -492,7 +525,7 @@ pub struct Chunk {
impl fmt::Display for Chunk {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "{}-{}", self.first, self.last).ok();
(write!(fmt, "{}-{}", self.first, self.last)).ok();
Ok(())
}
}
@@ -534,7 +567,7 @@ impl Header for ContentRange {
}
/// We are not parsable, as parsing is done by the `Range` header
fn parse_header(_: &[Vec<u8>]) -> Option<ContentRange> {
fn parse_header(_: &[Vec<u8>]) -> Option<Self> {
None
}
}
@@ -547,7 +580,7 @@ impl HeaderFormat for ContentRange {
Some(ref c) => try!(c.fmt(fmt)),
None => try!(fmt.write_str("*"))
}
write!(fmt, "/{}", self.total_length).ok();
(write!(fmt, "/{}", self.total_length)).ok();
Ok(())
}
}
@@ -560,8 +593,9 @@ impl Header for RangeResponseHeader {
"Range"
}
fn parse_header(raw: &[Vec<u8>]) -> Option<RangeResponseHeader> {
if let [ref v] = raw {
fn parse_header(raw: &[Vec<u8>]) -> Option<Self> {
if raw.len() > 0 {
let v = &raw[0];
if let Ok(s) = std::str::from_utf8(v) {
const PREFIX: &'static str = "bytes ";
if s.starts_with(PREFIX) {
@@ -700,4 +734,27 @@ impl<'a, A> ResumableUploadHelper<'a, A>
}
}
}
}
}
// Copy of src/rust/cli/cmn.rs
// TODO(ST): Allow sharing common code between program types
pub fn remove_json_null_values(value: &mut serde::json::value::Value) {
match *value {
serde::json::value::Value::Object(ref mut map) => {
let mut for_removal = Vec::new();
for (key, mut value) in map.iter_mut() {
if value.is_null() {
for_removal.push(key.clone());
} else {
remove_json_null_values(&mut value);
}
}
for key in &for_removal {
map.remove(key);
}
}
_ => {}
}
}

View File

@@ -1,5 +1,6 @@
use oauth2::{ApplicationSecret, ConsoleApplicationSecret, TokenStorage, Token};
use serde::json;
use serde::json::value::Value;
use mime::Mime;
use clap::{App, SubCommand};
use strsim;
@@ -17,11 +18,38 @@ use std::default::Default;
const FIELD_SEP: char = '.';
pub enum ComplexType {
Pod,
Vec,
Map,
}
// Null,
// Bool(bool),
// I64(i64),
// U64(u64),
// F64(f64),
// String(String),
pub enum JsonType {
Boolean,
Int,
Uint,
Float,
String,
}
pub struct JsonTypeInfo {
pub jtype: JsonType,
pub ctype: ComplexType,
}
// Based on @erickt user comment. Thanks for the idea !
// Remove all keys whose values are null from given value (changed in place)
pub fn remove_json_null_values(value: &mut json::value::Value) {
pub fn remove_json_null_values(value: &mut Value) {
match *value {
json::value::Value::Object(ref mut map) => {
Value::Object(ref mut map) => {
let mut for_removal = Vec::new();
for (key, mut value) in map.iter_mut() {
@@ -94,6 +122,14 @@ impl ToString for FieldCursor {
}
}
impl From<&'static str> for FieldCursor {
fn from(value: &'static str) -> FieldCursor {
let mut res = FieldCursor::default();
res.set(value).unwrap();
res
}
}
impl FieldCursor {
pub fn set(&mut self, value: &str) -> Result<(), CLIError> {
if value.len() == 0 {
@@ -199,6 +235,78 @@ impl FieldCursor {
}
}
pub fn set_json_value(&self, mut object: &mut Value,
value: &str, type_info: JsonTypeInfo,
err: &mut InvalidOptionsError,
orig_cursor: &FieldCursor) {
assert!(self.0.len() > 0);
for field in &self.0[..self.0.len()-1] {
let tmp = object;
object =
match *tmp {
Value::Object(ref mut mapping) => {
mapping.entry(field.to_owned()).or_insert(
Value::Object(Default::default())
)
},
_ => panic!("We don't expect non-object Values here ...")
};
}
match *object {
Value::Object(ref mut mapping) => {
let field = &self.0[self.0.len()-1];
let to_jval =
|value: &str, jtype: JsonType, err: &mut InvalidOptionsError|
-> Value {
match jtype {
JsonType::Boolean =>
Value::Bool(arg_from_str(value, err, &field, "boolean")),
JsonType::Int =>
Value::I64(arg_from_str(value, err, &field, "int")),
JsonType::Uint =>
Value::U64(arg_from_str(value, err, &field, "uint")),
JsonType::Float =>
Value::F64(arg_from_str(value, err, &field, "float")),
JsonType::String =>
Value::String(value.to_owned()),
}
};
match type_info.ctype {
ComplexType::Pod => {
if mapping.insert(field.to_owned(), to_jval(value, type_info.jtype, err)).is_some() {
err.issues.push(CLIError::Field(FieldError::Duplicate(orig_cursor.to_string())));
}
},
ComplexType::Vec => {
match *mapping.entry(field.to_owned())
.or_insert(Value::Array(Default::default())) {
Value::Array(ref mut values) => values.push(to_jval(value, type_info.jtype, err)),
_ => unreachable!()
}
},
ComplexType::Map => {
let (key, value) = parse_kv_arg(value, err, true);
let jval = to_jval(value.unwrap_or(""), type_info.jtype, err);
match *mapping.entry(field.to_owned())
.or_insert(Value::Object(Default::default())) {
Value::Object(ref mut value_map) => {
if value_map.insert(key.to_owned(), jval).is_some() {
err.issues.push(CLIError::Field(FieldError::Duplicate(orig_cursor.to_string())));
}
}
_ => unreachable!()
}
}
}
},
_ => unreachable!()
}
}
pub fn num_fields(&self) -> usize {
self.0.len()
}
@@ -266,15 +374,15 @@ pub fn writer_from_opts(arg: Option<&str>) -> Result<Box<Write>, io::Error> {
}
pub fn arg_from_str<T>(arg: &str, err: &mut InvalidOptionsError,
arg_name: &'static str,
arg_type: &'static str) -> T
pub fn arg_from_str<'a, T>(arg: &str, err: &mut InvalidOptionsError,
arg_name: &'a str,
arg_type: &'a str) -> T
where T: FromStr + Default,
<T as FromStr>::Err: fmt::Display {
match FromStr::from_str(arg) {
Err(perr) => {
err.issues.push(
CLIError::ParseError(arg_name, arg_type, arg.to_string(), format!("{}", perr))
CLIError::ParseError(arg_name.to_owned(), arg_type.to_owned(), arg.to_string(), format!("{}", perr))
);
Default::default()
},
@@ -409,6 +517,7 @@ pub enum FieldError {
PopOnEmpty(String),
TrailingFieldSep(String),
Unknown(String, Option<String>, Option<String>),
Duplicate(String),
Empty,
}
@@ -435,6 +544,8 @@ impl fmt::Display for FieldError {
};
writeln!(f, "Field '{}' does not exist.{}", field, suffix)
},
FieldError::Duplicate(ref cursor)
=> writeln!(f, "Value at '{}' was already set", cursor),
FieldError::Empty
=> writeln!(f, "Field names must not be empty."),
}
@@ -445,7 +556,7 @@ impl fmt::Display for FieldError {
#[derive(Debug)]
pub enum CLIError {
Configuration(ConfigurationError),
ParseError(&'static str, &'static str, String, String),
ParseError(String, String, String, String),
UnknownParameter(String, Vec<&'static str>),
InvalidUploadProtocol(String, Vec<String>),
InvalidKeyValueSyntax(String, bool),
@@ -463,7 +574,7 @@ impl fmt::Display for CLIError {
CLIError::Field(ref err) => write!(f, "Field -> {}", err),
CLIError::InvalidUploadProtocol(ref proto_name, ref valid_names)
=> writeln!(f, "'{}' is not a valid upload protocol. Choose from one of {}.", proto_name, valid_names.connect(", ")),
CLIError::ParseError(arg_name, type_name, ref value, ref err_desc)
CLIError::ParseError(ref arg_name, ref type_name, ref value, ref err_desc)
=> writeln!(f, "Failed to parse argument '{}' with value '{}' as {} with error: {}.",
arg_name, value, type_name, err_desc),
CLIError::UnknownParameter(ref param_name, ref possible_values) => {