Rename `HexVisitor` to `FromStrVisitor`

The visitor works with all types that implement `FromStr`. Whether or
not that ends up being hex encoding depends on the implementation
of `FromStr`.
This commit is contained in:
Thomas Eizinger 2021-01-13 09:54:33 +11:00
parent 18890d3b86
commit e6e23e9dd6
No known key found for this signature in database
GPG Key ID: 651AC83A6C6C8B96
4 changed files with 10 additions and 9 deletions

View File

@ -228,7 +228,7 @@ impl ::serde::Serialize for SecretKey {
impl<'de> ::serde::Deserialize<'de> for SecretKey { impl<'de> ::serde::Deserialize<'de> for SecretKey {
fn deserialize<D: ::serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> { fn deserialize<D: ::serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
if d.is_human_readable() { if d.is_human_readable() {
d.deserialize_str(super::serde_util::HexVisitor::new( d.deserialize_str(super::serde_util::FromStrVisitor::new(
"a hex string representing 32 byte SecretKey" "a hex string representing 32 byte SecretKey"
)) ))
} else { } else {
@ -442,7 +442,7 @@ impl ::serde::Serialize for PublicKey {
impl<'de> ::serde::Deserialize<'de> for PublicKey { impl<'de> ::serde::Deserialize<'de> for PublicKey {
fn deserialize<D: ::serde::Deserializer<'de>>(d: D) -> Result<PublicKey, D::Error> { fn deserialize<D: ::serde::Deserializer<'de>>(d: D) -> Result<PublicKey, D::Error> {
if d.is_human_readable() { if d.is_human_readable() {
d.deserialize_str(super::serde_util::HexVisitor::new( d.deserialize_str(super::serde_util::FromStrVisitor::new(
"an ASCII hex string representing a public key" "an ASCII hex string representing a public key"
)) ))
} else { } else {

View File

@ -444,7 +444,7 @@ impl ::serde::Serialize for Signature {
impl<'de> ::serde::Deserialize<'de> for Signature { impl<'de> ::serde::Deserialize<'de> for Signature {
fn deserialize<D: ::serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> { fn deserialize<D: ::serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
if d.is_human_readable() { if d.is_human_readable() {
d.deserialize_str(serde_util::HexVisitor::new( d.deserialize_str(serde_util::FromStrVisitor::new(
"a hex string representing a DER encoded Signature" "a hex string representing a DER encoded Signature"
)) ))
} else { } else {

View File

@ -34,7 +34,7 @@ impl ::serde::Serialize for Signature {
impl<'de> ::serde::Deserialize<'de> for Signature { impl<'de> ::serde::Deserialize<'de> for Signature {
fn deserialize<D: ::serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> { fn deserialize<D: ::serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
if d.is_human_readable() { if d.is_human_readable() {
d.deserialize_str(super::serde_util::HexVisitor::new( d.deserialize_str(super::serde_util::FromStrVisitor::new(
"a hex string representing 64 byte schnorr signature" "a hex string representing 64 byte schnorr signature"
)) ))
} else { } else {
@ -417,7 +417,7 @@ impl ::serde::Serialize for PublicKey {
impl<'de> ::serde::Deserialize<'de> for PublicKey { impl<'de> ::serde::Deserialize<'de> for PublicKey {
fn deserialize<D: ::serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> { fn deserialize<D: ::serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
if d.is_human_readable() { if d.is_human_readable() {
d.deserialize_str(super::serde_util::HexVisitor::new( d.deserialize_str(super::serde_util::FromStrVisitor::new(
"a hex string representing 32 byte schnorr public key" "a hex string representing 32 byte schnorr public key"
)) ))
} else { } else {

View File

@ -3,21 +3,22 @@ use core::marker::PhantomData;
use core::str::{self, FromStr}; use core::str::{self, FromStr};
use serde::de; use serde::de;
pub struct HexVisitor<T> { /// A serde visitor that works for `T`s implementing `FromStr`.
pub struct FromStrVisitor<T> {
expectation: &'static str, expectation: &'static str,
_pd: PhantomData<T>, _pd: PhantomData<T>,
} }
impl<T> HexVisitor<T> { impl<T> FromStrVisitor<T> {
pub fn new(expectation: &'static str) -> Self { pub fn new(expectation: &'static str) -> Self {
HexVisitor { FromStrVisitor {
expectation, expectation,
_pd: PhantomData, _pd: PhantomData,
} }
} }
} }
impl<'de, T> de::Visitor<'de> for HexVisitor<T> impl<'de, T> de::Visitor<'de> for FromStrVisitor<T>
where where
T: FromStr, T: FromStr,
<T as FromStr>::Err: fmt::Display, <T as FromStr>::Err: fmt::Display,