Skip to main content

FormBuilder

Trait FormBuilder 

Source
pub trait FormBuilder {
    type Output;

    // Required methods
    fn text(&mut self, name: &str, value: &str) -> Result<(), FormError>;
    fn file(&mut self, name: &str, file: FileValue<'_>) -> Result<(), FormError>;
    fn finish(self) -> Result<Self::Output, FormError>;

    // Provided methods
    fn file_field(&self, name: &str) -> Option<bool> { ... }
    fn finish_all(self, errors: &mut Vec<FormError>) -> Option<Self::Output>
       where Self: Sized { ... }
}
Available on crate feature alloc only.
Expand description

What form! generates, and what a hand-written FromForm implementation provides. Reports problems as FormError::Extract { reason, field }.

§Examples

use form_net::{ExtractError, FileValue, FormBuilder, FormError, FromForm};

/// Accepts any field names, keeping the last `q`.
#[derive(Default)]
struct LastQ(Option<String>);

impl FormBuilder for LastQ {
    type Output = Query;
    fn text(&mut self, name: &str, value: &str) -> Result<(), FormError> {
        if name == "q" { self.0 = Some(value.to_owned()); }
        Ok(())
    }
    fn file(&mut self, _name: &str, _file: FileValue<'_>) -> Result<(), FormError> {
        Err(FormError::Extract { reason: ExtractError::WrongKind, field: Some("q") })
    }
    fn finish(self) -> Result<Query, FormError> {
        Ok(Query { q: self.0.unwrap_or_default() })
    }
}

struct Query { q: String }
impl FromForm for Query { type Builder = LastQ; }

assert_eq!(Query::from_query(b"q=a&q=b")?.q, "b");

Required Associated Types§

Source

type Output

The struct being built.

Required Methods§

Source

fn text(&mut self, name: &str, value: &str) -> Result<(), FormError>

One text field.

§Errors

Extract { .. }.

Source

fn file(&mut self, name: &str, file: FileValue<'_>) -> Result<(), FormError>

One file.

§Errors

Extract { .. }.

Source

fn finish(self) -> Result<Self::Output, FormError>

The struct, once every field has arrived.

§Errors

Extract { Missing | Invalid, .. }.

Provided Methods§

Source

fn file_field(&self, name: &str) -> Option<bool>

What a file under name is to the struct: Some(true) a declared field, Some(false) an ignored name, None an unknown one. FromForm::from_upload_mut (feature fs) moves only declared files out of the upload. The default says every name is declared.

Source

fn finish_all(self, errors: &mut Vec<FormError>) -> Option<Self::Output>
where Self: Sized,

As finish, but records every field’s error in errors instead of stopping at the first; None when any field failed. The default records the one error finish returns.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§