platform_value/
display.rs

1use crate::Value;
2use base64::prelude::BASE64_STANDARD;
3use base64::Engine;
4use std::fmt::{Display, Formatter};
5
6impl Display for Value {
7    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
8        f.write_str(&self.string_representation())
9    }
10}
11
12impl Value {
13    pub fn non_qualified_string_representation(&self) -> String {
14        match self {
15            Value::Bytes(bytes) => format!("bytes {}", hex::encode(bytes)),
16            Value::Float(float) => {
17                format!("{}", float)
18            }
19            Value::Text(text) => text.clone(),
20            Value::Bool(b) => {
21                format!("{}", b)
22            }
23            Value::Null => "Null".to_string(),
24            Value::Array(value) => {
25                let inner_values = value
26                    .iter()
27                    .map(|v| v.string_representation())
28                    .collect::<Vec<String>>()
29                    .join(", ");
30                format!("array of [{}]", inner_values)
31            }
32            Value::Map(map) => {
33                let inner_string = map
34                    .iter()
35                    .map(|(key, value)| format!("{key}: {value}"))
36                    .collect::<Vec<_>>()
37                    .join(",\n");
38                format!("Map {{ {} }}", inner_string)
39            }
40            Value::U128(i) => format!("{}", i),
41            Value::I128(i) => format!("{}", i),
42            Value::U64(i) => format!("{}", i),
43            Value::I64(i) => format!("{}", i),
44            Value::U32(i) => format!("{}", i),
45            Value::I32(i) => format!("{}", i),
46            Value::U16(i) => format!("{}", i),
47            Value::I16(i) => format!("{}", i),
48            Value::U8(i) => format!("{}", i),
49            Value::I8(i) => format!("{}", i),
50            Value::Bytes20(bytes20) => {
51                format!("bytes20 {}", BASE64_STANDARD.encode(bytes20.as_slice()))
52            }
53            Value::Bytes32(bytes32) => {
54                format!("bytes32 {}", BASE64_STANDARD.encode(bytes32.as_slice()))
55            }
56            Value::Bytes36(bytes36) => {
57                format!("bytes36 {}", BASE64_STANDARD.encode(bytes36.as_slice()))
58            }
59            Value::Identifier(identifier) => format!(
60                "identifier {}",
61                bs58::encode(identifier.as_slice()).into_string()
62            ),
63            Value::EnumU8(_) => "enum u8".to_string(),
64            Value::EnumString(_) => "enum string".to_string(),
65        }
66    }
67
68    fn string_representation(&self) -> String {
69        match self {
70            Value::Bytes(bytes) => format!("bytes {}", hex::encode(bytes)),
71            Value::Float(float) => {
72                format!("float {}", float)
73            }
74            Value::Text(text) => {
75                let len = text.len();
76                if len > 20 {
77                    let first_text = text.split_at(20).0.to_string();
78                    format!("string {}[...({})]", first_text, len)
79                } else {
80                    format!("string {}", text)
81                }
82            }
83            Value::Bool(b) => {
84                format!("bool {}", b)
85            }
86            Value::Null => "Null".to_string(),
87            Value::Array(value) => {
88                let inner_values = value
89                    .iter()
90                    .map(|v| v.string_representation())
91                    .collect::<Vec<String>>()
92                    .join(", ");
93                format!("array of [{}]", inner_values)
94            }
95            Value::Map(map) => {
96                let inner_string = map
97                    .iter()
98                    .map(|(key, value)| format!("{key}: {value}"))
99                    .collect::<Vec<_>>()
100                    .join(",\n");
101                format!("Map {{ {} }}", inner_string)
102            }
103            Value::U128(i) => format!("(u128){}", i),
104            Value::I128(i) => format!("(i128){}", i),
105            Value::U64(i) => format!("(u64){}", i),
106            Value::I64(i) => format!("(i64){}", i),
107            Value::U32(i) => format!("(u32){}", i),
108            Value::I32(i) => format!("(i32){}", i),
109            Value::U16(i) => format!("(u16){}", i),
110            Value::I16(i) => format!("(i16){}", i),
111            Value::U8(i) => format!("(u8){}", i),
112            Value::I8(i) => format!("(i8){}", i),
113            Value::Bytes20(bytes20) => {
114                format!("bytes20 {}", BASE64_STANDARD.encode(bytes20.as_slice()))
115            }
116            Value::Bytes32(bytes32) => {
117                format!("bytes32 {}", BASE64_STANDARD.encode(bytes32.as_slice()))
118            }
119            Value::Bytes36(bytes36) => {
120                format!("bytes36 {}", BASE64_STANDARD.encode(bytes36.as_slice()))
121            }
122            Value::Identifier(identifier) => format!(
123                "identifier {}",
124                bs58::encode(identifier.as_slice()).into_string()
125            ),
126            Value::EnumU8(_) => "enum u8".to_string(),
127            Value::EnumString(_) => "enum string".to_string(),
128        }
129    }
130}