Skip to content

Commit

Permalink
Merge #166: Introduce crate level decoding functions
Browse files Browse the repository at this point in the history
97222f2 Introduce crate level decoding functions (Tobin C. Harding)

Pull request description:

  We would like to release a bare-bones 1.0 version of this crate that excludes the `FromHex` trait. So that users can still decode vectors and arrays without the trait introduced two new decoding functions.

  Intentionally do not use `decode` as the name incase we later want to add a more general `decode` function.

ACKs for top commit:
  Kixunil:
    ACK 97222f2
  apoelstra:
    ACK 97222f2; successfully ran local tests

Tree-SHA512: 64faf6f8b617a69c74e46cf6d967525ca691559aa7c2993025298fd8d6eb88c0058cdec09d395c1418dde6571ac9cbf0793a90688661b7a33afb6f316f66b20c
  • Loading branch information
apoelstra committed Mar 8, 2025
2 parents 55bbf0b + 97222f2 commit 6de170e
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ pub mod prelude {
pub use crate::{display::DisplayHex, parse::FromHex};
}

#[cfg(feature = "alloc")]
use alloc::vec::Vec;

pub(crate) use table::Table;

#[rustfmt::skip] // Keep public re-exports separate.
Expand All @@ -80,6 +83,23 @@ pub use self::{
parse::FromHex,
};

/// Decodes a hex string into a vector of bytes.
///
/// # Errors
///
/// Errors if `s` is not a valid hex string.
#[cfg(feature = "alloc")]
pub fn decode_vec(s: &str) -> Result<Vec<u8>, HexToBytesError> { Vec::from_hex(s) }

/// Decodes a hex string into an array of bytes.
///
/// # Errors
///
/// Errors if `s` is not a valid hex string or the correct length.
pub fn decode_array<const N: usize>(s: &str) -> Result<[u8; N], HexToArrayError> {
<[u8; N]>::from_hex(s)
}

/// Possible case of hex.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum Case {
Expand Down

0 comments on commit 6de170e

Please sign in to comment.