use alloy::primitives::{Address, FixedBytes};
use crate::allocation_id::AllocationId;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct CollectionId(FixedBytes<32>);
impl CollectionId {
pub const fn new(collection: FixedBytes<32>) -> Self {
CollectionId(collection)
}
pub fn into_inner(self) -> FixedBytes<32> {
self.0
}
pub fn as_address(&self) -> Address {
Address::from_slice(&self.0.as_slice()[12..])
}
}
impl std::fmt::Display for CollectionId {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::Display::fmt(&self.0, f)
}
}
impl std::fmt::Debug for CollectionId {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::Debug::fmt(&self.0, f)
}
}
impl std::fmt::LowerHex for CollectionId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::LowerHex::fmt(&self.0, f)
}
}
impl std::fmt::UpperHex for CollectionId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::UpperHex::fmt(&self.0, f)
}
}
impl From<FixedBytes<32>> for CollectionId {
fn from(collection: FixedBytes<32>) -> Self {
CollectionId(collection)
}
}
impl From<Address> for CollectionId {
fn from(address: Address) -> Self {
let mut buf = [0u8; 32];
buf[12..].copy_from_slice(address.as_slice());
CollectionId(FixedBytes::<32>::from(buf))
}
}
impl From<AllocationId> for CollectionId {
fn from(allocation: AllocationId) -> Self {
let mut buf = [0u8; 32];
buf[12..].copy_from_slice(allocation.as_slice());
CollectionId(FixedBytes::<32>::from(buf))
}
}
impl std::str::FromStr for CollectionId {
type Err = <FixedBytes<32> as std::str::FromStr>::Err;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let collection = std::str::FromStr::from_str(s)?;
Ok(CollectionId(collection))
}
}
impl PartialEq<FixedBytes<32>> for CollectionId {
fn eq(&self, other: &FixedBytes<32>) -> bool {
self.0.eq(other)
}
}
impl AsRef<FixedBytes<32>> for CollectionId {
fn as_ref(&self) -> &FixedBytes<32> {
&self.0
}
}
impl std::ops::Deref for CollectionId {
type Target = FixedBytes<32>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for CollectionId {
fn deserialize<D>(deserializer: D) -> Result<CollectionId, D::Error>
where
D: serde::Deserializer<'de>,
{
let collection = FixedBytes::<32>::deserialize(deserializer)?;
Ok(CollectionId(collection))
}
}
#[cfg(feature = "serde")]
impl serde::Serialize for CollectionId {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.0.serialize(serializer)
}
}
#[cfg(feature = "fake")]
impl fake::Dummy<fake::Faker> for CollectionId {
fn dummy_with_rng<R: fake::Rng + ?Sized>(_: &fake::Faker, rng: &mut R) -> Self {
use crate::fake_impl::alloy::Alloy;
Self(FixedBytes::<32>::dummy_with_rng(&Alloy, rng))
}
}
#[macro_export]
#[doc(hidden)]
macro_rules! __collection_id {
() => {
$crate::CollectionId::new($crate::alloy::primitives::FixedBytes::<32>::ZERO)
};
($value:tt) => {
$crate::CollectionId::new($crate::alloy::primitives::fixed_bytes!($value))
};
}