radix_clis/resim/
addressing.rs1use crate::prelude::*;
2
3#[derive(Debug, Clone, PartialEq, Eq)]
4pub enum AddressError {
5 InvalidAddress(String),
6}
7
8impl std::error::Error for AddressError {}
9
10impl fmt::Display for AddressError {
11 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
12 write!(f, "{:?}", self)
13 }
14}
15
16#[derive(Clone)]
17pub struct SimulatorPackageAddress(pub PackageAddress);
18
19impl From<SimulatorPackageAddress> for PackageAddress {
20 fn from(simulator_address: SimulatorPackageAddress) -> Self {
21 simulator_address.0
22 }
23}
24
25impl From<PackageAddress> for SimulatorPackageAddress {
26 fn from(address: PackageAddress) -> Self {
27 Self(address)
28 }
29}
30
31impl FromStr for SimulatorPackageAddress {
32 type Err = AddressError;
33
34 fn from_str(address: &str) -> Result<Self, Self::Err> {
35 PackageAddress::try_from_hex(address)
36 .or(PackageAddress::try_from_bech32(
37 &AddressBech32Decoder::for_simulator(),
38 address,
39 ))
40 .ok_or(AddressError::InvalidAddress(address.to_string()))
41 .map(Self)
42 }
43}
44
45impl fmt::Display for SimulatorPackageAddress {
46 fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
47 write!(
48 f,
49 "{}",
50 self.0.display(&AddressBech32Encoder::for_simulator())
51 )
52 }
53}
54
55impl fmt::Debug for SimulatorPackageAddress {
56 fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
57 write!(f, "{}", self)
58 }
59}
60
61#[derive(Clone)]
62pub struct SimulatorResourceAddress(pub ResourceAddress);
63
64impl From<SimulatorResourceAddress> for ResourceAddress {
65 fn from(simulator_address: SimulatorResourceAddress) -> Self {
66 simulator_address.0
67 }
68}
69
70impl From<ResourceAddress> for SimulatorResourceAddress {
71 fn from(address: ResourceAddress) -> Self {
72 Self(address)
73 }
74}
75
76impl FromStr for SimulatorResourceAddress {
77 type Err = AddressError;
78
79 fn from_str(address: &str) -> Result<Self, Self::Err> {
80 ResourceAddress::try_from_hex(address)
81 .or(ResourceAddress::try_from_bech32(
82 &AddressBech32Decoder::for_simulator(),
83 address,
84 ))
85 .ok_or(AddressError::InvalidAddress(address.to_string()))
86 .map(Self)
87 }
88}
89
90impl fmt::Display for SimulatorResourceAddress {
91 fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
92 write!(
93 f,
94 "{}",
95 self.0.display(&AddressBech32Encoder::for_simulator())
96 )
97 }
98}
99
100impl fmt::Debug for SimulatorResourceAddress {
101 fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
102 write!(f, "{}", self)
103 }
104}
105
106#[derive(Clone)]
107pub struct SimulatorComponentAddress(pub ComponentAddress);
108
109impl From<SimulatorComponentAddress> for ComponentAddress {
110 fn from(simulator_address: SimulatorComponentAddress) -> Self {
111 simulator_address.0
112 }
113}
114
115impl From<ComponentAddress> for SimulatorComponentAddress {
116 fn from(address: ComponentAddress) -> Self {
117 Self(address)
118 }
119}
120
121impl FromStr for SimulatorComponentAddress {
122 type Err = AddressError;
123
124 fn from_str(address: &str) -> Result<Self, Self::Err> {
125 ComponentAddress::try_from_hex(address)
126 .or(ComponentAddress::try_from_bech32(
127 &AddressBech32Decoder::for_simulator(),
128 address,
129 ))
130 .ok_or(AddressError::InvalidAddress(address.to_string()))
131 .map(Self)
132 }
133}
134
135impl fmt::Display for SimulatorComponentAddress {
136 fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
137 write!(
138 f,
139 "{}",
140 self.0.display(&AddressBech32Encoder::for_simulator())
141 )
142 }
143}
144
145impl fmt::Debug for SimulatorComponentAddress {
146 fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
147 write!(f, "{}", self)
148 }
149}
150
151#[derive(Clone)]
152pub struct SimulatorNonFungibleGlobalId(pub NonFungibleGlobalId);
153
154impl From<SimulatorNonFungibleGlobalId> for NonFungibleGlobalId {
155 fn from(global_id: SimulatorNonFungibleGlobalId) -> Self {
156 global_id.0
157 }
158}
159
160impl From<NonFungibleGlobalId> for SimulatorNonFungibleGlobalId {
161 fn from(address: NonFungibleGlobalId) -> Self {
162 Self(address)
163 }
164}
165
166impl FromStr for SimulatorNonFungibleGlobalId {
167 type Err = ParseNonFungibleGlobalIdError;
168
169 fn from_str(s: &str) -> Result<Self, Self::Err> {
170 let global_id = NonFungibleGlobalId::try_from_canonical_string(
171 &AddressBech32Decoder::for_simulator(),
172 s,
173 )?;
174 Ok(Self(global_id))
175 }
176}
177
178impl fmt::Display for SimulatorNonFungibleGlobalId {
179 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
180 write!(
181 f,
182 "{}",
183 self.0
184 .to_canonical_string(&AddressBech32Encoder::for_simulator())
185 )
186 }
187}
188
189impl fmt::Debug for SimulatorNonFungibleGlobalId {
190 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
191 write!(f, "{}", self)
192 }
193}
194
195#[derive(Clone)]
196pub enum SimulatorResourceOrNonFungibleGlobalId {
197 ResourceAddress(SimulatorResourceAddress),
198 NonFungibleGlobalId(SimulatorNonFungibleGlobalId),
199}
200
201impl From<SimulatorResourceAddress> for SimulatorResourceOrNonFungibleGlobalId {
202 fn from(address: SimulatorResourceAddress) -> Self {
203 Self::ResourceAddress(address)
204 }
205}
206
207impl From<SimulatorNonFungibleGlobalId> for SimulatorResourceOrNonFungibleGlobalId {
208 fn from(address: SimulatorNonFungibleGlobalId) -> Self {
209 Self::NonFungibleGlobalId(address)
210 }
211}
212
213impl From<SimulatorResourceOrNonFungibleGlobalId> for AccessRule {
214 fn from(address: SimulatorResourceOrNonFungibleGlobalId) -> Self {
215 match address {
216 SimulatorResourceOrNonFungibleGlobalId::ResourceAddress(resource_address) => {
217 rule!(require(resource_address.0))
218 }
219 SimulatorResourceOrNonFungibleGlobalId::NonFungibleGlobalId(non_fungible_global_id) => {
220 rule!(require(non_fungible_global_id.0))
221 }
222 }
223 }
224}
225
226impl FromStr for SimulatorResourceOrNonFungibleGlobalId {
227 type Err = ParseSimulatorResourceOrNonFungibleGlobalIdError;
228
229 fn from_str(address: &str) -> Result<Self, Self::Err> {
230 if address.contains(':') {
231 SimulatorNonFungibleGlobalId::from_str(address)
232 .map_err(ParseSimulatorResourceOrNonFungibleGlobalIdError::from)
233 .map(Self::NonFungibleGlobalId)
234 } else {
235 SimulatorResourceAddress::from_str(address)
236 .map_err(ParseSimulatorResourceOrNonFungibleGlobalIdError::from)
237 .map(Self::ResourceAddress)
238 }
239 }
240}
241
242impl fmt::Display for SimulatorResourceOrNonFungibleGlobalId {
243 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
244 match self {
245 Self::NonFungibleGlobalId(non_fungible_global_id) => {
246 Display::fmt(non_fungible_global_id, f)
247 }
248 Self::ResourceAddress(resource_address) => Display::fmt(resource_address, f),
249 }
250 }
251}
252
253impl fmt::Debug for SimulatorResourceOrNonFungibleGlobalId {
254 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
255 match self {
256 Self::NonFungibleGlobalId(non_fungible_global_id) => {
257 Debug::fmt(non_fungible_global_id, f)
258 }
259 Self::ResourceAddress(resource_address) => Debug::fmt(resource_address, f),
260 }
261 }
262}
263
264#[derive(Debug)]
265pub enum ParseSimulatorResourceOrNonFungibleGlobalIdError {
266 ParseNonFungibleGlobalIdError(ParseNonFungibleGlobalIdError),
267 ParseResourceAddressError(AddressError),
268}
269
270impl From<ParseNonFungibleGlobalIdError> for ParseSimulatorResourceOrNonFungibleGlobalIdError {
271 fn from(error: ParseNonFungibleGlobalIdError) -> Self {
272 Self::ParseNonFungibleGlobalIdError(error)
273 }
274}
275
276impl From<AddressError> for ParseSimulatorResourceOrNonFungibleGlobalIdError {
277 fn from(error: AddressError) -> Self {
278 Self::ParseResourceAddressError(error)
279 }
280}
281
282impl fmt::Display for ParseSimulatorResourceOrNonFungibleGlobalIdError {
283 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
284 write!(f, "{:?}", self)
285 }
286}
287
288impl std::error::Error for ParseSimulatorResourceOrNonFungibleGlobalIdError {}