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