-
-
Save whodidthis/744560ae4c6eb4540973 to your computer and use it in GitHub Desktop.
struct parser
This file contains 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(default_type_params)] | |
extern crate iron; | |
extern crate serialize; | |
extern crate plugin; | |
use iron::Request; | |
use iron::typemap::Assoc; | |
use plugin::{PluginFor, Phantom}; | |
use serialize::{json, Decodable}; | |
#[deriving(Clone)] | |
pub struct StructParser<T>; | |
impl<T: 'static> Assoc<T> for StructParser<T> {} | |
impl<T> PluginFor<Request, T> for StructParser<T> { | |
fn eval(req: &Request, _: Phantom<StructParser<T>>) -> Option<T> { | |
if !req.body.is_empty() { | |
let json_object = match json::from_str(req.body.as_slice()).ok() { | |
Some(json_object) => json_object, | |
None => {return None;}, | |
}; | |
let mut decoder = json::Decoder::new(json_object); | |
match Decodable::decode(&mut decoder) { | |
Ok(t) => Some(t), | |
Err(err) => None, | |
} | |
} else { | |
None | |
} | |
} | |
} | |
/* | |
lib.rs:27:19: 27:36 error: the trait `serialize::serialize::Decodable<serialize::json::Decoder,serialize::json::DecoderError>` is not implemented for the type `T` | |
lib.rs:27 match Decodable::decode(&mut decoder) { | |
^~~~~~~~~~~~~~~~~ | |
lib.rs:27:19: 27:36 note: the trait `serialize::serialize::Decodable` must be implemented because it is required by `serialize::serialize::Decodable::decode` | |
lib.rs:27 match Decodable::decode(&mut decoder) { | |
^~~~~~~~~~~~~~~~~ | |
error: aborting due to previous error | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment