#serde #serde-json #deserialize-with #derive-deserialize #deserialize-trim

serde_trim

serde deserialize_with 字符串 trim。支持多种 std::collections 类型

7 个版本 (2 个稳定版)

1.1.0 2023年12月18日
1.0.0 2023年7月23日
0.4.0 2022年9月1日
0.3.1 2022年6月17日
0.1.0 2022年6月17日

#182编码

Download history 1344/week @ 2024-03-13 1496/week @ 2024-03-20 1523/week @ 2024-03-27 1353/week @ 2024-04-03 1718/week @ 2024-04-10 2142/week @ 2024-04-17 2964/week @ 2024-04-24 2511/week @ 2024-05-01 2349/week @ 2024-05-08 2545/week @ 2024-05-15 1661/week @ 2024-05-22 1932/week @ 2024-05-29 1748/week @ 2024-06-05 2886/week @ 2024-06-12 2920/week @ 2024-06-19 3666/week @ 2024-06-26

每月11,581 次下载
用于 2 crates

MIT AND Apache-2.0

17KB
287

serde_trim:: serde deserialize_with string trim

GitHub Actions Crates.io Docs.rs Download DepStatus Coverage Status

支持 trim

  • String
  • Option<String>
  • Vec<String>
  • BTreeSet<String>
  • HashSet<String>
  • VecDeque<String>
  • LinkedList<String>
  • BinaryHeap<String>

支持多种 std::collections 类型

当前也支持 非空 trim

支持函数集合

  • binaryheap_non_empty_string_trim
  • binaryheap_string_trim
  • btreeset_non_empty_string_trim
  • btreeset_string_trim
  • hashset_non_empty_string_trim
  • hashset_string_trim
  • inkedlist_non_empty_string_trim
  • inkedlist_string_trim
  • option_string_trim
  • string_trim
  • vec_non_empty_string_trim
  • vec_string_trim
  • vecdeque_non_empty_string_trim
  • vecdeque_string_trim

使用方法

use serde_derive::Deserialize;
use serde_trim::*;
use std::collections::*;

fn main() {
    #[derive(Deserialize)]
    struct Foo {
        #[serde(deserialize_with = "string_trim")]
        name: String,
    }
    let json = r#"{"name":" "}"#;
    let foo = serde_json::from_str::<Foo>(json).unwrap();
    assert_eq!(foo.name, "");

    #[derive(Deserialize)]
    struct OptionFoo {
        #[serde(deserialize_with = "option_string_trim")]
        name: Option<String>,
    }
    let json = r#"{"name":" "}"#;
    let foo = serde_json::from_str::<OptionFoo>(json).unwrap();
    assert_eq!(foo.name, None);

    #[derive(Deserialize)]
    struct OptionBar {
        #[serde(default, deserialize_with = "option_string_trim")]
        name: Option<String>,
        addr: String,
    }
    let json = r#"{"addr":"ABC"}"#;
    let foo = serde_json::from_str::<OptionBar>(json).unwrap();
    assert_eq!(foo.name, None);
    assert_eq!(foo.addr, "ABC");

    #[derive(Deserialize)]
    struct VecFoo {
        #[serde(deserialize_with = "vec_string_trim")]
        name: Vec<String>,
    }
    let json = r#"{"name":["   ","foo","b ar","hello ","  rust"]}"#;
    let foo = serde_json::from_str::<VecFoo>(json).unwrap();
    assert_eq!(foo.name, vec!["", "foo", "b ar", "hello", "rust"]);

    #[derive(Deserialize)]
    struct BTreeSetFoo {
        #[serde(deserialize_with = "btreeset_string_trim")]
        name: BTreeSet<String>,
    }
    let json = r#"{"name":["   ","foo","b ar","hello ","  rust"]}"#;
    let foo = serde_json::from_str::<BTreeSetFoo>(json).unwrap();
    let expected: BTreeSet<String> = BTreeSet::from_iter([
        "".into(),
        "foo".into(),
        "b ar".into(),
        "hello".into(),
        "rust".into(),
    ]);
    assert_eq!(foo.name, expected);

    #[derive(Deserialize)]
    struct HashSetFoo {
        #[serde(deserialize_with = "hashset_string_trim")]
        name: HashSet<String>,
    }
    let json = r#"{"name":["   ","foo","b ar","hello ","  rust"]}"#;
    let foo = serde_json::from_str::<HashSetFoo>(json).unwrap();
    let expected: HashSet<String> = HashSet::from_iter([
        "".into(),
        "foo".into(),
        "b ar".into(),
        "hello".into(),
        "rust".into(),
    ]);
    assert_eq!(foo.name, expected);

    #[derive(Deserialize)]
    struct VecDequeFoo {
        #[serde(deserialize_with = "vecdeque_string_trim")]
        name: VecDeque<String>,
    }
    let json = r#"{"name":["   ","foo","b ar","hello ","  rust"]}"#;
    let foo = serde_json::from_str::<VecDequeFoo>(json).unwrap();
    assert_eq!(foo.name, vec!["", "foo", "b ar", "hello", "rust"]);

    #[derive(Deserialize)]
    struct LinkedListFoo {
        #[serde(deserialize_with = "linkedlist_string_trim")]
        name: LinkedList<String>,
    }
    let json = r#"{"name":["   ","foo","b ar","hello ","  rust"]}"#;
    let foo = serde_json::from_str::<LinkedListFoo>(json).unwrap();
    assert_eq!(
        foo.name,
        LinkedList::from_iter([
            "".into(),
            "foo".into(),
            "b ar".into(),
            "hello".into(),
            "rust".into(),
        ])
    );

    #[derive(Deserialize)]
    struct BinaryHeapFoo {
        #[serde(deserialize_with = "binaryheap_string_trim")]
        name: BinaryHeap<String>,
    }
    let json = r#"{"name":["   ","foo","b ar","hello ","  rust"]}"#;
    let foo = serde_json::from_str::<BinaryHeapFoo>(json).unwrap();
    assert_eq!(
        foo.name.into_vec(),
        vec!["rust", "hello", "b ar", "", "foo"]
    );
}

依赖

~0.4–1MB
~23K SLoC