aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordiogo464 <[email protected]>2025-08-08 15:43:04 +0100
committerdiogo464 <[email protected]>2025-08-08 15:43:04 +0100
commit541e8e49d9925c12bf36daa32fd511f5540bb1c6 (patch)
tree7f318bffb3900446d705bf1b22d17f643e2cf2d9
parentc19ad59d10913c2bc026159bb6ea3740deac6cd7 (diff)
added display() and HexDisplay
-rw-r--r--src/lib.rs37
1 files changed, 32 insertions, 5 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 36790b7..ca878a4 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -3,9 +3,11 @@
3//! # Examples 3//! # Examples
4//!``` 4//!```
5//!let hex_string = "1a2b3c"; 5//!let hex_string = "1a2b3c";
6//!let data = rhex::decode_hex(&hex_string).unwrap(); 6//!let data = hex::decode_hex(&hex_string).unwrap();
7//!``` 7//!```
8 8
9use std::fmt::Write;
10
9#[derive(Debug)] 11#[derive(Debug)]
10///Possible errors that occur during decoding 12///Possible errors that occur during decoding
11pub enum HexDecodeError { 13pub enum HexDecodeError {
@@ -119,7 +121,7 @@ fn append_byte_hex_to_string(byte: u8, string: &mut String) {
119/// 121///
120///``` 122///```
121///let hex_string = "12ffbc"; 123///let hex_string = "12ffbc";
122///for byte in rhex::decode_hex_iter(&hex_string) { 124///for byte in hex::decode_hex_iter(&hex_string) {
123/// //do something 125/// //do something
124///} 126///}
125///``` 127///```
@@ -135,7 +137,7 @@ where
135/// 137///
136///``` 138///```
137///let data = &[0x11, 0xbc, 0x22]; 139///let data = &[0x11, 0xbc, 0x22];
138///for c in rhex::encode_hex_iter(&data) { 140///for c in hex::encode_hex_iter(&data) {
139/// //do something with c 141/// //do something with c
140///} 142///}
141///``` 143///```
@@ -150,7 +152,7 @@ where
150/// 152///
151///``` 153///```
152///let hex_string = "ffaa11"; 154///let hex_string = "ffaa11";
153///let data = rhex::decode_hex(&hex_string).unwrap(); 155///let data = hex::decode_hex(&hex_string).unwrap();
154///``` 156///```
155pub fn decode_hex<T: AsRef<[u8]>>(string: T) -> Result<Vec<u8>, HexDecodeError> { 157pub fn decode_hex<T: AsRef<[u8]>>(string: T) -> Result<Vec<u8>, HexDecodeError> {
156 let chars = string.as_ref(); 158 let chars = string.as_ref();
@@ -175,7 +177,7 @@ pub fn decode_hex<T: AsRef<[u8]>>(string: T) -> Result<Vec<u8>, HexDecodeError>
175/// 177///
176///``` 178///```
177///let data = &[0x11, 0x12, 0x13]; 179///let data = &[0x11, 0x12, 0x13];
178///let string = rhex::encode_hex(&data); 180///let string = hex::encode_hex(&data);
179///assert_eq!(string, "111213"); 181///assert_eq!(string, "111213");
180///``` 182///```
181pub fn encode_hex<T: AsRef<[u8]>>(data: T) -> String { 183pub fn encode_hex<T: AsRef<[u8]>>(data: T) -> String {
@@ -187,6 +189,24 @@ pub fn encode_hex<T: AsRef<[u8]>>(data: T) -> String {
187 string 189 string
188} 190}
189 191
192pub struct HexDisplay<T>(T);
193
194impl<T> std::fmt::Display for HexDisplay<T>
195where
196 T: AsRef<[u8]>,
197{
198 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
199 for c in encode_hex_iter(self.0.as_ref()) {
200 f.write_char(c)?;
201 }
202 Ok(())
203 }
204}
205
206pub fn display<T: AsRef<[u8]>>(data: T) -> impl std::fmt::Display {
207 HexDisplay(data)
208}
209
190#[cfg(test)] 210#[cfg(test)]
191mod tests { 211mod tests {
192 use super::*; 212 use super::*;
@@ -274,4 +294,11 @@ mod tests {
274 let string: String = encode_hex_iter(&data).collect(); 294 let string: String = encode_hex_iter(&data).collect();
275 assert_eq!(string, "ffaa1245"); 295 assert_eq!(string, "ffaa1245");
276 } 296 }
297
298 #[test]
299 fn hex_display() {
300 let data: &[u8; 5] = &[0x01, 0x10, 0xFA, 0x4A, 0x00];
301 let string = display(data).to_string();
302 assert_eq!(string, "0110fa4a00");
303 }
277} 304}