use crate::dynamic::DecodedValueThunk;
use crate::metadata::DecodeWithMetadata;
use derive_where::derive_where;
pub use crate::utils::Yes;
pub trait Address {
type Target: DecodeWithMetadata;
type IsDecodable;
fn name(&self) -> &str;
fn validation_hash(&self) -> Option<[u8; 32]> {
None
}
}
impl Address for str {
type Target = DecodedValueThunk;
type IsDecodable = Yes;
fn name(&self) -> &str {
self
}
}
#[derive_where(Clone, Debug, PartialOrd, Ord, PartialEq, Eq)]
pub struct StaticAddress<ReturnTy, IsDecodable> {
name: &'static str,
hash: Option<[u8; 32]>,
phantom: core::marker::PhantomData<(ReturnTy, IsDecodable)>,
}
impl<ReturnTy, IsDecodable> StaticAddress<ReturnTy, IsDecodable> {
#[doc(hidden)]
pub fn new_static(name: &'static str, hash: [u8; 32]) -> StaticAddress<ReturnTy, IsDecodable> {
StaticAddress::<ReturnTy, IsDecodable> {
name,
hash: Some(hash),
phantom: core::marker::PhantomData,
}
}
pub fn unvalidated(self) -> Self {
Self {
name: self.name,
hash: None,
phantom: self.phantom,
}
}
}
impl<R: DecodeWithMetadata, Y> Address for StaticAddress<R, Y> {
type Target = R;
type IsDecodable = Y;
fn name(&self) -> &str {
self.name
}
fn validation_hash(&self) -> Option<[u8; 32]> {
self.hash
}
}