Rocket/examples/query_params/src/main.rs

42 lines
992 B
Rust
Raw Normal View History

#![feature(proc_macro_hygiene)]
#[macro_use] extern crate rocket;
2016-12-29 03:29:12 +00:00
#[cfg(test)] mod tests;
use rocket::request::{Form, LenientForm};
#[derive(FromForm)]
struct Person {
/// Use the `form` attribute to expect an invalid Rust identifier in the HTTP form.
#[form(field = "first-name")]
name: String,
age: Option<u8>
}
#[get("/hello?<person..>")]
fn hello(person: Option<Form<Person>>) -> String {
if let Some(person) = person {
if let Some(age) = person.age {
format!("Hello, {} year old named {}!", age, person.name)
} else {
format!("Hello {}!", person.name)
}
} else {
"We're gonna need a name, and only a name.".into()
}
}
#[get("/hello?age=20&<person..>")]
fn hello_20(person: LenientForm<Person>) -> String {
format!("20 years old? Hi, {}!", person.name)
}
fn rocket() -> rocket::Rocket {
rocket::ignite().mount("/", routes![hello, hello_20])
}
fn main() {
let _ = rocket().launch();
}