From c46a066827047cb411ee53f68bb0e8562cd08e2c Mon Sep 17 00:00:00 2001 From: Steffen Eiden Date: Mon, 26 Feb 2024 14:54:58 +0100 Subject: [PATCH] rust/utils: Add Hexslice Add a thin wrapper around [u8] to be able to represent an u8-slice as a hex-string for Display and Serialize. Acked-by: Qi Feng Huo Reviewed-by: Marc Hartmayer Signed-off-by: Steffen Eiden --- rust/Cargo.lock | 1 + rust/utils/Cargo.toml | 1 + rust/utils/src/hexslice.rs | 55 ++++++++++++++++++++++++++++++++++++++ rust/utils/src/lib.rs | 2 ++ 4 files changed, 59 insertions(+) create mode 100644 rust/utils/src/hexslice.rs diff --git a/rust/Cargo.lock b/rust/Cargo.lock index ef56d7da..475007c5 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -688,6 +688,7 @@ dependencies = [ "libc", "log", "pv", + "serde", ] [[package]] diff --git a/rust/utils/Cargo.toml b/rust/utils/Cargo.toml index 9ab410f1..216f63a4 100644 --- a/rust/utils/Cargo.toml +++ b/rust/utils/Cargo.toml @@ -9,3 +9,4 @@ clap = { version ="4.1", features = ["derive", "wrap_help"] } libc = "0.2.49" log = { version = "0.4.6", features = ["std", "release_max_level_debug"] } pv = { path = "../pv" } +serde = { version = "1.0.139"} diff --git a/rust/utils/src/hexslice.rs b/rust/utils/src/hexslice.rs new file mode 100644 index 00000000..58b60f82 --- /dev/null +++ b/rust/utils/src/hexslice.rs @@ -0,0 +1,55 @@ +// SPDX-License-Identifier: MIT +// +// Copyright IBM Corp. 2024 +use serde::Serialize; + +/// Displays/Serializes an u8-slice into a Hex-string +/// +/// Thin wrapper around an u8-slice. +#[derive(Debug)] +pub struct HexSlice<'a>(&'a [u8]); + +impl<'a> HexSlice<'a> { + /// Creates a [`HexSlice`] from the given value. + pub fn from(s: &'a T) -> Self + where + T: ?Sized + AsRef<[u8]> + 'a, + { + s.into() + } +} + +impl<'a, T> From<&'a T> for HexSlice<'a> +where + T: ?Sized + AsRef<[u8]> + 'a, +{ + fn from(value: &'a T) -> Self { + Self(value.as_ref()) + } +} +impl<'a> Serialize for HexSlice<'a> { + fn serialize(&self, serializer: S) -> std::result::Result + where + S: serde::Serializer, + { + serializer.serialize_str(&format!("{self:#}")) + } +} + +impl std::fmt::Display for HexSlice<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + if f.alternate() { + write!(f, "0x")?; + } + for byte in self.0 { + write!(f, "{:0>2x}", byte)?; + } + Ok(()) + } +} + +impl AsRef<[u8]> for HexSlice<'_> { + fn as_ref(&self) -> &[u8] { + self.0 + } +} diff --git a/rust/utils/src/lib.rs b/rust/utils/src/lib.rs index 83e8114f..6c39314d 100644 --- a/rust/utils/src/lib.rs +++ b/rust/utils/src/lib.rs @@ -4,6 +4,7 @@ //! //! Copyright IBM Corp. 2023, 2024 mod cli; +mod hexslice; mod log; mod tmpfile; @@ -11,6 +12,7 @@ pub use crate::cli::CertificateOptions; pub use crate::cli::{get_reader_from_cli_file_arg, get_writer_from_cli_file_arg}; pub use crate::cli::{print_cli_error, print_error}; pub use crate::cli::{STDIN, STDOUT}; +pub use crate::hexslice::HexSlice; pub use crate::log::PvLogger; pub use crate::tmpfile::TemporaryDirectory;