You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Create a form and derive FromForm with a field Option<Result<T>> where T is a custom type. Submit the form with nothing for that field, and the field is parsed as Some(Missing error) instead of None.
Test Case
#[macro_use]externcrate rocket;use rocket::form::FromFormField;#[derive(Debug)]pubstructCsl<T: std::fmt::Display>(pubVec<T>);#[rocket::async_trait]impl<'r,T>FromFormField<'r>forCsl<T>whereT: std::str::FromStr,T: std::fmt::Display,T: std::marker::Send,{fnfrom_value(field: rocket::form::ValueField<'r>) -> rocket::form::Result<'r,Self>{let value = field.value;letmut vec:Vec<T> = vec![];if value.is_empty(){returnOk(Csl(vec));}for i in value.split(','){let parsed = i.parse::<T>().map_err(|_| {
rocket::form::Errors::from(rocket::form::error::ErrorKind::Validation(format!("{} '{}' is incorrect type", field.name, i).into(),))})?;
vec.push(parsed);}Ok(Csl(vec))}asyncfnfrom_data(_: rocket::form::DataField<'r,'_>) -> rocket::form::Result<'r,Self>{todo!("This isn't implemented")}}#[cfg(test)]mod tests {use rocket::form::Form;usesuper::Csl;#[derive(Debug, rocket::FromForm)]pubstructTest2<'a>{publist:Option<rocket::form::Result<'a,Csl<i32>>>,}#[rocket::post("/test2", data = "<data>")]pubfntest2(data:Form<Test2>) -> String{dbg!(&data);match&data.list{None => "List is empty".to_string(),Some(x) => match x {Ok(y) => format!("List is: {}", y),Err(e) => format!("List failed: {}", e[0]),},}}fnclient() -> rocket::local::blocking::Client{
rocket::local::blocking::Client::untracked(
rocket::build().mount("/", rocket::routes![test2]),).unwrap()}#[test]fnit_should_handle_optional_values(){let response = client().post("/test2").header(rocket::http::ContentType::Form).dispatch().into_string().unwrap();assert!(response, "List is empty");}}
Rocket Version
0.5.1
Operating System
Linux
Rust Toolchain Version
rustc 1.79.0-nightly (0d8b3346a 2024-04-14)
What happened?
Create a form and derive FromForm with a field
Option<Result<T>>
whereT
is a custom type. Submit the form with nothing for that field, and the field is parsed as Some(Missing
error) instead ofNone
.Test Case
Log Output
Additional Context
I don't think this is a runtime bug so haven't provided complete logs... it seems like it's a failure in parsing that no debug logs could help with.
System Checks
rustc
toolchain.The text was updated successfully, but these errors were encountered: