pub trait FromForm: Sized {
type Builder: FormBuilder<Output = Self> + Default;
// Provided methods
fn from_urlencoded(input: &[u8]) -> Result<Self, FormError> { ... }
fn from_query(query: &[u8]) -> Result<Self, FormError> { ... }
fn from_multipart(
content_type: &[u8],
body: &[u8],
) -> Result<Self, FormError> { ... }
fn from_body(content_type: &[u8], body: &[u8]) -> Result<Self, FormError> { ... }
fn from_urlencoded_with(
input: &[u8],
limits: &Limits,
policy: &Policy,
) -> Result<Self, FormError> { ... }
fn from_body_with(
content_type: &[u8],
body: &[u8],
limits: &Limits,
policy: &Policy,
) -> Result<Self, FormError> { ... }
fn from_body_inspected<I: Inspector>(
content_type: &[u8],
body: &[u8],
limits: &Limits,
policy: &Policy,
inspector: &mut I,
) -> Result<Self, FormError> { ... }
fn from_body_all(
content_type: &[u8],
body: &[u8],
) -> Result<Self, FormErrors> { ... }
fn from_body_all_with(
content_type: &[u8],
body: &[u8],
limits: &Limits,
policy: &Policy,
) -> Result<Self, FormErrors> { ... }
fn from_upload_mut(upload: &mut Upload) -> Result<Self, FormError> { ... }
fn from_upload(upload: Upload) -> Result<Self, FormError> { ... }
}alloc only.Expand description
A struct that can be read from a form. Implemented by
form!; every method is provided.
§Examples
use form_net::FromForm;
form_net::form! {
pub struct Login {
pub email: String,
pub password: form_net::Secret,
pub remember: bool,
}
ignore = ["csrf_token"];
}
let login = Login::from_body(b"application/x-www-form-urlencoded", b"email=a%40b.example&password=x&csrf_token=t")?;
assert_eq!((login.email.as_str(), login.remember), ("a@b.example", false));Required Associated Types§
Sourcetype Builder: FormBuilder<Output = Self> + Default
type Builder: FormBuilder<Output = Self> + Default
The builder that collects the fields.
Provided Methods§
Sourcefn from_urlencoded(input: &[u8]) -> Result<Self, FormError>
fn from_urlencoded(input: &[u8]) -> Result<Self, FormError>
From a urlencoded body, with the default limits and policy.
§Errors
The parser’s errors; Extract { .. }.
Sourcefn from_query(query: &[u8]) -> Result<Self, FormError>
fn from_query(query: &[u8]) -> Result<Self, FormError>
Sourcefn from_body(content_type: &[u8], body: &[u8]) -> Result<Self, FormError>
fn from_body(content_type: &[u8], body: &[u8]) -> Result<Self, FormError>
From either encoding, by the Content-Type.
§Errors
UnsupportedMediaType; the parsers’ errors; Extract { .. }.
Sourcefn from_urlencoded_with(
input: &[u8],
limits: &Limits,
policy: &Policy,
) -> Result<Self, FormError>
fn from_urlencoded_with( input: &[u8], limits: &Limits, policy: &Policy, ) -> Result<Self, FormError>
Sourcefn from_body_with(
content_type: &[u8],
body: &[u8],
limits: &Limits,
policy: &Policy,
) -> Result<Self, FormError>
fn from_body_with( content_type: &[u8], body: &[u8], limits: &Limits, policy: &Policy, ) -> Result<Self, FormError>
Sourcefn from_body_inspected<I: Inspector>(
content_type: &[u8],
body: &[u8],
limits: &Limits,
policy: &Policy,
inspector: &mut I,
) -> Result<Self, FormError>
fn from_body_inspected<I: Inspector>( content_type: &[u8], body: &[u8], limits: &Limits, policy: &Policy, inspector: &mut I, ) -> Result<Self, FormError>
From either encoding, with an inspector in the same pass: the inspector and the struct see the same bytes.
§Errors
As from_body_with; Refused.
Sourcefn from_body_all(content_type: &[u8], body: &[u8]) -> Result<Self, FormErrors>
fn from_body_all(content_type: &[u8], body: &[u8]) -> Result<Self, FormErrors>
From either encoding, collecting every field’s error instead of stopping at the first (D-090), so a form can be shown again with a message beside each field.
§Errors
FormErrors: the parse error alone, or every extraction error.
Sourcefn from_body_all_with(
content_type: &[u8],
body: &[u8],
limits: &Limits,
policy: &Policy,
) -> Result<Self, FormErrors>
fn from_body_all_with( content_type: &[u8], body: &[u8], limits: &Limits, policy: &Policy, ) -> Result<Self, FormErrors>
Sourcefn from_upload_mut(upload: &mut Upload) -> Result<Self, FormError>
Available on crate feature fs only.
fn from_upload_mut(upload: &mut Upload) -> Result<Self, FormError>
fs only.From an upload that stays usable (D-090): text fields are read by
reference, and only the files the struct declares are moved out of
it; files under ignore = [..] names stay in the upload for the
caller. Unknown names are refused as in
from_upload. On error, files already moved
into the builder are dropped (deleted).
§Errors
Extract { .. }.
§Examples
use form_net::FromForm;
form_net::form! {
pub struct Post { pub title: String, pub cover: form_net::StoredFile }
ignore = ["attachment"];
}
let body = b"--X\r\nContent-Disposition: form-data; name=title\r\n\r\nHi\r\n--X\r\nContent-Disposition: form-data; name=cover; filename=c.png\r\n\r\nC\r\n--X\r\nContent-Disposition: form-data; name=attachment; filename=a.pdf\r\n\r\nA\r\n--X--";
let mut upload = form_net::read_multipart(b"multipart/form-data; boundary=X", &body[..])?;
let post = Post::from_upload_mut(&mut upload)?;
assert_eq!((post.title.as_str(), post.cover.len()), ("Hi", 1));
assert_eq!(upload.field("title"), Some("Hi"));
assert!(upload.file("cover").is_none() && upload.file("attachment").is_some());Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".