1#![doc = include_str!("../README.md")]
2
3#[macro_use]
4mod macros;
5
6use paste::paste;
7use serde::{Deserialize, Serialize};
8use std::fmt;
9use std::ops::{Deref, DerefMut};
10
11#[derive(Serialize, Deserialize, Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Ord)]
13#[serde(transparent)]
14pub struct Hex<T> {
15 internal: T,
16}
17
18impl<T: Deref> Deref for Hex<T> {
19 type Target = T::Target;
20
21 fn deref(&self) -> &Self::Target {
22 &self.internal
23 }
24}
25
26impl<T: DerefMut> DerefMut for Hex<T> {
27 fn deref_mut(&mut self) -> &mut <Self as Deref>::Target {
28 &mut self.internal
29 }
30}
31
32impl<T: fmt::Display> fmt::Display for Hex<T> {
33 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34 write!(f, "{}", &self.internal)
35 }
36}
37
38macro_rules! impl_for_ty {
39 ($target_type: ty) => {
40 paste! {
41 mod [<hex_ $target_type _lower>] {
42 serde_hex_mod_with_target_type!($target_type, "{:x}");
43 }
44 use [<hex_ $target_type _lower>]::HexInternal as [<Hex $target_type:camel LowerInternal>];
45 #[doc = "`" $target_type "`"]
47 pub type [<Hex $target_type:camel Lower>] = Hex<[<Hex $target_type:camel LowerInternal>]>;
48 impl From<$target_type> for [<Hex $target_type:camel Lower>] {
49 fn from(value: $target_type) -> Self {
50 Self {
51 internal: [<Hex $target_type:camel LowerInternal>]::from(value)
52 }
53 }
54 }
55 mod [<hex_ $target_type _upper>] {
56 serde_hex_mod_with_target_type!($target_type, "{:X}");
57 }
58 use [<hex_ $target_type _upper>]::HexInternal as [<Hex $target_type:camel UpperInternal>];
59 #[doc = "`" $target_type "`"]
61 pub type [<Hex $target_type:camel Upper>] = Hex<[<Hex $target_type:camel UpperInternal>]>;
62 impl From<$target_type> for [<Hex $target_type:camel Upper>] {
63 fn from(value: $target_type) -> Self {
64 Self {
65 internal: [<Hex $target_type:camel UpperInternal>]::from(value)
66 }
67 }
68 }
69 mod [<hex_ $target_type _prefix_lower>] {
70 serde_hex_prefix_mod_with_target_type!($target_type, "{:#x}");
71 }
72 use [<hex_ $target_type _prefix_lower>]::HexInternal as [<Hex $target_type:camel PrefixLowerInternal>];
73 #[doc = "`" $target_type "`"]
75 pub type [<Hex $target_type:camel PrefixLower>] = Hex<[<Hex $target_type:camel PrefixLowerInternal>]>;
76 impl From<$target_type> for [<Hex $target_type:camel PrefixLower>] {
77 fn from(value: $target_type) -> Self {
78 Self {
79 internal: [<Hex $target_type:camel PrefixLowerInternal>]::from(value)
80 }
81 }
82 }
83 mod [<hex_ $target_type _prefix_upper>] {
84 serde_hex_prefix_mod_with_target_type!($target_type, "{:#X}");
85 }
86 use [<hex_ $target_type _prefix_upper>]::HexInternal as [<Hex $target_type:camel PrefixUpperInternal>];
87 #[doc = "`" $target_type "`"]
89 pub type [<Hex $target_type:camel PrefixUpper>] = Hex<[<Hex $target_type:camel PrefixUpperInternal>]>;
90 impl From<$target_type> for [<Hex $target_type:camel PrefixUpper>] {
91 fn from(value: $target_type) -> Self {
92 Self {
93 internal: [<Hex $target_type:camel PrefixUpperInternal>]::from(value)
94 }
95 }
96 }
97 }
98 };
99}
100impl_for_ty!(u8);
101impl_for_ty!(u16);
102impl_for_ty!(u32);
103impl_for_ty!(u64);
104impl_for_ty!(usize);