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>;
}
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, .. }.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§