Skip to content

Commit

Permalink
Rename HexVisitor to FromStrVisitor
Browse files Browse the repository at this point in the history
The visitor works with all types that implement `FromStr`. Whether or
not that ends up being hex encoding depends on the implementation
of `FromStr`.
  • Loading branch information
thomaseizinger committed Jan 12, 2021
1 parent 18890d3 commit e6e23e9
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ impl ::serde::Serialize for SecretKey {
impl<'de> ::serde::Deserialize<'de> for SecretKey {
fn deserialize<D: ::serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
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"
))
} else {
Expand Down Expand Up @@ -442,7 +442,7 @@ impl ::serde::Serialize for PublicKey {
impl<'de> ::serde::Deserialize<'de> for PublicKey {
fn deserialize<D: ::serde::Deserializer<'de>>(d: D) -> Result<PublicKey, D::Error> {
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"
))
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ impl ::serde::Serialize for Signature {
impl<'de> ::serde::Deserialize<'de> for Signature {
fn deserialize<D: ::serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
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"
))
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/schnorrsig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl ::serde::Serialize for Signature {
impl<'de> ::serde::Deserialize<'de> for Signature {
fn deserialize<D: ::serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
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"
))
} else {
Expand Down Expand Up @@ -417,7 +417,7 @@ impl ::serde::Serialize for PublicKey {
impl<'de> ::serde::Deserialize<'de> for PublicKey {
fn deserialize<D: ::serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
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"
))
} else {
Expand Down
9 changes: 5 additions & 4 deletions src/serde_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@ use core::marker::PhantomData;
use core::str::{self, FromStr};
use serde::de;

pub struct HexVisitor<T> {
/// A serde visitor that works for `T`s implementing `FromStr`.
pub struct FromStrVisitor<T> {
expectation: &'static str,
_pd: PhantomData<T>,
}

impl<T> HexVisitor<T> {
impl<T> FromStrVisitor<T> {
pub fn new(expectation: &'static str) -> Self {
HexVisitor {
FromStrVisitor {
expectation,
_pd: PhantomData,
}
}
}

impl<'de, T> de::Visitor<'de> for HexVisitor<T>
impl<'de, T> de::Visitor<'de> for FromStrVisitor<T>
where
T: FromStr,
<T as FromStr>::Err: fmt::Display,
Expand Down

0 comments on commit e6e23e9

Please sign in to comment.