Skip to main content

FromField

Trait FromField 

Source
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§

Source

type Slot: Default

What has arrived so far.

Required Methods§

Source

fn finish(slot: Self::Slot) -> Result<Self, ValueError>

The value, once every field has arrived.

§Errors

Missing, or the type’s own errors.

Provided Methods§

Source

fn accept_text(slot: &mut Self::Slot, value: &str) -> Result<(), ValueError>

Takes one text value.

§Errors

ExpectedFile by default; the type’s own conversion errors.

Source

fn accept_file( slot: &mut Self::Slot, file: FileValue<'_>, ) -> Result<(), ValueError>

Takes one file.

§Errors

ExpectedText by default; the type’s own errors.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl FromField for String

Source§

type Slot = Option<String>

Source§

fn accept_text(slot: &mut Self::Slot, value: &str) -> Result<(), ValueError>

Source§

fn finish(slot: Self::Slot) -> Result<Self, ValueError>

Source§

impl FromField for bool

Source§

fn finish(slot: Self::Slot) -> Result<Self, ValueError>

An unchecked checkbox sends nothing: missing is false.

Source§

type Slot = Option<bool>

Source§

fn accept_text(slot: &mut Self::Slot, value: &str) -> Result<(), ValueError>

Source§

impl FromField for char

Source§

type Slot = Option<char>

Source§

fn accept_text(slot: &mut Self::Slot, value: &str) -> Result<(), ValueError>

Source§

fn finish(slot: Self::Slot) -> Result<Self, ValueError>

Source§

impl FromField for f32

Source§

type Slot = Option<f32>

Source§

fn accept_text(slot: &mut Self::Slot, value: &str) -> Result<(), ValueError>

Source§

fn finish(slot: Self::Slot) -> Result<Self, ValueError>

Source§

impl FromField for f64

Source§

type Slot = Option<f64>

Source§

fn accept_text(slot: &mut Self::Slot, value: &str) -> Result<(), ValueError>

Source§

fn finish(slot: Self::Slot) -> Result<Self, ValueError>

Source§

impl FromField for i8

Source§

type Slot = Option<i8>

Source§

fn accept_text(slot: &mut Self::Slot, value: &str) -> Result<(), ValueError>

Source§

fn finish(slot: Self::Slot) -> Result<Self, ValueError>

Source§

impl FromField for i16

Source§

type Slot = Option<i16>

Source§

fn accept_text(slot: &mut Self::Slot, value: &str) -> Result<(), ValueError>

Source§

fn finish(slot: Self::Slot) -> Result<Self, ValueError>

Source§

impl FromField for i32

Source§

type Slot = Option<i32>

Source§

fn accept_text(slot: &mut Self::Slot, value: &str) -> Result<(), ValueError>

Source§

fn finish(slot: Self::Slot) -> Result<Self, ValueError>

Source§

impl FromField for i64

Source§

type Slot = Option<i64>

Source§

fn accept_text(slot: &mut Self::Slot, value: &str) -> Result<(), ValueError>

Source§

fn finish(slot: Self::Slot) -> Result<Self, ValueError>

Source§

impl FromField for i128

Source§

type Slot = Option<i128>

Source§

fn accept_text(slot: &mut Self::Slot, value: &str) -> Result<(), ValueError>

Source§

fn finish(slot: Self::Slot) -> Result<Self, ValueError>

Source§

impl FromField for isize

Source§

type Slot = Option<isize>

Source§

fn accept_text(slot: &mut Self::Slot, value: &str) -> Result<(), ValueError>

Source§

fn finish(slot: Self::Slot) -> Result<Self, ValueError>

Source§

impl FromField for u8

Source§

type Slot = Option<u8>

Source§

fn accept_text(slot: &mut Self::Slot, value: &str) -> Result<(), ValueError>

Source§

fn finish(slot: Self::Slot) -> Result<Self, ValueError>

Source§

impl FromField for u16

Source§

type Slot = Option<u16>

Source§

fn accept_text(slot: &mut Self::Slot, value: &str) -> Result<(), ValueError>

Source§

fn finish(slot: Self::Slot) -> Result<Self, ValueError>

Source§

impl FromField for u32

Source§

type Slot = Option<u32>

Source§

fn accept_text(slot: &mut Self::Slot, value: &str) -> Result<(), ValueError>

Source§

fn finish(slot: Self::Slot) -> Result<Self, ValueError>

Source§

impl FromField for u64

Source§

type Slot = Option<u64>

Source§

fn accept_text(slot: &mut Self::Slot, value: &str) -> Result<(), ValueError>

Source§

fn finish(slot: Self::Slot) -> Result<Self, ValueError>

Source§

impl FromField for u128

Source§

type Slot = Option<u128>

Source§

fn accept_text(slot: &mut Self::Slot, value: &str) -> Result<(), ValueError>

Source§

fn finish(slot: Self::Slot) -> Result<Self, ValueError>

Source§

impl FromField for usize

Source§

type Slot = Option<usize>

Source§

fn accept_text(slot: &mut Self::Slot, value: &str) -> Result<(), ValueError>

Source§

fn finish(slot: Self::Slot) -> Result<Self, ValueError>

Source§

impl<T: FromField> FromField for Option<T>

Source§

type Slot = (bool, <T as FromField>::Slot)

Source§

fn accept_text(slot: &mut Self::Slot, value: &str) -> Result<(), ValueError>

Source§

fn accept_file( slot: &mut Self::Slot, file: FileValue<'_>, ) -> Result<(), ValueError>

Source§

fn finish(slot: Self::Slot) -> Result<Self, ValueError>

Source§

impl<T: FromField> FromField for Vec<T>

Source§

type Slot = Vec<T>

Source§

fn accept_text(slot: &mut Self::Slot, value: &str) -> Result<(), ValueError>

Source§

fn accept_file( slot: &mut Self::Slot, file: FileValue<'_>, ) -> Result<(), ValueError>

Source§

fn finish(slot: Self::Slot) -> Result<Self, ValueError>

Implementors§