subxt_core/constants/
address.rs1use crate::dynamic::DecodedValueThunk;
8use crate::metadata::DecodeWithMetadata;
9use alloc::borrow::Cow;
10use alloc::string::String;
11use derive_where::derive_where;
12
13pub trait Address {
16 type Target: DecodeWithMetadata;
18
19 fn pallet_name(&self) -> &str;
21
22 fn constant_name(&self) -> &str;
24
25 fn validation_hash(&self) -> Option<[u8; 32]> {
29 None
30 }
31}
32
33#[derive_where(Clone, Debug, PartialOrd, Ord, PartialEq, Eq)]
35pub struct DefaultAddress<ReturnTy> {
36 pallet_name: Cow<'static, str>,
37 constant_name: Cow<'static, str>,
38 constant_hash: Option<[u8; 32]>,
39 _marker: core::marker::PhantomData<ReturnTy>,
40}
41
42pub type StaticAddress<ReturnTy> = DefaultAddress<ReturnTy>;
44pub type DynamicAddress = DefaultAddress<DecodedValueThunk>;
46
47impl<ReturnTy> DefaultAddress<ReturnTy> {
48 pub fn new(pallet_name: impl Into<String>, constant_name: impl Into<String>) -> Self {
50 Self {
51 pallet_name: Cow::Owned(pallet_name.into()),
52 constant_name: Cow::Owned(constant_name.into()),
53 constant_hash: None,
54 _marker: core::marker::PhantomData,
55 }
56 }
57
58 #[doc(hidden)]
61 pub fn new_static(
62 pallet_name: &'static str,
63 constant_name: &'static str,
64 hash: [u8; 32],
65 ) -> Self {
66 Self {
67 pallet_name: Cow::Borrowed(pallet_name),
68 constant_name: Cow::Borrowed(constant_name),
69 constant_hash: Some(hash),
70 _marker: core::marker::PhantomData,
71 }
72 }
73
74 pub fn unvalidated(self) -> Self {
76 Self {
77 pallet_name: self.pallet_name,
78 constant_name: self.constant_name,
79 constant_hash: None,
80 _marker: self._marker,
81 }
82 }
83}
84
85impl<ReturnTy: DecodeWithMetadata> Address for DefaultAddress<ReturnTy> {
86 type Target = ReturnTy;
87
88 fn pallet_name(&self) -> &str {
89 &self.pallet_name
90 }
91
92 fn constant_name(&self) -> &str {
93 &self.constant_name
94 }
95
96 fn validation_hash(&self) -> Option<[u8; 32]> {
97 self.constant_hash
98 }
99}
100
101pub fn dynamic(pallet_name: impl Into<String>, constant_name: impl Into<String>) -> DynamicAddress {
103 DynamicAddress::new(pallet_name, constant_name)
104}