Kinda sucks that we have to do this ourselves.
Maybe in the future, there will be a library provided which gives us interop between Rocket and other libraries.
use chrono::NaiveDate;
use chrono::NaiveTime;
use chrono::NaiveDateTime;
// https://stackoverflow.com/questions/25413201/how-do-i-implement-a-trait-i-dont-own-for-a-type-i-dont-own
// https://github.com/SergioBenitez/Rocket/issues/602#issuecomment-380497269
pub struct NaiveDateForm(NaiveDate);
pub struct NaiveTimeForm(NaiveTime);
pub struct NaiveDateTimeForm(NaiveDateTime);
impl<'v> FromFormValue<'v> for NaiveDateForm {
type Error = &'v RawStr;
fn from_form_value(form_value: &'v RawStr) -> Result<NaiveDateForm, &'v RawStr> {
let decoded = form_value.url_decode().map_err(|_| form_value)?;
if let Ok(date) = NaiveDate::parse_from_str(&decoded, "%Y-%m-%d") {
return Ok(NaiveDateForm(date));
}
Err(form_value)
}
}
impl<'v> FromFormValue<'v> for NaiveTimeForm {
type Error = &'v RawStr;
fn from_form_value(form_value: &'v RawStr) -> Result<Self, Self::Error> {
let decoded = form_value.url_decode().map_err(|_| form_value)?;
if let Ok(time) = NaiveTime::parse_from_str(&decoded, "%H:%M:%S%.3f") {
// if time.nanosecond() >= 1_000_000_000 {
// return Err(form_value);
// }
return Ok(NaiveTimeForm(time));
}
if let Ok(time) = NaiveTime::parse_from_str(&decoded, "%H:%M") {
return Ok(NaiveTimeForm(time));
}
Err(form_value)
}
}
impl<'v> FromFormValue<'v> for NaiveDateTimeForm {
type Error = &'v RawStr;
fn from_form_value(form_value: &'v RawStr) -> Result<NaiveDateTimeForm, &'v RawStr> {
let decoded = form_value.url_decode().map_err(|_| form_value)?;
if decoded.len() < "0000-00-00T00:00".len() {
return Err(form_value)
}
let date = NaiveDateForm::from_form_value(RawStr::from_str(&decoded[.."0000-00-00".len()]))
.map_err(|_| form_value)?;
let time = NaiveTimeForm::from_form_value(RawStr::from_str(&decoded["0000-00-00T".len()..]))
.map_err(|_| form_value)?;
Ok(NaiveDateTimeForm(NaiveDateTime::new(*date, *time)))
}
}
impl Deref for NaiveDateForm {
type Target = NaiveDate;
fn deref(&self) -> &NaiveDate {
&self.0
}
}
impl Deref for NaiveTimeForm {
type Target = NaiveTime;
fn deref(&self) -> &NaiveTime {
&self.0
}
}
impl Deref for NaiveDateTimeForm {
type Target = NaiveDateTime;
fn deref(&self) -> &NaiveDateTime {
&self.0
}
}
You should then be able to do:
#[get("/hello/<name>/<age>")]
fn hello(name: String, age: NaiveDateTimeForm) -> String {
// Deref back to chrono::NaiveDatetime
let date_time = *age;
// write some code to figure out their age
}
My dependencies:
chrono = { version = "0.4.19", features = ["serde"] }
rocket = "0.4.2"
This implementation is mostly stolen from https://github.com/chronotope/chrono/pull/362/files where someone made a PR to try to get this stuff already into Chrono.
Probably rather than age
, you should have birthday
so you can calculate their age.