Skip to content

Commit

Permalink
WIP: feautre gate inner string error
Browse files Browse the repository at this point in the history
WIP because another solution is to depend on `bitcoin-internals` and use
`InputString`.
  • Loading branch information
tcharding committed Jan 9, 2024
1 parent 9c42bb6 commit 4668969
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use core::{fmt, str};

#[cfg(all(feature = "alloc", not(feature = "std")))]
use crate::alloc::vec::Vec;
use crate::alloc::{string::String, vec::Vec};
use crate::error::InvalidLengthError;
use crate::iter::HexToBytesIter;
use crate::write_err;
Expand All @@ -27,7 +27,10 @@ pub trait FromHex: Sized {
///
/// Accepts an input string either with `0x` prefix or without, if you require specific handling
/// of the prefix see [`from_hex_str_prefixed`] and [`from_hex_str_no_prefix`].
fn from_hex<S: AsRef<str> + Into<String>>(s: S) -> Result<Self, Self::Error> {
fn from_hex<
#[cfg(feature = "alloc")] S: AsRef<str> + Into<String>,
#[cfg(not(feature = "alloc"))] S: AsRef<str>,
>(s: S) -> Result<Self, Self::Error> {
if s.as_ref().starts_with("0x") {
Self::from_hex_str_no_prefix(s.as_ref().trim_start_matches("0x"))
} else {
Expand All @@ -38,11 +41,15 @@ pub trait FromHex: Sized {
/// Parses provided string as hex requiring 0x prefix.
///
/// This is intended for user-supplied inputs or already-existing protocols in which 0x prefix is used.
fn from_hex_str_prefixed<S: AsRef<str> + Into<String>>(
s: S,
) -> Result<Self, PrefixedError<Self::Error>> {
fn from_hex_str_prefixed<
#[cfg(feature = "alloc")] S: AsRef<str> + Into<String>,
#[cfg(not(feature = "alloc"))] S: AsRef<str>,
>(s: S) -> Result<Self, PrefixedError<Self::Error>> {
if !s.as_ref().starts_with("0x") {
Err(PrefixedError::MissingPrefix(s.into()))
#[cfg(feature = "alloc")]
return Err(PrefixedError::MissingPrefix(s.into()));
#[cfg(not(feature = "alloc"))]
return Err(PrefixedError::MissingPrefix);
} else {
Ok(Self::from_hex_str_no_prefix(s.as_ref().trim_start_matches("0x"))?)
}
Expand All @@ -52,7 +59,10 @@ pub trait FromHex: Sized {
///
/// This is not recommended for user-supplied inputs because of possible confusion with decimals.
/// It should be only used for existing protocols which always encode values as hex without 0x prefix.
fn from_hex_str_no_prefix<S: AsRef<str> + Into<String>>(s: S) -> Result<Self, Self::Error> {
fn from_hex_str_no_prefix<
#[cfg(feature = "alloc")] S: AsRef<str> + Into<String>,
#[cfg(not(feature = "alloc"))] S: AsRef<str>,
>(s: S) -> Result<Self, Self::Error> {
Self::from_byte_iter(HexToBytesIter::new(s.as_ref())?)
}
}
Expand Down Expand Up @@ -122,8 +132,11 @@ pub enum PrefixedError<E> {
/// The input was not a valid hex string, contains the error that occurred while parsing.
ParseHex(E),
/// The input is missing `0x` prefix, contains the invalid input.
// TODO: Use internals::InputString.
#[cfg(feature = "alloc")]
MissingPrefix(String),
/// The input is missing `0x` prefix.
#[cfg(not(feature = "alloc"))]
MissingPrefix,
}

impl<E> From<E> for PrefixedError<E> {
Expand All @@ -136,8 +149,11 @@ impl<E: fmt::Display> fmt::Display for PrefixedError<E> {

match *self {
ParseHex(ref e) => write_err!(f, "failed to parse hex string"; e),
#[cfg(feature = "alloc")]
MissingPrefix(ref value) =>
write_err!(f, "the input value `{}` is missing the `0x` prefix", value; self),
#[cfg(not(feature = "alloc"))]
MissingPrefix => write!(f, "input string is missing the `0x` prefix"),
}
}
}
Expand All @@ -152,7 +168,10 @@ where

match *self {
ParseHex(ref e) => Some(e),
#[cfg(feature = "alloc")]
MissingPrefix(_) => None,
#[cfg(not(feature = "alloc"))]
MissingPrefix => None,
}
}
}
Expand Down

0 comments on commit 4668969

Please sign in to comment.