#serialization #deserialize #serde #urlencoded #parse-url #default-value

serde_html_form

(对 application/x-www-form-urlencoded 格式的序列化和反序列化支持)

9 个版本

0.2.6 2024 年 3 月 28 日
0.2.5 2024 年 3 月 10 日
0.2.4 2024 年 1 月 27 日
0.2.3 2023 年 12 月 6 日
0.1.0 2022 年 4 月 19 日

113编码 中排名

Download history 18078/week @ 2024-04-08 23955/week @ 2024-04-15 27000/week @ 2024-04-22 27540/week @ 2024-04-29 30154/week @ 2024-05-06 28003/week @ 2024-05-13 28565/week @ 2024-05-20 22685/week @ 2024-05-27 26848/week @ 2024-06-03 28043/week @ 2024-06-10 26900/week @ 2024-06-17 32929/week @ 2024-06-24 28104/week @ 2024-07-01 35097/week @ 2024-07-08 36253/week @ 2024-07-15 37863/week @ 2024-07-22

138,536 每月下载量
91crate中使用(直接使用11个)

MIT 许可证

67KB
2K SLoC

serde_html_form

(对 application/x-www-form-urlencoded 格式的序列化和反序列化支持)。

这是一个用于将数据序列化到和从 application/x-www-form-urlencoded 格式进行反序列化的 Rust 库。它建立在高性能通用序列化框架 Serde 和 Rust 的 URL 解析器 rust-url 之上。

它是 serde_urlencoded 的一个分支,增加了对具有序列类型字段的映射或结构的额外支持(例如,Vec)。它还支持在值中使用 Option,将 foo= 视为 foo: None

示例

value=x&value=y 之类的序列

use serde::Deserialize;

#[derive(Debug, PartialEq, Deserialize)]
struct Form {
    // By default, at least one occurrence of this field must be present (this
    // is mandated by how serde works).
    //
    // Since this is usually not desired, use `serde(default)` to instantiate
    // this struct's field with a `Default` value if input doesn't contain that
    // field.
    #[serde(default)]
    value: Vec<String>,
}

assert_eq!(
    serde_html_form::from_str("value=&value=abc"),
    Ok(Form { value: vec!["".to_owned(), "abc".to_owned()] })
);
assert_eq!(
    serde_html_form::from_str(""),
    Ok(Form { value: vec![] })
);

value=[x]&value=[y] 之类的序列

use serde::Deserialize;

#[derive(Debug, PartialEq, Deserialize)]
struct Form {
    // If you want to support `value[]=x&value[]=y`, you can use
    // `serde(rename)`. You could even use `serde(alias)` instead to allow both,
    // but note that mixing both in one input string would also be allowed then.
    #[serde(default, rename = "value[]")]
    value: Vec<String>,
}

assert_eq!(
    serde_html_form::from_str("value[]=x&value[]=y"),
    Ok(Form { value: vec!["x".to_owned(), "y".to_owned()] })
);
assert_eq!(
    serde_html_form::from_str("value[]=hello"),
    Ok(Form { value: vec!["hello".to_owned()] })
);

可选值

use serde::Deserialize;

#[derive(Debug, PartialEq, Deserialize)]
struct Form {
    // Finally, this crate also supports deserializing empty values as `None`
    // if your values are `Option`s.
    // Note that serde's `Deserialize` derive implicitly allows omission of
    // `Option`-typed fields (except when combined with some other attributes).
    single: Option<u32>,
    // Not using `serde(default)` here to require at least one occurrence.
    at_least_one: Vec<Option<u32>>,
}

assert_eq!(
    serde_html_form::from_str("at_least_one=5"),
    Ok(Form {
        // Implicit `serde(default)` in action.
        single: None,
        // `serde_html_form`'s support for optional values being used.
        at_least_one: vec![Some(5)],
    })
);
assert_eq!(
    serde_html_form::from_str("at_least_one=&single=1&at_least_one=5"),
    Ok(Form {
        single: Some(1),
        at_least_one: vec![
            // Empty strings get deserialized as `None`.
            None,
            // It's no problem that the `at_least_one` field repetitions are
            // not consecutive (single comes in between).
            Some(5),
        ]
    })
);
assert!(
    serde_html_form::from_str::<Form>("").is_err(),
    "at_least_one is not part of the input"
);

许可证

此crate受MIT许可证的许可(LICENSEhttps://opensource.org/license/mit/)。

依赖关系

~0.9–1.3MB
~24K SLoC