Skip to content

Instantly share code, notes, and snippets.

@pzol
Created August 11, 2014 09:46
Show Gist options
  • Save pzol/3691b181656a77181de3 to your computer and use it in GitHub Desktop.
Save pzol/3691b181656a77181de3 to your computer and use it in GitHub Desktop.
#![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)});
}
@pzol
Copy link
Author

pzol commented Aug 11, 2014

task 'test_category_decoder' failed at 'called Option::unwrap() on a None value', /Users/rustbuild/src/rust-buildbot/slave/nightly-mac/build/src/libcore/option.rs:262

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment