Coder Social home page Coder Social logo

serde_trim's Introduction

serde_trim:: serde deserialize_with string trim

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

Support trim

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

Supports multiple std::collections types

The current also supports non-empty trim

support function collection:

  • 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
  • linkedlist_string_trim
  • option_string_trim
  • string_trim
  • vec_non_empty_string_trim
  • vec_string_trim
  • vecdeque_non_empty_string_trim
  • vecdeque_string_trim

how to use

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"]
    );
}

serde_trim's People

Contributors

baoyachi avatar dependabot[bot] avatar leroyguillaume avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

leroyguillaume

serde_trim's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.