有点糟糕,我们必须自己做这件事。
也许将来会提供一个库,让我们能够在 Rocket 和其他库之间进行互操作。
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
}
}
然后,您应该能够:
#[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
}
我的依赖:
chrono = { version = "0.4.19", features = ["serde"] }
rocket = "0.4.2"
这个实现主要是从https://github.com/chronotope/chrono/pull/362/files偷来的,有人做了 PR 试图把这些东西放到 Chrono 中。
可能而不是age
,你应该有birthday
,所以你可以计算他们的年龄。