-
Hello, first of all impressive work! I have seen the I have a struct Task, that has certain fields for the form. One field should be a NaiveDateTimeForm (idk if that should be a field instead). That whole task comes from the user input as post form. I just want to parse the string to NaiveDateTime using the chrono lib, but for that I need to parse it somehow. I think my issue and use-case is fairly standard that a lot of people (will) encounter. My following approach is obviously not working due to some lifetime issues (idk if that works at all) - and throws a loooot of errors. #[derive(FromForm)]
struct Task<'r> {
#[field(validate = len(1..))]
description: &'r str,
datetime: NaiveDateTimeForm,
completed: bool
}
pub struct NaiveDateTimeForm(NaiveDateTime);
#[rocket::async_trait]
impl<'r> FromFormField<'r> for NaiveDateTimeForm<'r> {
fn from_value(field: ValueField<'r>) -> form::Result<'r, Self> {
match NaiveDateTime::parse_from_str(&field, "%+") {
Ok(date)=> Ok(NaiveDateForm(date)),
Err(e) =>Err(e),
}
}
async fn from_data(field: DataField<'r, '_>) -> form::Result<'r, Self> {
match NaiveDateTime::parse_from_str(&field, "%+") {
Ok(date)=> Ok(NaiveDateForm(date)),
Err(e) =>Err(e),
}
}
}
// I took that part from Stackoverflow (idk if its needed in this case)
impl Deref for NaiveDateTimeForm {
type Target = NaiveDateTime;
fn deref(&self) -> &NaiveDateTime {
&self.0
}
}
As far as I've read on StackOverflow there was a breaking change in v0.5 and nothing works since then anymore. Someone tried to use this: use chrono::NaiveDate;
use chrono::ParseError;
use rocket::request::FromParam;
// 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(pub NaiveDate);
impl<'a> FromParam<'a> for NaiveDateForm {
type Error = ParseError;
fn from_param(param: &'a str) -> Result<Self, Self::Error>{
match NaiveDate::parse_from_str(¶m, "%Y-%m-%d") {
Ok(date)=> Ok(NaiveDateForm(date)),
Err(e) =>Err(e),
}
}
} Sadly, that does not work in my case, and throws errors for DateTime. So I tried this #[derive(FromForm)]
struct Task<'r> {
#[field(validate = len(1..))]
description: &'r str,
datetime: NaiveDateTimeForm,
completed: bool
}
pub struct NaiveDateTimeForm(pub NaiveDateTime);
impl<'a> FromParam<'a> for NaiveDateTimeForm {
type Error = ParseError;
fn from_param(param: &'a str) -> Result<Self, Self::Error>{
match NaiveDateTime::parse_from_str(¶m, "%+") {
Ok(dt)=> Ok(NaiveDateTimeForm(dt)),
Err(e) =>Err(e),
}
}
} And this throws: error[E0277]: the trait bound `NaiveDateTimeForm: FromFormField<'_>` is not satisfied
--> src/bin/webserver.rs:80:14
|
80 | deadline: NaiveDateTimeForm,
| ^^^^^^^^^^^^^^^^^ the trait `FromFormField<'_>` is not implemented for `NaiveDateTimeForm`
|
= help: the following other types implement trait `FromFormField<'v>`:
&'v [u8]
&'v str
Capped<&'v [u8]>
Capped<&'v str>
Capped<Cow<'v, str>>
Capped<TempFile<'v>>
Capped<std::string::String>
Cow<'v, str>
and 39 others
= note: required for `NaiveDateTimeForm` to implement `rocket::form::FromForm<'r>` Finally I tried this #[rocket::async_trait]
impl<'r> FromFormField<'r> for NaiveDateTimeForm {
fn from_value(field: ValueField<'r>) -> form::Result<'r, Self> {
match NaiveDateTime::parse_from_str(<&str>::from_value(field)?.parse()?, "%+") {
Ok(date)=> Ok(NaiveDateTimeForm(date)),
// Err(e) =>Err(e),
}
}
async fn from_data(field: DataField<'r, '_>) -> form::Result<'r, Self> {
match NaiveDateTime::parse_from_str(<&str>::from_data(field).await?.parse()?, "%+") {
Ok(date)=> Ok(NaiveDateTimeForm(date))
}
}
} Which doesnt work either because error[E0277]: the trait bound `&str: FromStr` is not satisfied
--> src/bin/webserver.rs:63:72
|
63 | match NaiveDateTime::parse_from_str(<&str>::from_value(field)?.parse()?, "%+") {
| ^^^^^ the trait `FromStr` is not implemented for `&str`
|
= help: the trait `FromStr` is implemented for `std::string::String`
note: required by a bound in `core::str::<impl str>::parse`
--> /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/core/src/str/mod.rs:2345:5
error[E0277]: the trait bound `&str: FromStr` is not satisfied
--> src/bin/webserver.rs:70:77
|
70 | match NaiveDateTime::parse_from_str(<&str>::from_data(field).await?.parse()?, "%+") {
| ^^^^^ the trait `FromStr` is not implemented for `&str`
|
= help: the trait `FromStr` is implemented for `std::string::String`
note: required by a bound in `core::str::<impl str>::parse`
--> /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/core/src/str/mod.rs:2345:5
For more information about this error, try `rustc --explain E0277`. My questions:
Sources:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Little update, partially working code: // https://github.com/SergioBenitez/Rocket/issues/2302
#[rocket::async_trait]
impl<'r> FromFormField<'r> for NaiveDateTimeForm {
fn from_value(field: ValueField<'r>) -> form::Result<'r, Self> {
println!("{}", field.value); // = 2023-08-10T23:02
match NaiveDateTime::parse_from_str(field.value, "%Y-%m-%d %H:%M") {
Ok(date)=> Ok(NaiveDateTimeForm(date)),
Err(e) => panic!("{}", e),
// Err(e) =>Err(e),
}
}
async fn from_data(field: DataField<'r, '_>) -> form::Result<'r, Self> {
// match NaiveDateTime::parse_from_str(field.value, "%+") {
// Ok(date)=> Ok(NaiveDateTimeForm(date))
// }
unimplemented!()
}
} Now Chrono seems to be the problem. |
Beta Was this translation helpful? Give feedback.
-
I solved my question three days ago. I just forgot to post it. The code above is basically the solution #[rocket::async_trait]
impl<'r> FromFormField<'r> for NaiveDateTimeForm {
fn from_value(field: ValueField<'r>) -> form::Result<'r, Self> {
println!("{}", field.value);
match NaiveDateTime::parse_from_str(field.value, "%Y-%m-%dT%H:%M") {
Ok(date)=> Ok(NaiveDateTimeForm(date)),
Err(e) => panic!("{}", e),
// Err(e) =>Err(e),
}
}
async fn from_data(_field: DataField<'r, '_>) -> form::Result<'r, Self> {
unimplemented!()
}
} However, what I did not try yet is loading data from the DB into the Form. I only used this for a Form that creates an DB "Entity". A problem here is also the TZ, as NaiveDateTime and the HTML datetime-local picker not adding a ZULU value. |
Beta Was this translation helpful? Give feedback.
I solved my question three days ago. I just forgot to post it. The code above is basically the solution
However, what I did not try yet is loading data from the DB into the Form. I only use…