-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
108 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,6 @@ words: | |
- deserializers | ||
- detrim | ||
- docsrs | ||
- hashset | ||
- rdme | ||
- serde |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
{ | ||
"cSpell.autoFormatConfigFile": true | ||
"cSpell.autoFormatConfigFile": true, | ||
"rust-analyzer.cargo.features": "all", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use core::iter::FromIterator as _; | ||
use std::collections::HashSet; | ||
|
||
use alloc::{borrow::ToOwned as _, string::String, vec::Vec}; | ||
|
||
use serde::{Deserialize as _, Deserializer}; | ||
|
||
/// Trims set of strings during deserialization. | ||
/// | ||
/// Strings are deduplicated _after_ being trimmed (i.e., differences in extraneous whitespace are | ||
/// handled). | ||
pub fn hashset_string<'a, D: Deserializer<'a>>(de: D) -> Result<HashSet<String>, D::Error> { | ||
let mut set = Vec::<String>::deserialize(de)?; | ||
|
||
for item in &mut set { | ||
*item = item.trim().to_owned(); | ||
} | ||
|
||
Ok(HashSet::from_iter(set)) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use serde::Deserialize; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn hashset_string() { | ||
#[derive(Debug, Deserialize, PartialEq, Eq)] | ||
struct Foo { | ||
#[serde(deserialize_with = "super::hashset_string")] | ||
foo: HashSet<String>, | ||
} | ||
|
||
impl Foo { | ||
fn new(foo: impl IntoIterator<Item = impl Into<String>>) -> Self { | ||
Self { | ||
foo: foo.into_iter().map(Into::into).collect(), | ||
} | ||
} | ||
} | ||
|
||
serde_json::from_str::<Foo>(r#"{ "foo": 1 }"#).unwrap_err(); | ||
serde_json::from_str::<Foo>(r#"{ "foo": "" }"#).unwrap_err(); | ||
|
||
assert_eq!( | ||
Foo::new([""; 0]), | ||
serde_json::from_str(r#"{ "foo": [] }"#).unwrap(), | ||
); | ||
assert_eq!( | ||
Foo::new([""]), | ||
serde_json::from_str(r#"{ "foo": [""] }"#).unwrap(), | ||
); | ||
assert_eq!( | ||
Foo::new([""]), | ||
serde_json::from_str(r#"{ "foo": [" "] }"#).unwrap(), | ||
); | ||
assert_eq!( | ||
Foo::new(["bar"]), | ||
serde_json::from_str(r#"{ "foo": ["bar"] }"#).unwrap(), | ||
); | ||
assert_eq!( | ||
Foo::new(["bar"]), | ||
serde_json::from_str(r#"{ "foo": [" bar"] }"#).unwrap(), | ||
); | ||
assert_eq!( | ||
Foo::new(["bar"]), | ||
serde_json::from_str(r#"{ "foo": [" bar"] }"#).unwrap(), | ||
); | ||
assert_eq!( | ||
Foo::new(["bar"]), | ||
serde_json::from_str(r#"{ "foo": ["bar "] }"#).unwrap(), | ||
); | ||
assert_eq!( | ||
Foo::new(["bar"]), | ||
serde_json::from_str(r#"{ "foo": [" bar "] }"#).unwrap(), | ||
); | ||
assert_eq!( | ||
Foo::new(["bar"]), | ||
serde_json::from_str(r#"{ "foo": [" bar ", " bar "] }"#).unwrap(), | ||
); | ||
assert_eq!( | ||
Foo::new(["bar"]), | ||
serde_json::from_str(r#"{ "foo": [" bar ", " bar"] }"#).unwrap(), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters