pub trait FromField: Sized {
type Slot: Default;
// Required method
fn finish(slot: Self::Slot) -> Result<Self, ValueError>;
// Provided methods
fn accept_text(slot: &mut Self::Slot, value: &str) -> Result<(), ValueError> { ... }
fn accept_file(
slot: &mut Self::Slot,
file: FileValue<'_>,
) -> Result<(), ValueError> { ... }
}Available on crate feature
alloc only.Expand description
How one field type takes its values.
The slot holds what has arrived so far, so every arity rule lives in one
place: a scalar’s slot is Option<T> (a second value is Duplicate,
none is Missing), Vec<T> pushes, bool finishes a missing value as
false. Implement it for an application type to validate while
extracting.
§Examples
use form_net::{FromField, ValueError};
pub struct Slug(String);
impl FromField for Slug {
type Slot = Option<Slug>;
fn accept_text(slot: &mut Option<Slug>, value: &str) -> Result<(), ValueError> {
if slot.is_some() { return Err(ValueError::Duplicate); }
if value.is_empty() || value.len() > 64 || !value.bytes().all(|b| b.is_ascii_lowercase() || b == b'-') {
return Err(ValueError::Invalid("lowercase letters and hyphens, 1 to 64"));
}
*slot = Some(Slug(value.to_owned()));
Ok(())
}
fn finish(slot: Option<Slug>) -> Result<Slug, ValueError> {
slot.ok_or(ValueError::Missing)
}
}Required Associated Types§
Required Methods§
Provided Methods§
Sourcefn accept_text(slot: &mut Self::Slot, value: &str) -> Result<(), ValueError>
fn accept_text(slot: &mut Self::Slot, value: &str) -> Result<(), ValueError>
Sourcefn accept_file(
slot: &mut Self::Slot,
file: FileValue<'_>,
) -> Result<(), ValueError>
fn accept_file( slot: &mut Self::Slot, file: FileValue<'_>, ) -> Result<(), ValueError>
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".