Skip to content

Instantly share code, notes, and snippets.

Created January 31, 2017 07:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/457ef1b886bfd2bb35219a7b64525d3c to your computer and use it in GitHub Desktop.
Save anonymous/457ef1b886bfd2bb35219a7b64525d3c to your computer and use it in GitHub Desktop.
Shared via Rust Playground
#![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