Created
January 31, 2017 07:55
-
-
Save anonymous/457ef1b886bfd2bb35219a7b64525d3c to your computer and use it in GitHub Desktop.
Shared via Rust Playground
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(plugin, custom_derive)] | |
#![plugin(rocket_codegen)] | |
extern crate rocket; | |
extern crate serde; | |
extern crate serde_json; | |
#[macro_use] | |
extern crate rocket_contrib; | |
#[macro_use] | |
extern crate serde_derive; | |
use serde_json::Value; | |
use rocket::request::Form; | |
use rocket_contrib::JSON; | |
#[derive(FromForm)] | |
struct Message { | |
foo: String, | |
bar: String, | |
baz: Option<String>, | |
} | |
#[derive(Serialize)] | |
struct Foo { | |
bar: String, | |
baz: String, | |
val: Value, | |
} | |
#[post("/foo", data = "<msg>")] | |
fn foo(msg: Form<Message>) -> JSON<Foo> { | |
let form = msg.into_inner(); | |
let res = Foo { | |
bar: form.bar.to_owned(), | |
baz: match form.baz { | |
Some(x) => x, | |
None => "boo".to_string(), | |
}, | |
val: Value::Null, | |
}; | |
JSON(res) | |
} | |
fn main() { | |
println!("Hello, world!"); | |
} #![feature(plugin, custom_derive)] | |
#![plugin(rocket_codegen)] | |
extern crate rocket; | |
extern crate serde; | |
extern crate serde_json; | |
#[macro_use] | |
extern crate rocket_contrib; | |
#[macro_use] | |
extern crate serde_derive; | |
use serde_json::Value; | |
use rocket::request::Form; | |
use rocket_contrib::JSON; | |
#[derive(FromForm)] | |
struct Message { | |
foo: String, | |
bar: String, | |
baz: Option<String>, | |
} | |
#[derive(Serialize)] | |
struct Foo { | |
bar: String, | |
baz: String, | |
val: Value, | |
} | |
#[post("/foo", data = "<msg>")] | |
fn foo(msg: Form<Message>) -> JSON<Foo> { | |
let form = msg.into_inner(); | |
let res = Foo { | |
bar: form.bar.to_owned(), | |
baz: match form.baz { | |
Some(x) => x, | |
None => "boo".to_string(), | |
}, | |
val: Value::Null, | |
}; | |
JSON(res) | |
} | |
fn main() { | |
println!("Hello, world!"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment