multiversx_sc_scenario/scenario/model/value/
address_key.rs1use multiversx_sc::{
2 chain_core::std::Bech32Address,
3 types::{Address, TestAddress, TestSCAddress},
4};
5
6use super::{value_from_slice, AddressValue};
7use crate::scenario_format::{
8 interpret_trait::{InterpretableFrom, InterpreterContext, IntoRaw},
9 value_interpreter::interpret_string,
10};
11use std::{cmp::Ordering, fmt};
12
13#[derive(Debug, Clone, Eq)]
14pub struct AddressKey {
15 pub value: Address,
16 pub original: String,
17}
18
19impl Default for AddressKey {
20 fn default() -> Self {
21 Self {
22 value: Address::zero(),
23 original: Default::default(),
24 }
25 }
26}
27
28impl AddressKey {
29 pub fn to_address(&self) -> Address {
30 self.value.clone()
31 }
32}
33
34impl Ord for AddressKey {
35 fn cmp(&self, other: &Self) -> Ordering {
36 self.original.cmp(&other.original)
37 }
38}
39
40impl PartialOrd for AddressKey {
41 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
42 Some(self.cmp(other))
43 }
44}
45
46impl PartialEq for AddressKey {
47 fn eq(&self, other: &Self) -> bool {
48 self.original == other.original
49 }
50}
51
52impl fmt::Display for AddressKey {
53 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54 self.original.fmt(f)
55 }
56}
57
58impl InterpretableFrom<&str> for AddressKey {
59 fn interpret_from(from: &str, context: &InterpreterContext) -> Self {
60 let bytes = interpret_string(from, context);
61 AddressKey {
62 value: value_from_slice(bytes.as_slice()),
63 original: from.to_string(),
64 }
65 }
66}
67
68impl InterpretableFrom<String> for AddressKey {
69 fn interpret_from(from: String, context: &InterpreterContext) -> Self {
70 AddressKey::interpret_from(from.as_str(), context)
71 }
72}
73
74impl IntoRaw<String> for AddressKey {
75 fn into_raw(self) -> String {
76 self.original
77 }
78}
79
80impl From<&str> for AddressKey {
81 fn from(from: &str) -> Self {
82 Self::interpret_from(from, &InterpreterContext::default())
83 }
84}
85
86impl From<String> for AddressKey {
87 fn from(from: String) -> Self {
88 Self::interpret_from(from, &InterpreterContext::default())
89 }
90}
91
92impl From<&AddressValue> for AddressKey {
93 fn from(from: &AddressValue) -> Self {
94 AddressKey {
95 value: from.to_address(),
96 original: from.original.to_concatenated_string(),
97 }
98 }
99}
100
101impl From<AddressValue> for AddressKey {
102 fn from(from: AddressValue) -> Self {
103 AddressKey::from(&from)
104 }
105}
106
107impl From<&Address> for AddressKey {
108 fn from(from: &Address) -> Self {
109 AddressKey {
110 value: from.clone(),
111 original: format!("0x{}", hex::encode(from)),
112 }
113 }
114}
115
116impl From<&Bech32Address> for AddressKey {
117 fn from(from: &Bech32Address) -> Self {
118 AddressKey {
119 value: from.to_address().clone(),
120 original: from.to_bech32_expr(),
121 }
122 }
123}
124
125impl From<Bech32Address> for AddressKey {
126 fn from(from: Bech32Address) -> Self {
127 AddressKey {
128 original: from.to_bech32_expr(),
129 value: from.into_address(),
130 }
131 }
132}
133
134impl From<TestAddress<'_>> for AddressKey {
135 fn from(from: TestAddress) -> Self {
136 AddressKey {
137 value: from.eval_to_array().into(),
138 original: from.eval_to_expr(),
139 }
140 }
141}
142
143impl From<TestSCAddress<'_>> for AddressKey {
144 fn from(from: TestSCAddress) -> Self {
145 AddressKey {
146 value: from.eval_to_array().into(),
147 original: from.eval_to_expr(),
148 }
149 }
150}