rocket::form::validate

Function try_with

Source
pub fn try_with<'v, V, F, T, E>(value: V, f: F) -> Result<'v, ()>
where F: FnOnce(V) -> Result<T, E>, E: Display,
Expand description

Try With validator: succeeds when an arbitrary function or closure does.

Along with with, this is the most generic validator. It succeeds exactly when f returns Ok and fails otherwise.

On error, returns a validation error with the message in the Err variant converted into a string.

ยงExample

Assuming Token has a from_str method:

use std::str::FromStr;

#[derive(FromForm)]
#[field(validate = try_with(|s| Token::from_str(s)))]
struct Token<'r>(&'r str);

#[derive(FromForm)]
#[field(validate = try_with(|s| s.parse::<Token>()))]
struct Token2<'r>(&'r str);