thegraph_core/allocation_id.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
use alloy::primitives::Address;
/// A unique identifier for an allocation: the allocation's Ethereum address.
///
/// This is a "new-type" wrapper around [`Address`] to provide type safety.
///
/// ## Formatting
///
/// The `AllocationId` type implements the following formatting traits:
///
/// - Use [`Display`] for formatting the `AllocationId` as an [EIP-55] checksum string.
/// - Use [`LowerHex`] (or [`UpperHex`]) for formatting the `AllocationId` as a hexadecimal string.
///
/// [EIP-55]: https://eips.ethereum.org/EIPS/eip-55
/// [`Display`]: struct.AllocationId.html#impl-Display-for-AllocationId
/// [`LowerHex`]: struct.AllocationId.html#impl-LowerHex-for-AllocationId
/// [`UpperHex`]: struct.AllocationId.html#impl-UpperHex-for-AllocationId
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct AllocationId(Address);
impl AllocationId {
/// Create a new [`AllocationId`].
pub const fn new(address: Address) -> Self {
AllocationId(address)
}
/// Return the internal representation.
pub fn into_inner(self) -> Address {
self.0
}
}
impl std::fmt::Display for AllocationId {
/// Formats the `AllocationId` using its [EIP-55](https://eips.ethereum.org/EIPS/eip-55)
/// checksum representation.
///
/// See [`LowerHex`] (and [`UpperHex`]) for formatting the `AllocationId` as a hexadecimal
/// string.
///
/// [`LowerHex`]: struct.AllocationId.html#impl-LowerHex-for-AllocationId
/// [`UpperHex`]: struct.AllocationId.html#impl-UpperHex-for-AllocationId
///
/// ```rust
/// use thegraph_core::{allocation_id, AllocationId};
///
/// const ID: AllocationId = allocation_id!("0002c67268fb8c8917f36f865a0cbdf5292fa68d");
///
/// // Note the uppercase and lowercase hex characters in the checksum
/// assert_eq!(format!("{}", ID), "0x0002c67268FB8C8917F36F865a0CbdF5292FA68d");
/// ```
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::Display::fmt(&self.0, f)
}
}
impl std::fmt::Debug for AllocationId {
/// Formats the `AllocationId` using its raw lower-case hexadecimal representation.
///
/// It is advised to use the [`LowerHex`] (and [`UpperHex`]) format trait implementation over
/// the [`Debug`](std::fmt::Debug) implementation to format the `AllocationId` as a lower-case
/// hexadecimal string.
///
/// This implementation matches `alloy_primitives::Address`'s `Debug` implementation.
///
/// [`LowerHex`]: struct.AllocationId.html#impl-LowerHex-for-AllocationId
/// [`UpperHex`]: struct.AllocationId.html#impl-UpperHex-for-AllocationId
///
/// ```rust
/// use thegraph_core::{allocation_id, AllocationId};
///
/// const ID: AllocationId = allocation_id!("0002c67268fb8c8917f36f865a0cbdf5292fa68d");
///
/// assert_eq!(format!("{:?}", ID), "0x0002c67268fb8c8917f36f865a0cbdf5292fa68d");
/// ```
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::Debug::fmt(&self.0, f)
}
}
impl std::fmt::LowerHex for AllocationId {
/// Lowercase hex representation of the `AllocationId`.
///
/// Note that the alternate flag, `#`, adds a `0x` in front of the output.
///
/// ```rust
/// use thegraph_core::{allocation_id, AllocationId};
///
/// const ID: AllocationId = allocation_id!("0002c67268fb8c8917f36f865a0cbdf5292fa68d");
///
/// // Lower hex
/// assert_eq!(format!("{:x}", ID), "0002c67268fb8c8917f36f865a0cbdf5292fa68d");
///
/// // Lower hex with alternate flag
/// assert_eq!(format!("{:#x}", ID), "0x0002c67268fb8c8917f36f865a0cbdf5292fa68d");
/// ```
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::LowerHex::fmt(&self.0, f)
}
}
impl std::fmt::UpperHex for AllocationId {
/// Uppercase hex representation of the `AllocationId`.
///
/// Note that the alternate flag, `#`, adds a `0x` in front of the output.
///
/// ```rust
/// use thegraph_core::{allocation_id, AllocationId};
///
/// const ID: AllocationId = allocation_id!("0002c67268fb8c8917f36f865a0cbdf5292fa68d");
///
/// // Upper hex
/// assert_eq!(format!("{:X}", ID), "0002C67268FB8C8917F36F865A0CBDF5292FA68D");
///
/// // Upper hex with alternate flag
/// assert_eq!(format!("{:#X}", ID), "0x0002C67268FB8C8917F36F865A0CBDF5292FA68D");
/// ```
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::UpperHex::fmt(&self.0, f)
}
}
impl From<Address> for AllocationId {
fn from(address: Address) -> Self {
AllocationId(address)
}
}
impl std::str::FromStr for AllocationId {
type Err = <Address as std::str::FromStr>::Err;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let address = std::str::FromStr::from_str(s)?;
Ok(AllocationId(address))
}
}
impl PartialEq<Address> for AllocationId {
fn eq(&self, other: &Address) -> bool {
self.0.eq(other)
}
}
impl AsRef<Address> for AllocationId {
fn as_ref(&self) -> &Address {
&self.0
}
}
impl std::ops::Deref for AllocationId {
type Target = Address;
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for AllocationId {
fn deserialize<D>(deserializer: D) -> Result<AllocationId, D::Error>
where
D: serde::Deserializer<'de>,
{
let address = Address::deserialize(deserializer)?;
Ok(AllocationId(address))
}
}
#[cfg(feature = "serde")]
impl serde::Serialize for AllocationId {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.0.serialize(serializer)
}
}
/// Converts a sequence of string literals containing hex-encoded data into a new [`AllocationId`]
/// at compile time.
///
/// To create an `AllocationId` from a string literal (no `0x` prefix) at compile time:
///
/// ```rust
/// use thegraph_core::{allocation_id, AllocationId};
///
/// const ALLOCATION_ID: AllocationId = allocation_id!("0002c67268fb8c8917f36f865a0cbdf5292fa68d");
/// ```
///
/// If no argument is provided, the macro will create an `AllocationId` with the zero address:
///
/// ```rust
/// use thegraph_core::{
/// alloy::primitives::Address,
/// allocation_id, AllocationId
/// };
///
/// const ALLOCATION_ID: AllocationId = allocation_id!();
///
/// assert_eq!(ALLOCATION_ID, Address::ZERO);
/// ```
#[macro_export]
#[doc(hidden)]
macro_rules! __allocation_id {
() => {
$crate::AllocationId::new($crate::alloy::primitives::Address::ZERO)
};
($value:tt) => {
$crate::AllocationId::new($crate::alloy::primitives::address!($value))
};
}