#![cfg_attr(not(feature = "std"), no_std)]
extern crate alloc;
#[cfg(feature = "std")]
extern crate std;
mod data;
mod error;
#[cfg(feature = "primitive-types")]
mod primitive_types;
use alloc::string::String;
pub use error::Error;
pub trait FromHexPrefixed: Sized {
fn from_hex_prefixed(hex: impl AsRef<str>) -> Result<Self, Error>;
}
pub trait ToHexPrefixed {
fn to_hex_prefixed(self) -> String;
}
pub fn decode<T: FromHexPrefixed>(hex: impl AsRef<str>) -> Result<T, Error> {
T::from_hex_prefixed(hex)
}
pub fn encode<T: ToHexPrefixed>(value: T) -> String {
ToHexPrefixed::to_hex_prefixed(value)
}
fn strip_prefix(hex: &str) -> Result<&str, Error> {
if let Some(hex) = hex.strip_prefix("0x") {
Ok(hex)
} else if hex.len() < 2 {
Err(Error::InvalidStringLength)
} else {
let mut chars = hex.chars();
let c0 = chars.next().unwrap();
let c1 = chars.next().unwrap();
Err(Error::InvalidPrefix { c0, c1 })
}
}