-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AbciInfo reimplemented as domain type with JSON serialization
- Loading branch information
1 parent
1612d1c
commit 356324a
Showing
10 changed files
with
340 additions
and
6 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
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,52 @@ | ||
//! Serde JSON serializers | ||
//! | ||
//! Serializers and deserializers for a transparent developer experience. | ||
//! | ||
//! CAUTION: There are no guarantees for backwards compatibility, this module should be considered | ||
//! an internal implementation detail which can vanish without further warning. Use at your own | ||
//! risk. | ||
//! | ||
//! All serializers are presented in a serializers::<Rust_nickname>::<JSON_representation_name> | ||
//! format. | ||
//! | ||
//! This example shows how to serialize Vec<u8> into different types of strings: | ||
//! ```ignore | ||
//! use serde::{Serialize, Deserialize}; | ||
//! use tendermint::serializers; | ||
//! | ||
//! #[derive(Serialize, Deserialize)] | ||
//! struct ByteTypes { | ||
//! | ||
//! #[serde(with="serializers::bytes::hexstring")] | ||
//! hexbytes: Vec<u8>, | ||
//! | ||
//! #[serde(with="serializers::bytes::base64string")] | ||
//! base64bytes: Vec<u8>, | ||
//! | ||
//! #[serde(with="serializers::bytes::string")] | ||
//! bytes: Vec<u8>, | ||
//! | ||
//! } | ||
//! ``` | ||
//! | ||
//! Available serializers: | ||
//! i64 <-> string: #[serde(with="serializers::from_str")] | ||
//! u64 <-> string: #[serde(with="serializers::from_str")] | ||
//! std::time::Duration <-> nanoseconds as string #[serde(with="serializers::time_duration")] | ||
//! Vec<u8> <-> HexString: #[serde(with="serializers::bytes::hexstring")] | ||
//! Vec<u8> <-> Base64String: #[serde(with="serializers::bytes::base64string")] | ||
//! Vec<u8> <-> String: #[serde(with="serializers::bytes::string")] | ||
//! | ||
//! Notes: | ||
//! * Any type that has the "FromStr" trait can be serialized into a string with | ||
//! serializers::primitives::string. | ||
//! * serializers::bytes::* deserializes a null value into an empty vec![]. | ||
|
||
// Todo: remove dead_code allowance as soon as more types are implemented | ||
#![allow(dead_code)] | ||
pub mod bytes; | ||
pub mod from_str; | ||
pub mod time_duration; | ||
|
||
mod custom; | ||
pub use custom::null_as_default; |
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,82 @@ | ||
//! Serialize/deserialize bytes (Vec<u8>) type | ||
|
||
/// Serialize into hexstring, deserialize from hexstring | ||
pub mod hexstring { | ||
use serde::{Deserialize, Deserializer, Serializer}; | ||
use subtle_encoding::hex; | ||
|
||
/// Deserialize hexstring into Vec<u8> | ||
pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
let string = Option::<String>::deserialize(deserializer)?.unwrap_or_default(); | ||
hex::decode_upper(&string) | ||
.or_else(|_| hex::decode(&string)) | ||
.map_err(serde::de::Error::custom) | ||
} | ||
|
||
/// Serialize from T into hexstring | ||
pub fn serialize<S, T>(value: &T, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
T: AsRef<[u8]>, | ||
{ | ||
let hex_bytes = hex::encode(value.as_ref()); | ||
let hex_string = String::from_utf8(hex_bytes).map_err(serde::ser::Error::custom)?; | ||
serializer.serialize_str(&hex_string) | ||
} | ||
} | ||
|
||
/// Serialize into base64string, deserialize from base64string | ||
pub mod base64string { | ||
use serde::{Deserialize, Deserializer, Serializer}; | ||
use subtle_encoding::base64; | ||
|
||
/// Deserialize base64string into Vec<u8> | ||
pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
let string = Option::<String>::deserialize(deserializer)?.unwrap_or_default(); | ||
base64::decode(&string).map_err(serde::de::Error::custom) | ||
} | ||
|
||
/// Serialize from T into base64string | ||
pub fn serialize<S, T>(value: &T, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
T: AsRef<[u8]>, | ||
{ | ||
let base64_bytes = base64::encode(value.as_ref()); | ||
let base64_string = String::from_utf8(base64_bytes).map_err(serde::ser::Error::custom)?; | ||
serializer.serialize_str(&base64_string) | ||
} | ||
} | ||
|
||
/// Serialize into string, deserialize from string | ||
pub(crate) mod string { | ||
use serde::{Deserialize, Deserializer, Serializer}; | ||
|
||
/// Deserialize string into Vec<u8> | ||
#[allow(dead_code)] | ||
pub(crate) fn deserialize<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
let string = Option::<String>::deserialize(deserializer)?.unwrap_or_default(); | ||
Ok(string.as_bytes().to_vec()) | ||
} | ||
|
||
/// Serialize from T into string | ||
#[allow(dead_code)] | ||
pub(crate) fn serialize<S, T>(value: &T, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
T: AsRef<[u8]>, | ||
{ | ||
let string = | ||
String::from_utf8(value.as_ref().to_vec()).map_err(serde::ser::Error::custom)?; | ||
serializer.serialize_str(&string) | ||
} | ||
} |
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,13 @@ | ||
//! Custom serializers | ||
|
||
use serde::{Deserialize, Deserializer}; | ||
|
||
/// Parse null as default | ||
pub fn null_as_default<'de, D, T: Default + Deserialize<'de>>( | ||
deserializer: D, | ||
) -> Result<T, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
Ok(<Option<T>>::deserialize(deserializer)?.unwrap_or_default()) | ||
} |
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,25 @@ | ||
//! Serialize and deserialize any `T` that implements [[std::str::FromStr]] | ||
//! and [[std::fmt::Display]] from or into string. Note this be used for | ||
//! all primitive data types (e.g. . | ||
use serde::{de::Error as _, Deserialize, Deserializer, Serialize, Serializer}; | ||
|
||
/// Deserialize string into T | ||
pub fn deserialize<'de, D, T>(deserializer: D) -> Result<T, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
T: std::str::FromStr, | ||
<T as std::str::FromStr>::Err: std::fmt::Display, | ||
{ | ||
String::deserialize(deserializer)? | ||
.parse::<T>() | ||
.map_err(|e| D::Error::custom(format!("{}", e))) | ||
} | ||
|
||
/// Serialize from T into string | ||
pub fn serialize<S, T>(value: &T, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
T: std::fmt::Display, | ||
{ | ||
format!("{}", value).serialize(serializer) | ||
} |
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,24 @@ | ||
//! Serialize/deserialize std::time::Duration type from and into string: | ||
use serde::{de::Error as _, Deserialize, Deserializer, Serialize, Serializer}; | ||
|
||
use std::time::Duration; | ||
|
||
/// Deserialize string into Duration | ||
pub fn deserialize<'de, D>(deserializer: D) -> Result<Duration, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
let value = String::deserialize(deserializer)? | ||
.parse::<u64>() | ||
.map_err(|e| D::Error::custom(format!("{}", e)))?; | ||
|
||
Ok(Duration::from_nanos(value)) | ||
} | ||
|
||
/// Serialize from Duration into string | ||
pub fn serialize<S>(value: &Duration, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
format!("{}", value.as_nanos()).serialize(serializer) | ||
} |
Oops, something went wrong.