2019-01-07 11:33:07 +01:00
|
|
|
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity Ethereum.
|
2018-06-04 10:19:50 +02:00
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2018-06-04 10:19:50 +02:00
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is distributed in the hope that it will be useful,
|
2018-06-04 10:19:50 +02:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
2019-01-07 11:33:07 +01:00
|
|
|
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2016-03-24 01:25:59 +01:00
|
|
|
|
|
|
|
//! Deserializer of empty string values into optionals.
|
|
|
|
|
2017-02-13 16:38:47 +01:00
|
|
|
use std::fmt;
|
2016-03-24 01:25:59 +01:00
|
|
|
use std::marker::PhantomData;
|
2019-09-25 10:02:04 +02:00
|
|
|
|
|
|
|
use ethereum_types::U256;
|
2017-02-13 16:38:47 +01:00
|
|
|
use serde::{Deserialize, Deserializer};
|
2017-07-06 11:36:15 +02:00
|
|
|
use serde::de::{Error, Visitor, IntoDeserializer};
|
2016-03-24 01:25:59 +01:00
|
|
|
|
2019-09-25 10:02:04 +02:00
|
|
|
use crate::uint::Uint;
|
|
|
|
|
2016-03-24 01:25:59 +01:00
|
|
|
/// Deserializer of empty string values into optionals.
|
2017-04-12 13:33:49 +02:00
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
2016-03-24 01:25:59 +01:00
|
|
|
pub enum MaybeEmpty<T> {
|
|
|
|
/// Some.
|
|
|
|
Some(T),
|
|
|
|
/// None.
|
|
|
|
None,
|
|
|
|
}
|
|
|
|
|
2017-07-06 11:36:15 +02:00
|
|
|
impl<'a, T> Deserialize<'a> for MaybeEmpty<T> where T: Deserialize<'a> {
|
2017-02-13 16:38:47 +01:00
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
2019-09-25 10:02:04 +02:00
|
|
|
where D: Deserializer<'a>
|
|
|
|
{
|
2017-07-06 11:36:15 +02:00
|
|
|
deserializer.deserialize_any(MaybeEmptyVisitor::new())
|
2016-03-24 01:25:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct MaybeEmptyVisitor<T> {
|
|
|
|
_phantom: PhantomData<T>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> MaybeEmptyVisitor<T> {
|
|
|
|
fn new() -> Self {
|
|
|
|
MaybeEmptyVisitor {
|
|
|
|
_phantom: PhantomData
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-06 11:36:15 +02:00
|
|
|
impl<'a, T> Visitor<'a> for MaybeEmptyVisitor<T> where T: Deserialize<'a> {
|
2016-03-24 01:25:59 +01:00
|
|
|
type Value = MaybeEmpty<T>;
|
|
|
|
|
2017-02-13 16:38:47 +01:00
|
|
|
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(formatter, "an empty string or string-encoded type")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> where E: Error {
|
2016-03-24 01:25:59 +01:00
|
|
|
self.visit_string(value.to_owned())
|
|
|
|
}
|
|
|
|
|
2017-02-13 16:38:47 +01:00
|
|
|
fn visit_string<E>(self, value: String) -> Result<Self::Value, E> where E: Error {
|
2019-09-10 20:46:50 +02:00
|
|
|
if value.is_empty() {
|
|
|
|
Ok(MaybeEmpty::None)
|
|
|
|
} else {
|
|
|
|
T::deserialize(value.into_deserializer()).map(MaybeEmpty::Some)
|
2016-03-24 01:25:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Into<Option<T>> for MaybeEmpty<T> {
|
|
|
|
fn into(self) -> Option<T> {
|
|
|
|
match self {
|
|
|
|
MaybeEmpty::Some(s) => Some(s),
|
|
|
|
MaybeEmpty::None => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-25 10:02:04 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
impl From<Uint> for MaybeEmpty<Uint> {
|
|
|
|
fn from(uint: Uint) -> Self {
|
|
|
|
MaybeEmpty::Some(uint)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<MaybeEmpty<Uint>> for U256 {
|
|
|
|
fn from(maybe: MaybeEmpty<Uint>) -> U256 {
|
|
|
|
match maybe {
|
|
|
|
MaybeEmpty::Some(v) => v.0,
|
|
|
|
MaybeEmpty::None => U256::zero(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<MaybeEmpty<Uint>> for u64 {
|
|
|
|
fn from(maybe: MaybeEmpty<Uint>) -> u64 {
|
|
|
|
match maybe {
|
|
|
|
MaybeEmpty::Some(v) => v.0.low_u64(),
|
|
|
|
MaybeEmpty::None => 0u64,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for MaybeEmpty<Uint> {
|
|
|
|
fn default() -> Self {
|
|
|
|
MaybeEmpty::Some(Uint::default())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-24 01:25:59 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use std::str::FromStr;
|
2019-09-10 20:46:50 +02:00
|
|
|
use super::MaybeEmpty;
|
|
|
|
use crate::hash::H256;
|
2016-03-24 01:25:59 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn maybe_deserialization() {
|
|
|
|
let s = r#"["", "5a39ed1020c04d4d84539975b893a4e7c53eab6c2965db8bc3468093a31bc5ae"]"#;
|
|
|
|
let deserialized: Vec<MaybeEmpty<H256>> = serde_json::from_str(s).unwrap();
|
|
|
|
assert_eq!(deserialized, vec![
|
|
|
|
MaybeEmpty::None,
|
2018-01-10 13:35:18 +01:00
|
|
|
MaybeEmpty::Some(H256(ethereum_types::H256::from_str("5a39ed1020c04d4d84539975b893a4e7c53eab6c2965db8bc3468093a31bc5ae").unwrap()))
|
2016-03-24 01:25:59 +01:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|