Skip to content

Commit

Permalink
Merge pull request #6 from leroyguillaume/main
Browse files Browse the repository at this point in the history
Add deserializers to remove empty items
  • Loading branch information
baoyachi authored Dec 18, 2023
2 parents d68f6ae + bc8256f commit 416aa85
Showing 1 changed file with 117 additions and 0 deletions.
117 changes: 117 additions & 0 deletions lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,31 @@ macro_rules! iter_trim {
};
}

macro_rules! iter_non_empty_trim {
($fn_name:ident,$t:ty) => {
pub fn $fn_name<'de, D>(d: D) -> Result<$t, D::Error>
where
D: de::Deserializer<'de>,
{
collection_non_empty_trim(d)
}
};
}

iter_trim!(btreeset_string_trim, BTreeSet<String>);
iter_trim!(vec_string_trim, Vec<String>);
iter_trim!(hashset_string_trim, HashSet<String>);
iter_trim!(vecdeque_string_trim, VecDeque<String>);
iter_trim!(linkedlist_string_trim, LinkedList<String>);
iter_trim!(binaryheap_string_trim, BinaryHeap<String>);

iter_non_empty_trim!(btreeset_non_empty_string_trim, BTreeSet<String>);
iter_non_empty_trim!(vec_non_empty_string_trim, Vec<String>);
iter_non_empty_trim!(hashset_non_empty_string_trim, HashSet<String>);
iter_non_empty_trim!(vecdeque_non_empty_string_trim, VecDeque<String>);
iter_non_empty_trim!(linkedlist_non_empty_string_trim, LinkedList<String>);
iter_non_empty_trim!(binaryheap_non_empty_string_trim, BinaryHeap<String>);

fn collection_trim<'de, C, D, S>(d: D) -> Result<C, D::Error>
where
S: TrimInPlace,
Expand All @@ -56,6 +74,26 @@ where
Ok(de_string)
}

fn collection_non_empty_trim<'de, C, D, S>(d: D) -> Result<C, D::Error>
where
S: TrimInPlace,
C: Deserialize<'de> + IntoIterator<Item = S> + FromIterator<String>,
D: de::Deserializer<'de>,
{
let de_string: C = C::deserialize(d)?
.into_iter()
.filter_map(|mut x| {
let x = x.trim_in_place().to_string();
if x.is_empty() {
None
} else {
Some(x)
}
})
.collect();
Ok(de_string)
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -192,4 +230,83 @@ mod tests {
vec!["rust", "hello", "b ar", "", "foo"]
);
}

#[test]
fn test_vec_non_empty_string_trim() {
#[derive(Deserialize)]
struct VecFoo {
#[serde(deserialize_with = "vec_non_empty_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"]);
}

#[test]
fn test_btreeset_non_empty_string_trim() {
#[derive(Deserialize)]
struct BTreeSetFoo {
#[serde(deserialize_with = "btreeset_non_empty_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(["foo".into(), "b ar".into(), "hello".into(), "rust".into()]);
assert_eq!(foo.name, expected);
}

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

#[test]
fn test_vecdeque_non_empty_string_trim() {
#[derive(Deserialize)]
struct VecDequeFoo {
#[serde(deserialize_with = "vecdeque_non_empty_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"]);
}

#[test]
fn test_linkedlist_non_empty_string_trim() {
#[derive(Deserialize)]
struct LinkedListFoo {
#[serde(deserialize_with = "linkedlist_non_empty_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(["foo".into(), "b ar".into(), "hello".into(), "rust".into(),])
);
}

#[test]
fn test_binaryheap_non_empty_string_trim() {
#[derive(Deserialize)]
struct BinaryHeapFoo {
#[serde(deserialize_with = "binaryheap_non_empty_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 comments on commit 416aa85

Please sign in to comment.