Crates.io | deserialize_form_style_query_parameter |
lib.rs | deserialize_form_style_query_parameter |
version | 0.2.2 |
source | src |
created_at | 2024-01-22 02:55:55.50901 |
updated_at | 2024-02-17 02:36:36.358223 |
description | Deserialize Form style Query parameters with Serde. |
homepage | |
repository | https://github.com/Ayana-chan/ipfs_storage_cruster/tree/master/crates/deserialize_form_style_query_parameter |
max_upload_size | |
id | 1108258 |
size | 15,794 |
Tiny crate to deserialize form style data from query parameters in http GET request. Built upon Serde.
/users?id=3,4,5
), whose elements' type T impls FromStr
trait./users?id=role,admin,firstName,Alex
) that impls FromStr
trait in a certain way (a little complex).NOTE: If you only want to deserialize with
FromStr
, you may just need serde_with.
use deserialize_form_style_query_parameter::{form_vec_deserialize, option_form_vec_deserialize};
use serde::{Deserialize, Serialize};
#[derive(Debug, PartialEq, Deserialize, Serialize)]
struct QueryParams {
id: Option<u32>,
#[serde(deserialize_with = "form_vec_deserialize")]
user_ids: Vec<u8>,
#[serde(deserialize_with = "option_form_vec_deserialize", default)]
user_names: Option<Vec<String>>,
}
fn main() {
let correct_answer = QueryParams {
id: Some(12345),
user_ids: vec![1, 3],
user_names: None,
};
// serde_urlencoded::from_str is executed by axum::extract::Query.
// https://docs.rs/axum/latest/src/axum/extract/query.rs.html#87
// So handler(Query(para)) also works.
let example_params: QueryParams =
serde_urlencoded::from_str("id=12345&user_ids=1,2b,3")
.unwrap();
assert_eq!(example_params, correct_answer);
}
use deserialize_form_style_query_parameter::pure_from_str;
use serde::{Deserialize, Serialize};
use std::str::FromStr;
#[derive(Debug, PartialEq, Deserialize, Serialize)]
struct Address {
city: String,
postcode: String,
}
impl FromStr for Address {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
// This function might be very complex in actual situations.
let parts: Vec<&str> = s.split(',').collect();
if parts.len() != 4{
return Err(());
}
if !parts[0].eq("city") || !parts[2].eq("postcode") {
return Err(());
}
Ok(Address{
city: parts[1].to_string(),
postcode: parts[3].to_string()
})
}
}
#[derive(Debug, PartialEq, Deserialize, Serialize)]
struct QueryParams {
id: Option<u32>,
#[serde(deserialize_with = "pure_from_str")]
address: Address,
}
fn main() {
let correct_answer = QueryParams {
id: Some(12345),
address: Address {
city: "Teyvat".to_string(),
postcode: "191919".to_string()
}
};
let example_params: QueryParams =
serde_urlencoded::from_str("id=12345&address=city,Teyvat,postcode,191919")
.unwrap();
assert_eq!(example_params, correct_answer);
}
For more details and usage , please refer to Document.