2019-01-07 11:33:07 +01:00
|
|
|
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity Ethereum.
|
2017-02-13 16:38:47 +01:00
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2017-02-13 16:38:47 +01: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,
|
2017-02-13 16:38:47 +01: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/>.
|
2017-02-13 16:38:47 +01:00
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
use rustc_hex::{FromHex, FromHexError, ToHex};
|
|
|
|
use serde::{de::Error, Deserialize, Deserializer, Serialize, Serializer};
|
2016-09-28 15:47:52 +02:00
|
|
|
use std::{ops, str};
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub struct Bytes(Vec<u8>);
|
|
|
|
|
|
|
|
impl ops::Deref for Bytes {
|
2020-08-05 06:08:03 +02:00
|
|
|
type Target = [u8];
|
2016-09-28 15:47:52 +02:00
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
2016-09-28 15:47:52 +02:00
|
|
|
}
|
|
|
|
|
2017-07-06 11:36:15 +02:00
|
|
|
impl<'a> Deserialize<'a> for Bytes {
|
2020-08-05 06:08:03 +02:00
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
|
|
where
|
|
|
|
D: Deserializer<'a>,
|
|
|
|
{
|
|
|
|
let s = String::deserialize(deserializer)?;
|
|
|
|
let data = s
|
|
|
|
.from_hex()
|
|
|
|
.map_err(|e| Error::custom(format!("Invalid hex value {}", e)))?;
|
|
|
|
Ok(Bytes(data))
|
|
|
|
}
|
2016-09-28 15:47:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for Bytes {
|
2020-08-05 06:08:03 +02:00
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: Serializer,
|
|
|
|
{
|
|
|
|
serializer.serialize_str(&self.0.to_hex())
|
|
|
|
}
|
2016-09-28 15:47:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl str::FromStr for Bytes {
|
2020-08-05 06:08:03 +02:00
|
|
|
type Err = FromHexError;
|
2016-09-28 15:47:52 +02:00
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
s.from_hex().map(Bytes)
|
|
|
|
}
|
2016-09-28 15:47:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<&'static str> for Bytes {
|
2020-08-05 06:08:03 +02:00
|
|
|
fn from(s: &'static str) -> Self {
|
|
|
|
s.parse().expect(&format!(
|
|
|
|
"invalid string literal for {}: '{}'",
|
|
|
|
stringify!(Self),
|
|
|
|
s
|
|
|
|
))
|
|
|
|
}
|
2016-09-28 15:47:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Vec<u8>> for Bytes {
|
2020-08-05 06:08:03 +02:00
|
|
|
fn from(v: Vec<u8>) -> Self {
|
|
|
|
Bytes(v)
|
|
|
|
}
|
2016-09-28 15:47:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Bytes> for Vec<u8> {
|
2020-08-05 06:08:03 +02:00
|
|
|
fn from(b: Bytes) -> Self {
|
|
|
|
b.0
|
|
|
|
}
|
2016-09-28 15:47:52 +02:00
|
|
|
}
|