multiversx_sc_scenario/scenario/model/value/
value_key_u64.rs

1use crate::scenario_format::{
2    interpret_trait::{InterpretableFrom, InterpreterContext, IntoRaw},
3    value_interpreter::interpret_string,
4};
5
6use num_bigint::BigUint;
7use num_traits::ToPrimitive;
8use std::{
9    cmp::{Ord, Ordering},
10    fmt,
11};
12
13/// Currently not used.
14#[derive(Clone, Debug)]
15pub struct U64Key {
16    pub value: u64,
17    pub original: String,
18}
19
20impl PartialEq for U64Key {
21    fn eq(&self, other: &Self) -> bool {
22        self.value == other.value
23    }
24}
25
26impl Eq for U64Key {}
27
28impl PartialOrd for U64Key {
29    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
30        Some(self.cmp(other))
31    }
32}
33
34impl Ord for U64Key {
35    fn cmp(&self, other: &Self) -> Ordering {
36        self.value.cmp(&other.value)
37    }
38}
39
40impl InterpretableFrom<&str> for U64Key {
41    fn interpret_from(from: &str, context: &InterpreterContext) -> Self {
42        let bytes = interpret_string(from, context);
43        let bu = BigUint::from_bytes_be(&bytes);
44        U64Key {
45            value: bu.to_u64().unwrap(),
46            original: from.to_string(),
47        }
48    }
49}
50
51impl IntoRaw<String> for U64Key {
52    fn into_raw(self) -> String {
53        self.original
54    }
55}
56
57impl From<u64> for U64Key {
58    fn from(from: u64) -> Self {
59        U64Key {
60            value: from,
61            original: from.to_string(),
62        }
63    }
64}
65
66impl From<u32> for U64Key {
67    fn from(from: u32) -> Self {
68        U64Key {
69            value: from as u64,
70            original: from.to_string(),
71        }
72    }
73}
74
75impl From<i32> for U64Key {
76    fn from(from: i32) -> Self {
77        assert!(from >= 0, "U64Key cannot be negative");
78        Self::from(from as u32)
79    }
80}
81
82impl From<&str> for U64Key {
83    fn from(from: &str) -> Self {
84        U64Key::interpret_from(from, &InterpreterContext::default())
85    }
86}
87
88impl fmt::Display for U64Key {
89    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
90        self.original.fmt(f)
91    }
92}