Created
August 11, 2014 09:46
-
-
Save pzol/3691b181656a77181de3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(phase, macro_rules)] | |
extern crate debug; | |
#[phase(plugin, link)] extern crate log; | |
extern crate serialize; | |
pub use serialize::{ Decodable, Decoder, Encoder, Encodable,}; | |
#[path="../src/json.rs"] | |
mod json; | |
#[deriving(Show, PartialEq)] | |
struct Category { | |
min: Option<uint>, | |
max: Option<uint> | |
} | |
impl <D: Decoder<E>, E> Decodable<D, E> for Category { | |
fn decode(d: &mut D) -> Result<Category, E> { | |
debug!("d {:?}", d); | |
d.read_struct("Category", 2u, |d| | |
Ok(Category{ | |
min: Some(match d.read_struct_field("min", 0u, |d| Decodable::decode(d)) | |
{ | |
Ok(min) => min, | |
Err(_) => 0u | |
}), | |
max: Some(match d.read_struct_field("max", 1u, |d| { let res = Decodable::decode(d); debug!("max {:?}", res); res }) | |
{ | |
Ok(max) => max, | |
Err(_) => 5u | |
}) | |
} | |
) | |
) | |
} | |
} | |
#[test] | |
fn test_category_decoder() { | |
let jstr = r##"{ "min":0 }"##; | |
let jso = json::from_str(jstr); | |
let mut decoder = json::Decoder::new(jso.unwrap()); | |
let res : Category = Decodable::decode(&mut decoder).unwrap(); | |
assert_eq!(res, Category { min: Some(0), max: Some(5)}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
task 'test_category_decoder' failed at 'called
Option::unwrap()
on aNone
value', /Users/rustbuild/src/rust-buildbot/slave/nightly-mac/build/src/libcore/option.rs:262