1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use super::{value_from_slice, AddressValue};
use crate::scenario_format::{
    interpret_trait::{InterpretableFrom, InterpreterContext, IntoRaw},
    value_interpreter::interpret_string,
};
use dharitri_sc::types::Address;
use std::{cmp::Ordering, fmt};

#[derive(Debug, Clone, Eq)]
pub struct AddressKey {
    pub value: Address,
    pub original: String,
}

impl Default for AddressKey {
    fn default() -> Self {
        Self {
            value: Address::zero(),
            original: Default::default(),
        }
    }
}

impl AddressKey {
    pub fn to_address(&self) -> Address {
        self.value.clone()
    }
}

impl Ord for AddressKey {
    fn cmp(&self, other: &Self) -> Ordering {
        self.original.cmp(&other.original)
    }
}

impl PartialOrd for AddressKey {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl PartialEq for AddressKey {
    fn eq(&self, other: &Self) -> bool {
        self.original == other.original
    }
}

impl fmt::Display for AddressKey {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.original.fmt(f)
    }
}

impl InterpretableFrom<&str> for AddressKey {
    fn interpret_from(from: &str, context: &InterpreterContext) -> Self {
        let bytes = interpret_string(from, context);
        AddressKey {
            value: value_from_slice(bytes.as_slice()),
            original: from.to_string(),
        }
    }
}

impl InterpretableFrom<String> for AddressKey {
    fn interpret_from(from: String, context: &InterpreterContext) -> Self {
        AddressKey::interpret_from(from.as_str(), context)
    }
}

impl IntoRaw<String> for AddressKey {
    fn into_raw(self) -> String {
        self.original
    }
}

impl From<&str> for AddressKey {
    fn from(from: &str) -> Self {
        Self::interpret_from(from, &InterpreterContext::default())
    }
}

impl From<String> for AddressKey {
    fn from(from: String) -> Self {
        Self::interpret_from(from, &InterpreterContext::default())
    }
}

impl From<&AddressValue> for AddressKey {
    fn from(from: &AddressValue) -> Self {
        AddressKey {
            value: from.to_address(),
            original: from.original.to_concatenated_string(),
        }
    }
}

impl From<AddressValue> for AddressKey {
    fn from(from: AddressValue) -> Self {
        AddressKey::from(&from)
    }
}

impl From<&Address> for AddressKey {
    fn from(from: &Address) -> Self {
        AddressKey {
            value: from.clone(),
            original: format!("0x{}", hex::encode(from)),
        }
    }
}