Function from_str

Source
pub fn from_str<'input: 'facet, 'facet, T: Facet<'facet>>(
    urlencoded: &'input str,
) -> Result<T, UrlEncodedError<'facet>>
Expand description

Deserializes a URL encoded form data string into a value of type T that implements Facet.

This function supports parsing both flat structures and nested structures using the common bracket notation. For example, a form field like user[name] will be deserialized into a struct with a field named user that contains a field named name.

§Nested Structure Format

For nested structures, the library supports the standard bracket notation used in most web frameworks:

  • Simple nested objects: object[field]=value
  • Deeply nested objects: object[field1][field2]=value

§Basic Example

use facet::Facet;
use facet_urlencoded::from_str;

#[derive(Debug, Facet, PartialEq)]
struct SearchParams {
    query: String,
    page: u64,
}

let query_string = "query=rust+programming&page=2";

let params: SearchParams = from_str(query_string).expect("Failed to parse URL encoded data");
assert_eq!(params, SearchParams { query: "rust programming".to_string(), page: 2 });

§Nested Structure Example

use facet::Facet;
use facet_urlencoded::from_str;

#[derive(Debug, Facet, PartialEq)]
struct Address {
    street: String,
    city: String,
}

#[derive(Debug, Facet, PartialEq)]
struct User {
    name: String,
    address: Address,
}

let query_string = "name=John+Doe&address[street]=123+Main+St&address[city]=Anytown";

let user: User = from_str(query_string).expect("Failed to parse URL encoded data");
assert_eq!(user, User {
    name: "John Doe".to_string(),
    address: Address {
        street: "123 Main St".to_string(),
        city: "Anytown".to_string(),
    },
});