mirror of
https://github.com/OMGeeky/google-apis-rs.git
synced 2026-01-02 17:42:16 +01:00
Merge branch 'serde'
This commit is contained in:
@@ -18,8 +18,9 @@ path = "src/rust/lib.rs"
|
||||
[dependencies]
|
||||
hyper = "*"
|
||||
mime = "*"
|
||||
rustc-serialize = "*"
|
||||
yup-oauth2 = "*"
|
||||
serde = "*"
|
||||
serde_macros = "*"
|
||||
|
||||
[dev-dependencies]
|
||||
yup-hyper-mock = "*"
|
||||
|
||||
@@ -97,7 +97,7 @@ google-youtube3 = "0.0.1"
|
||||
```Rust
|
||||
extern crate hyper;
|
||||
extern crate "yup-oauth2" as oauth2;
|
||||
extern crate "rustc-serialize" as rustc_serialize;
|
||||
extern crate serde;
|
||||
extern crate "google-youtube3" as youtube3;
|
||||
use youtube3::Result;
|
||||
use std::default::Default;
|
||||
|
||||
@@ -17,5 +17,6 @@ keywords = ["youtube", "google", "protocol", "web", "api"]
|
||||
hyper = "*"
|
||||
mime = "*"
|
||||
url = "*"
|
||||
rustc-serialize = "*"
|
||||
serde = "*"
|
||||
serde_macros = "*"
|
||||
yup-oauth2 = "*"
|
||||
|
||||
@@ -45,10 +45,10 @@ impl<T: Seek + Read> ReadSeek for T {}
|
||||
|
||||
|
||||
/// A utility type which can decode a server response that indicates error
|
||||
#[derive(RustcDecodable)]
|
||||
#[derive(Deserialize)]
|
||||
pub struct JsonServerError {
|
||||
error: String,
|
||||
error_description: Option<String>
|
||||
pub error: String,
|
||||
pub error_description: Option<String>
|
||||
}
|
||||
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -19,5 +19,6 @@ keywords = ["${name}", ${", ".join(estr(cargo.keywords))}]
|
||||
hyper = "*"
|
||||
mime = "*"
|
||||
url = "*"
|
||||
rustc-serialize = "*"
|
||||
serde = "*"
|
||||
serde_macros = "*"
|
||||
yup-oauth2 = "*"
|
||||
|
||||
@@ -28,10 +28,12 @@ ${lib.docs(c)}
|
||||
// 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)]
|
||||
|
||||
// Required for serde annotations
|
||||
#![feature(custom_derive, custom_attribute, plugin)]
|
||||
#![plugin(serde_macros)]
|
||||
|
||||
extern crate hyper;
|
||||
extern crate "rustc-serialize" as rustc_serialize;
|
||||
extern crate serde;
|
||||
extern crate "yup-oauth2" as oauth2;
|
||||
extern crate mime;
|
||||
extern crate url;
|
||||
@@ -44,7 +46,7 @@ use std::borrow::BorrowMut;
|
||||
use std::default::Default;
|
||||
use std::collections::BTreeMap;
|
||||
use std::marker::PhantomData;
|
||||
use rustc_serialize::json;
|
||||
use serde::json;
|
||||
use std::io;
|
||||
use std::fs;
|
||||
use std::old_io::timer::sleep;
|
||||
|
||||
@@ -649,7 +649,7 @@ else {
|
||||
|
||||
% if request_value:
|
||||
let mut json_mime_type = mime::Mime(mime::TopLevel::Application, mime::SubLevel::Json, Default::default());
|
||||
let mut request_value_reader = io::Cursor::new(json::encode(&self.${property(REQUEST_VALUE_PROPERTY_NAME)}).unwrap().into_bytes());
|
||||
let mut request_value_reader = io::Cursor::new(json::to_vec(&self.${property(REQUEST_VALUE_PROPERTY_NAME)}));
|
||||
let request_size = request_value_reader.seek(io::SeekFrom::End(0)).unwrap();
|
||||
request_value_reader.seek(io::SeekFrom::Start(0)).unwrap();
|
||||
% endif
|
||||
@@ -727,7 +727,7 @@ else {
|
||||
if !res.status.is_success() {
|
||||
let mut json_err = String::new();
|
||||
res.read_to_string(&mut json_err).unwrap();
|
||||
let error_info: cmn::JsonServerError = json::decode(&json_err).unwrap();
|
||||
let error_info: cmn::JsonServerError = json::from_str(&json_err).unwrap();
|
||||
if let oauth2::Retry::After(d) = dlg.http_failure(&res, error_info) {
|
||||
sleep(d);
|
||||
continue;
|
||||
@@ -744,7 +744,7 @@ if enable_resource_parsing \
|
||||
{
|
||||
let mut json_response = String::new();
|
||||
res.read_to_string(&mut json_response).unwrap();
|
||||
(res, json::decode(&json_response).unwrap())
|
||||
(res, json::from_str(&json_response).unwrap())
|
||||
}\
|
||||
% if supports_download:
|
||||
else { (res, Default::default()) }\
|
||||
|
||||
@@ -14,6 +14,9 @@
|
||||
${struct} {
|
||||
% for pn, p in properties.iteritems():
|
||||
${p.get('description', 'no description provided') | rust_doc_comment, indent_all_but_first_by(1)}
|
||||
% if pn != mangle_ident(pn):
|
||||
#[serde(alias="${pn}")]
|
||||
% endif
|
||||
pub ${mangle_ident(pn)}: ${to_rust_type(schemas, s.id, pn, p)},
|
||||
% endfor
|
||||
}
|
||||
@@ -34,9 +37,9 @@ ${struct};
|
||||
traits = ['Default', 'Clone', 'Debug']
|
||||
|
||||
if REQUEST_MARKER_TRAIT in markers:
|
||||
traits.append('RustcEncodable')
|
||||
traits.append('Serialize')
|
||||
if RESPONSE_MARKER_TRAIT in markers:
|
||||
traits.append('RustcDecodable')
|
||||
traits.append('Deserialize')
|
||||
|
||||
## waiting for Default: https://github.com/rust-lang/rustc-serialize/issues/71
|
||||
if s.type == 'any':
|
||||
@@ -58,11 +61,11 @@ ${_new_object(s, s.items.get('properties'), c)}\
|
||||
% endif ## array item != 'object'
|
||||
% elif s.type == 'any':
|
||||
## waiting for Default: https://github.com/rust-lang/rustc-serialize/issues/71
|
||||
pub struct ${s_type}(rustc_serialize::json::Json);
|
||||
pub struct ${s_type}(json::Value);
|
||||
|
||||
impl Default for ${s_type} {
|
||||
fn default() -> ${s_type} {
|
||||
${s_type}(rustc_serialize::json::Json::Null)
|
||||
${s_type}(json::Value::Null)
|
||||
}
|
||||
}
|
||||
% else:
|
||||
|
||||
@@ -32,7 +32,7 @@ ${util.library_to_crate_name(util.library_name(name, version))}\
|
||||
<%def name="test_prelude()">\
|
||||
extern crate hyper;
|
||||
extern crate "yup-oauth2" as oauth2;
|
||||
extern crate "rustc-serialize" as rustc_serialize;
|
||||
extern crate serde;
|
||||
extern crate "${self.crate_name()}" as ${self.library_name()};
|
||||
</%def>
|
||||
|
||||
|
||||
@@ -320,10 +320,10 @@ def to_rust_type(schemas, sn, pn, t, allow_optionals=True):
|
||||
try:
|
||||
rust_type = TYPE_MAP[t.type]
|
||||
if t.type == 'array':
|
||||
return "%s<%s>" % (rust_type, unique_type_name((nested_type(t))))
|
||||
return wrap_type("%s<%s>" % (rust_type, unique_type_name((nested_type(t)))))
|
||||
elif t.type == 'object':
|
||||
if _is_map_prop(t):
|
||||
return "%s<String, %s>" % (rust_type, nested_type(t))
|
||||
return wrap_type("%s<String, %s>" % (rust_type, nested_type(t)))
|
||||
else:
|
||||
return wrap_type(nested_type(t))
|
||||
elif t.type == 'string' and 'Count' in pn:
|
||||
|
||||
@@ -43,10 +43,10 @@ impl<T: Seek + Read> ReadSeek for T {}
|
||||
|
||||
|
||||
/// A utility type which can decode a server response that indicates error
|
||||
#[derive(RustcDecodable)]
|
||||
#[derive(Deserialize)]
|
||||
pub struct JsonServerError {
|
||||
error: String,
|
||||
error_description: Option<String>
|
||||
pub error: String,
|
||||
pub error_description: Option<String>
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
#![feature(core,io,old_path)]
|
||||
#![allow(dead_code, deprecated, unused_features)]
|
||||
#![feature(core,io,old_path, custom_derive, custom_attribute, plugin)]
|
||||
#![allow(dead_code, deprecated, unused_features, unused_variables, unused_imports)]
|
||||
//! library with code shared by all generated implementations
|
||||
#![plugin(serde_macros)]
|
||||
extern crate hyper;
|
||||
extern crate mime;
|
||||
extern crate "rustc-serialize" as rustc_serialize;
|
||||
extern crate "yup-oauth2" as oauth2;
|
||||
extern crate serde;
|
||||
|
||||
// just pull it in the check if it compiles
|
||||
mod cmn;
|
||||
@@ -19,6 +20,8 @@ mod tests {
|
||||
use std::default::Default;
|
||||
use std::old_path::BytesContainer;
|
||||
|
||||
use serde::json;
|
||||
|
||||
const EXPECTED: &'static str =
|
||||
"\r\n--MDuXWGyeE33QFXGchb2VFWc4Z7945d\r\n\
|
||||
Content-Length: 50\r\n\
|
||||
@@ -76,4 +79,37 @@ bar\r\n\
|
||||
// See above: headers are unordered
|
||||
// assert_eq!(v.container_as_str().unwrap(), EXPECTED);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn serde() {
|
||||
#[derive(Default, Serialize, Deserialize)]
|
||||
struct Foo {
|
||||
opt: Option<String>,
|
||||
req: u32,
|
||||
opt_vec: Option<Vec<String>>,
|
||||
vec: Vec<String>,
|
||||
}
|
||||
|
||||
let f: Foo = Default::default();
|
||||
json::to_string(&f).unwrap(); // should work
|
||||
|
||||
let j = "{\"opt\":null,\"req\":0,\"vec\":[]}";
|
||||
let f: Foo = json::from_str(j).unwrap();
|
||||
|
||||
// This fails, unless 'vec' is optional
|
||||
// let j = "{\"opt\":null,\"req\":0}";
|
||||
// let f: Foo = json::from_str(j).unwrap();
|
||||
|
||||
#[derive(Default, Serialize, Deserialize)]
|
||||
struct Bar {
|
||||
#[serde(alias="snooSnoo")]
|
||||
snoo_snoo: String
|
||||
}
|
||||
json::to_string(&<Bar as Default>::default()).unwrap();
|
||||
|
||||
|
||||
let j = "{\"snooSnoo\":\"foo\"}";
|
||||
let b: Bar = json::from_str(&j).unwrap();
|
||||
assert_eq!(b.snoo_snoo, "foo");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user