-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Custom Option<Cow<str>> deserializer
- Loading branch information
1 parent
637f985
commit a69941c
Showing
14 changed files
with
152 additions
and
40 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
File renamed without changes.
File renamed without changes.
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,3 @@ | ||
// This file is part of ICU4X. For terms of use, please see the file | ||
// called LICENSE at the top level of the ICU4X source tree | ||
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). |
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,6 @@ | ||
#![no_std] | ||
extern crate alloc; | ||
|
||
pub mod deduplicating_array; | ||
pub mod array_of_cow; | ||
pub mod option_of_cow; |
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,122 @@ | ||
// This file is part of ICU4X. For terms of use, please see the file | ||
// called LICENSE at the top level of the ICU4X source tree | ||
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). | ||
|
||
use alloc::borrow::Cow; | ||
use alloc::fmt; | ||
use alloc::string::String; | ||
use alloc::borrow::ToOwned; | ||
|
||
pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<Cow<'de, str>>, D::Error> | ||
where | ||
D: serde::de::Deserializer<'de> | ||
{ | ||
use serde::de::Error; | ||
use serde::de::Unexpected; | ||
|
||
// Note: The following visitor is taken from serde::private::de::borrow_cow_str | ||
struct CowStrVisitor; | ||
|
||
impl<'a> serde::de::Visitor<'a> for CowStrVisitor { | ||
type Value = Cow<'a, str>; | ||
|
||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | ||
formatter.write_str("a string") | ||
} | ||
|
||
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> | ||
where | ||
E: Error, | ||
{ | ||
Ok(Cow::Owned(v.to_owned())) | ||
} | ||
|
||
fn visit_borrowed_str<E>(self, v: &'a str) -> Result<Self::Value, E> | ||
where | ||
E: Error, | ||
{ | ||
Ok(Cow::Borrowed(v)) | ||
} | ||
|
||
fn visit_string<E>(self, v: String) -> Result<Self::Value, E> | ||
where | ||
E: Error, | ||
{ | ||
Ok(Cow::Owned(v)) | ||
} | ||
|
||
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E> | ||
where | ||
E: Error, | ||
{ | ||
match alloc::str::from_utf8(v) { | ||
Ok(s) => Ok(Cow::Owned(s.to_owned())), | ||
Err(_) => Err(Error::invalid_value(Unexpected::Bytes(v), &self)), | ||
} | ||
} | ||
|
||
fn visit_borrowed_bytes<E>(self, v: &'a [u8]) -> Result<Self::Value, E> | ||
where | ||
E: Error, | ||
{ | ||
match alloc::str::from_utf8(v) { | ||
Ok(s) => Ok(Cow::Borrowed(s)), | ||
Err(_) => Err(Error::invalid_value(Unexpected::Bytes(v), &self)), | ||
} | ||
} | ||
|
||
fn visit_byte_buf<E>(self, v: alloc::vec::Vec<u8>) -> Result<Self::Value, E> | ||
where | ||
E: Error, | ||
{ | ||
match String::from_utf8(v) { | ||
Ok(s) => Ok(Cow::Owned(s)), | ||
Err(e) => Err(Error::invalid_value( | ||
Unexpected::Bytes(&e.into_bytes()), | ||
&self, | ||
)), | ||
} | ||
} | ||
} | ||
|
||
struct OptionCowStrVisitor; | ||
|
||
impl<'a> serde::de::Visitor<'a> for OptionCowStrVisitor { | ||
type Value = Option<Cow<'a, str>>; | ||
|
||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | ||
formatter.write_str("a string or null") | ||
} | ||
|
||
fn visit_none<E>(self) -> Result<Self::Value, E> | ||
where | ||
E: Error, | ||
{ | ||
Ok(None) | ||
} | ||
|
||
fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error> | ||
where | ||
D: serde::de::Deserializer<'a>, | ||
{ | ||
Ok(Some(deserializer.deserialize_str(CowStrVisitor)?)) | ||
} | ||
} | ||
|
||
deserializer.deserialize_option(OptionCowStrVisitor) | ||
} | ||
|
||
#[test] | ||
fn test() { | ||
#[derive(Debug, PartialEq, serde::Serialize, serde::Deserialize)] | ||
struct Demo<'s>( | ||
#[serde(borrow, deserialize_with = "deserialize")] | ||
Option<Cow<'s, str>>, | ||
); | ||
|
||
let data_orig = Demo(Some("Hello world".into())); | ||
let bytes = postcard::to_stdvec(&data_orig).expect("serialize"); | ||
let data_new = postcard::from_bytes::<Demo>(&bytes).expect("deserialize"); | ||
assert_eq!(data_orig, data_new); | ||
assert!(matches!(data_new.0, Some(Cow::Borrowed(_)))); | ||
} |