bee/swarm/bytes.rs
1//! Hex parsing / display helpers shared by every typed-byte newtype.
2//! Mirrors bee-go's `pkg/swarm/bytes.go` `Bytes` base type, but in
3//! Rust we don't need a runtime base struct — instead we expose
4//! free helpers that the `define_typed_bytes!` macro (in
5//! `swarm::typed_bytes`) uses to build each newtype's `from_hex`,
6//! `to_hex`, and serde impls.
7
8use crate::swarm::errors::Error;
9
10/// Decode a hex string (with or without `0x` prefix).
11pub fn decode_hex(s: &str) -> Result<Vec<u8>, Error> {
12 let s = s.strip_prefix("0x").unwrap_or(s);
13 Ok(hex::decode(s)?)
14}
15
16/// Encode bytes as lowercase hex (no `0x` prefix), matching bee-js /
17/// bee-go wire format.
18pub fn encode_hex(b: &[u8]) -> String {
19 hex::encode(b)
20}