platform_value/system_bytes.rs
1use base64::engine::{DecodePaddingMode, GeneralPurpose, GeneralPurposeConfig};
2use base64::{alphabet, Engine};
3
4use crate::{BinaryData, Bytes20, Bytes32, Bytes36, Error, Identifier, Value};
5
6pub const PADDING_INDIFFERENT: GeneralPurposeConfig = GeneralPurposeConfig::new()
7 .with_encode_padding(false)
8 .with_decode_padding_mode(DecodePaddingMode::Indifferent);
9
10impl Value {
11 /// If the `Value` is a `Bytes`, a `Text` using base 58 or Vector of `U8`, returns the
12 /// associated `Vec<u8>` data as `Ok`.
13 /// Returns `Err(Error::Structure("reason"))` otherwise.
14 ///
15 /// ```
16 /// # use platform_value::{Error, Value};
17 /// #
18 /// let value = Value::Bytes(vec![104, 101, 108, 108, 111]);
19 /// assert_eq!(value.into_identifier_bytes(), Ok(vec![104, 101, 108, 108, 111])); ///
20 ///
21 /// let value = Value::Text("a811".to_string());
22 /// assert_eq!(value.into_identifier_bytes(), Ok(vec![98, 155, 36]));
23 ///
24 /// let value = Value::Array(vec![Value::U8(104), Value::U8(101), Value::U8(108)]);
25 /// assert_eq!(value.into_identifier_bytes(), Ok(vec![104, 101, 108]));
26 ///
27 /// let value = Value::Identifier([5u8;32]);
28 /// assert_eq!(value.into_identifier_bytes(), Ok(vec![5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5]));
29 ///
30 /// let value = Value::Bool(true);
31 /// assert_eq!(value.into_identifier_bytes(), Err(Error::StructureError("value are not bytes, a string, or an array of values representing bytes".to_string())));
32 /// ```
33 pub fn into_identifier_bytes(self) -> Result<Vec<u8>, Error> {
34 match self {
35 Value::Text(text) => bs58::decode(text).into_vec().map_err(|_| {
36 Error::StructureError(
37 "value was a string, but could not be decoded from base 58".to_string(),
38 )
39 }),
40 Value::Array(array) => array
41 .into_iter()
42 .map(|byte| match byte {
43 Value::U8(value_as_u8) => Ok(value_as_u8),
44 _ => Err(Error::StructureError("not an array of bytes".to_string())),
45 })
46 .collect::<Result<Vec<u8>, Error>>(),
47 Value::Bytes(vec) => Ok(vec),
48 Value::Bytes32(bytes) => Ok(bytes.into()),
49 Value::Identifier(identifier) => Ok(Vec::from(identifier)),
50 _other => Err(Error::StructureError(
51 "value are not bytes, a string, or an array of values representing bytes"
52 .to_string(),
53 )),
54 }
55 }
56
57 /// If the `Value` is a ref to a `Bytes`, a `Text` using base 58 or Vector of `U8`, returns the
58 /// associated `Vec<u8>` data as `Ok`.
59 /// Returns `Err(Error::Structure("reason"))` otherwise.
60 ///
61 /// ```
62 /// # use platform_value::{Error, Value};
63 /// #
64 /// let value = Value::Bytes(vec![104, 101, 108, 108, 111]);
65 /// assert_eq!(value.to_identifier_bytes(), Ok(vec![104, 101, 108, 108, 111])); ///
66 ///
67 /// let value = Value::Text("a811".to_string());
68 /// assert_eq!(value.to_identifier_bytes(), Ok(vec![98, 155, 36]));
69 ///
70 /// let value = Value::Array(vec![Value::U8(104), Value::U8(101), Value::U8(108)]);
71 /// assert_eq!(value.to_identifier_bytes(), Ok(vec![104, 101, 108]));
72 ///
73 /// let value = Value::Identifier([5u8;32]);
74 /// assert_eq!(value.to_identifier_bytes(), Ok(vec![5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5]));
75 ///
76 /// let value = Value::Bool(true);
77 /// assert_eq!(value.to_identifier_bytes(), Err(Error::StructureError("value are not bytes, a string, or an array of values representing bytes".to_string())));
78 /// ```
79 pub fn to_identifier_bytes(&self) -> Result<Vec<u8>, Error> {
80 match self {
81 Value::Text(text) => bs58::decode(text).into_vec().map_err(|_| {
82 Error::StructureError(
83 "value was a string, but could not be decoded from base 58".to_string(),
84 )
85 }),
86 Value::Array(array) => array
87 .iter()
88 .map(|byte| match byte {
89 Value::U8(value_as_u8) => Ok(*value_as_u8),
90 _ => Err(Error::StructureError("not an array of bytes".to_string())),
91 })
92 .collect::<Result<Vec<u8>, Error>>(),
93 Value::Bytes(vec) => Ok(vec.clone()),
94 Value::Bytes32(vec) => Ok(vec.to_vec()),
95 Value::Identifier(identifier) => Ok(Vec::from(identifier.as_slice())),
96 _other => Err(Error::StructureError(
97 "value are not bytes, a string, or an array of values representing bytes"
98 .to_string(),
99 )),
100 }
101 }
102
103 /// If the `Value` is a `Bytes`, a `Text` using base 64 or Vector of `U8`, returns the
104 /// associated `Vec<u8>` data as `Ok`.
105 /// Returns `Err(Error::Structure("reason"))` otherwise.
106 ///
107 /// ```
108 /// # use platform_value::{Error, Value};
109 /// #
110 /// let value = Value::Bytes(vec![104, 101, 108, 108, 111]);
111 /// assert_eq!(value.into_binary_bytes(), Ok(vec![104, 101, 108, 108, 111])); ///
112 ///
113 /// let value = Value::Text("a811".to_string());
114 /// assert_eq!(value.into_binary_bytes(), Ok(vec![107, 205, 117]));
115 ///
116 /// let value = Value::Array(vec![Value::U8(104), Value::U8(101), Value::U8(108)]);
117 /// assert_eq!(value.into_binary_bytes(), Ok(vec![104, 101, 108]));
118 ///
119 /// let value = Value::Identifier([5u8;32]);
120 /// assert_eq!(value.into_binary_bytes(), Ok(vec![5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5]));
121 ///
122 /// let value = Value::Bool(true);
123 /// assert_eq!(value.into_binary_bytes(), Err(Error::StructureError("value are not bytes, a string, or an array of values representing bytes".to_string())));
124 /// ```
125 pub fn into_binary_bytes(self) -> Result<Vec<u8>, Error> {
126 match self {
127 Value::Text(text) => GeneralPurpose::new(&alphabet::STANDARD, PADDING_INDIFFERENT).decode(text).map_err(|e| {
128 Error::StructureError(
129 format!("value was a string, but could not be decoded from base 64 into binary bytes, error: {}", e),
130 )
131 }),
132 Value::Array(array) => array
133 .into_iter()
134 .map(|byte| match byte {
135 Value::U8(value_as_u8) => Ok(value_as_u8),
136 _ => Err(Error::StructureError("not an array of bytes".to_string())),
137 })
138 .collect::<Result<Vec<u8>, Error>>(),
139 Value::Bytes(vec) => Ok(vec),
140 Value::Bytes32(bytes) => Ok(bytes.into()),
141 Value::Identifier(identifier) => Ok(Vec::from(identifier)),
142 _other => Err(Error::StructureError(
143 "value are not bytes, a string, or an array of values representing bytes"
144 .to_string(),
145 )),
146 }
147 }
148
149 /// If the `Value` is a `Bytes`, a `Text` using base 64 or Vector of `U8`, returns the
150 /// associated `Vec<u8>` data as `Ok`.
151 /// Returns `Err(Error::Structure("reason"))` otherwise.
152 ///
153 /// ```
154 /// # use platform_value::{BinaryData, Error, Value};
155 /// #
156 /// let value = Value::Bytes(vec![104, 101, 108, 108, 111]);
157 /// assert_eq!(value.into_binary_data(), Ok(BinaryData::new(vec![104, 101, 108, 108, 111]))); ///
158 ///
159 /// let value = Value::Text("a811".to_string());
160 /// assert_eq!(value.into_binary_data(), Ok(BinaryData::new(vec![107, 205, 117])));
161 ///
162 /// let value = Value::Array(vec![Value::U8(104), Value::U8(101), Value::U8(108)]);
163 /// assert_eq!(value.into_binary_data(), Ok(BinaryData::new(vec![104, 101, 108])));
164 ///
165 /// let value = Value::Identifier([5u8;32]);
166 /// assert_eq!(value.into_binary_data(), Ok(BinaryData::new(vec![5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5])));
167 ///
168 /// let value = Value::Bool(true);
169 /// assert_eq!(value.into_binary_data(), Err(Error::StructureError("value are not bytes, a string, or an array of values representing bytes".to_string())));
170 /// ```
171 pub fn into_binary_data(self) -> Result<BinaryData, Error> {
172 Ok(BinaryData::new(self.into_binary_bytes()?))
173 }
174
175 /// If the `Value` is a ref to a `Bytes`, a `Text` using base 58 or Vector of `U8`, returns the
176 /// associated `Vec<u8>` data as `Ok`.
177 /// Returns `Err(Error::Structure("reason"))` otherwise.
178 ///
179 /// ```
180 /// # use platform_value::{Error, Value};
181 /// #
182 /// let value = Value::Bytes(vec![104, 101, 108, 108, 111]);
183 /// assert_eq!(value.to_binary_bytes(), Ok(vec![104, 101, 108, 108, 111])); ///
184 ///
185 /// let value = Value::Text("a811".to_string());
186 /// assert_eq!(value.to_binary_bytes(), Ok(vec![107, 205, 117]));
187 ///
188 /// let value = Value::Array(vec![Value::U8(104), Value::U8(101), Value::U8(108)]);
189 /// assert_eq!(value.to_binary_bytes(), Ok(vec![104, 101, 108]));
190 ///
191 /// let value = Value::Identifier([5u8;32]);
192 /// assert_eq!(value.to_binary_bytes(), Ok(vec![5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5]));
193 ///
194 /// let value = Value::Bool(true);
195 /// assert_eq!(value.to_binary_bytes(), Err(Error::StructureError("value are not bytes, a string, or an array of values representing bytes".to_string())));
196 /// ```
197 pub fn to_binary_bytes(&self) -> Result<Vec<u8>, Error> {
198 match self {
199 Value::Text(text) => GeneralPurpose::new(&alphabet::STANDARD, PADDING_INDIFFERENT)
200 .decode(text)
201 .map_err(|e| {
202 Error::StructureError(format!(
203 "value was a string, but could not be decoded from base 64, error: {}",
204 e
205 ))
206 }),
207 Value::Array(array) => array
208 .iter()
209 .map(|byte| match byte {
210 Value::U8(value_as_u8) => Ok(*value_as_u8),
211 _ => Err(Error::StructureError("not an array of bytes".to_string())),
212 })
213 .collect::<Result<Vec<u8>, Error>>(),
214 Value::Bytes(vec) => Ok(vec.clone()),
215 Value::Bytes20(vec) => Ok(vec.to_vec()),
216 Value::Bytes32(vec) => Ok(vec.to_vec()),
217 Value::Bytes36(vec) => Ok(vec.to_vec()),
218 Value::Identifier(identifier) => Ok(Vec::from(identifier.as_slice())),
219 _other => Err(Error::StructureError(
220 "value are not bytes, a string, or an array of values representing bytes"
221 .to_string(),
222 )),
223 }
224 }
225
226 /// If the `Value` is a `Bytes`, a `Text` using base 58 or Vector of `U8`, returns the
227 /// associated `Vec<u8>` data as `Ok`.
228 /// Returns `Err(Error::Structure("reason"))` otherwise.
229 ///
230 /// ```
231 /// # use platform_value::{Error, Value};
232 /// #
233 /// let value = Value::Bytes(vec![104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50]);
234 /// assert_eq!(value.into_hash256(), Ok([104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50])); ///
235 ///
236 /// let value = Value::Text("6oFRdsUNiAtXscRn52atKYCiF8RBnH9vbUzhtzY3d83e".to_string());
237 /// assert_eq!(value.into_hash256(), Ok([86, 35, 118, 67, 167, 43, 101, 109, 72, 97, 35, 99, 0, 254, 108, 154, 254, 154, 190, 40, 237, 25, 58, 246, 111, 19, 44, 215, 141, 140, 156, 117]));
238 ///
239 /// let value = Value::Text("a811".to_string());
240 /// assert_eq!(value.into_hash256(), Err(Error::StructureError("value was a string, could be decoded from base 58, but was not 32 bytes long".to_string())));
241 ///
242 /// let value = Value::Text("a811Ii".to_string());
243 /// assert_eq!(value.into_hash256(), Err(Error::StructureError("value was a string, but could not be decoded from base 58".to_string())));
244 ///
245 /// let value = Value::Array(vec![Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101)]);
246 /// assert_eq!(value.into_hash256(), Ok([104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101]));
247 ///
248 /// let value = Value::Identifier([5u8;32]);
249 /// assert_eq!(value.into_hash256(), Ok([5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5]));
250 ///
251 /// let value = Value::Bool(true);
252 /// assert_eq!(value.into_hash256(), Err(Error::StructureError("value are not bytes, a string, or an array of values representing bytes".to_string())));
253 /// ```
254 pub fn into_hash256(self) -> Result<[u8; 32], Error> {
255 match self {
256 Value::Text(text) => {
257 bs58::decode(text).into_vec()
258 .map_err(|_| Error::StructureError("value was a string, but could not be decoded from base 58".to_string()))?
259 .try_into()
260 .map_err(|_| Error::StructureError("value was a string, could be decoded from base 58, but was not 32 bytes long".to_string()))
261 }
262 Value::Array(array) => {
263 Ok(array
264 .into_iter()
265 .map(|byte| match byte {
266 Value::U8(value_as_u8) => {
267 Ok(value_as_u8)
268 }
269 _ => Err(Error::StructureError("not an array of bytes".to_string())),
270 })
271 .collect::<Result<Vec<u8>, Error>>()?
272 .try_into()
273 .map_err(|_| Error::StructureError("value was an array of bytes, but was not 32 bytes long".to_string()))?)
274 }
275 Value::Bytes(vec) => {
276 vec.try_into()
277 .map_err(|_| Error::StructureError("value was bytes, but was not 32 bytes long".to_string()))
278 },
279 Value::Bytes32(bytes) => Ok(bytes),
280 Value::Identifier(identifier) => Ok(identifier),
281 _other => Err(Error::StructureError("value are not bytes, a string, or an array of values representing bytes".to_string())),
282 }
283 }
284
285 /// If the `Value` is a `Bytes`, a `Text` using base 58 or Vector of `U8`, returns the
286 /// associated `Vec<u8>` data as `Ok`.
287 /// Returns `Err(Error::Structure("reason"))` otherwise.
288 ///
289 /// ```
290 /// # use platform_value::{Error, Value};
291 /// #
292 /// let value = Value::Bytes(vec![104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50]);
293 /// assert_eq!(value.to_hash256(), Ok([104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50])); ///
294 ///
295 /// let value = Value::Text("6oFRdsUNiAtXscRn52atKYCiF8RBnH9vbUzhtzY3d83e".to_string());
296 /// assert_eq!(value.to_hash256(), Ok([86, 35, 118, 67, 167, 43, 101, 109, 72, 97, 35, 99, 0, 254, 108, 154, 254, 154, 190, 40, 237, 25, 58, 246, 111, 19, 44, 215, 141, 140, 156, 117]));
297 ///
298 /// let value = Value::Text("a811".to_string());
299 /// assert_eq!(value.to_hash256(), Err(Error::StructureError("value was a string, could be decoded from base 58, but was not 32 bytes long".to_string())));
300 ///
301 /// let value = Value::Text("a811Ii".to_string());
302 /// assert_eq!(value.to_hash256(), Err(Error::StructureError("value was a string, but could not be decoded from base 58".to_string())));
303 ///
304 /// let value = Value::Array(vec![Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101)]);
305 /// assert_eq!(value.to_hash256(), Ok([104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101]));
306 ///
307 /// let value = Value::Identifier([5u8;32]);
308 /// assert_eq!(value.to_hash256(), Ok([5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5]));
309 ///
310 /// let value = Value::Bool(true);
311 /// assert_eq!(value.to_hash256(), Err(Error::StructureError("value are not bytes, a string, or an array of values representing bytes".to_string())));
312 /// ```
313 pub fn to_hash256(&self) -> Result<[u8; 32], Error> {
314 match self {
315 Value::Text(text) => {
316 bs58::decode(text).into_vec()
317 .map_err(|_| Error::StructureError("value was a string, but could not be decoded from base 58".to_string()))?
318 .try_into()
319 .map_err(|_| Error::StructureError("value was a string, could be decoded from base 58, but was not 32 bytes long".to_string()))
320 },
321 Value::Array(array) => {
322 Ok(array
323 .iter()
324 .map(|byte| byte.to_integer())
325 .collect::<Result<Vec<u8>, Error>>()?
326 .try_into()
327 .map_err(|_| Error::StructureError("value was an array of bytes, but was not 32 bytes long".to_string()))?)
328 },
329 Value::Bytes32(bytes) => Ok(*bytes),
330 Value::Bytes(vec) => {
331 vec.clone().try_into()
332 .map_err(|_| Error::StructureError("value was bytes, but was not 32 bytes long".to_string()))
333 },
334 Value::Identifier(identifier) => Ok(identifier.to_owned()),
335 _other => Err(Error::StructureError("value are not bytes, a string, or an array of values representing bytes".to_string())),
336 }
337 }
338
339 /// If the `Value` is a `Bytes`, a `Text` using base 64 or Vector of `U8`, returns the
340 /// associated `Bytes20` data as `Ok`.
341 /// Returns `Err(Error::Structure("reason"))` otherwise.
342 ///
343 /// ```
344 /// # use platform_value::{Bytes20, Error, Value};
345 ///
346 /// #
347 /// let value = Value::Bytes(vec![104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108]);
348 /// assert_eq!(value.into_bytes_20(), Ok(Bytes20([104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108]))); ///
349 ///
350 /// let value = Value::Text("WRdZY72e8yUfNJ5VC9ckzga7ysE=".to_string());
351 /// assert_eq!(value.into_bytes_20(), Ok(Bytes20([89, 23, 89, 99, 189, 158, 243, 37, 31, 52, 158, 85, 11, 215, 36, 206, 6, 187, 202, 193])));
352 ///
353 /// let value = Value::Text("a811".to_string());
354 /// assert_eq!(value.into_bytes_20(), Err(Error::ByteLengthNot20BytesError("buffer was not 20 bytes long".to_string())));
355 ///
356 /// let value = Value::Text("a811Ii".to_string());
357 /// assert_eq!(value.into_bytes_20(), Err(Error::StructureError("value was a string, but could not be decoded from base 64 into bytes 20, error: Invalid last symbol 105, offset 5.".to_string())));
358 ///
359 /// let value = Value::Array(vec![Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104)]);
360 /// assert_eq!(value.into_bytes_20(), Ok(Bytes20([104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104])));
361 ///
362 /// let value = Value::Bool(true);
363 /// assert_eq!(value.into_bytes_20(), Err(Error::StructureError("value are not bytes, a string, or an array of values representing bytes".to_string())));
364 /// ```
365 pub fn into_bytes_20(self) -> Result<Bytes20, Error> {
366 match self {
367 Value::Text(text) => Bytes20::from_vec(GeneralPurpose::new(&alphabet::STANDARD, PADDING_INDIFFERENT).decode(text).map_err(|e| {
368 Error::StructureError(
369 format!("value was a string, but could not be decoded from base 64 into bytes 20, error: {}", e),
370 )
371 })?),
372 Value::Array(array) => Bytes20::from_vec(
373 array
374 .into_iter()
375 .map(|byte| byte.into_integer())
376 .collect::<Result<Vec<u8>, Error>>()?,
377 ),
378 Value::Bytes20(bytes) => Ok(Bytes20::new(bytes)),
379 Value::Bytes(vec) => Bytes20::from_vec(vec),
380 _other => Err(Error::StructureError(
381 "value are not bytes, a string, or an array of values representing bytes"
382 .to_string(),
383 )),
384 }
385 }
386
387 /// If the `Value` is a `Bytes`, a `Text` using base 64 or Vector of `U8`, returns the
388 /// associated `Bytes20` data as `Ok`.
389 /// Returns `Err(Error::Structure("reason"))` otherwise.
390 ///
391 /// ```
392 /// # use platform_value::{Bytes20, Error, Value};
393 ///
394 /// #
395 /// let value = Value::Bytes(vec![104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108]);
396 /// assert_eq!(value.to_bytes_20(), Ok(Bytes20([104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108]))); ///
397 ///
398 /// let value = Value::Text("WRdZY72e8yUfNJ5VC9ckzga7ysE=".to_string());
399 /// assert_eq!(value.to_bytes_20(), Ok(Bytes20([89, 23, 89, 99, 189, 158, 243, 37, 31, 52, 158, 85, 11, 215, 36, 206, 6, 187, 202, 193])));
400 ///
401 /// let value = Value::Text("a811".to_string());
402 /// assert_eq!(value.to_bytes_20(), Err(Error::ByteLengthNot20BytesError("buffer was not 20 bytes long".to_string())));
403 ///
404 /// let value = Value::Text("a811Ii".to_string());
405 /// assert_eq!(value.to_bytes_20(), Err(Error::StructureError("value was a string, but could not be decoded from base 64 to bytes 20, error: Invalid last symbol 105, offset 5.".to_string())));
406 ///
407 /// let value = Value::Array(vec![Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104)]);
408 /// assert_eq!(value.to_bytes_20(), Ok(Bytes20([104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104])));
409 ///
410 /// let value = Value::Bool(true);
411 /// assert_eq!(value.to_bytes_20(), Err(Error::StructureError("value are not bytes, a string, or an array of values representing bytes".to_string())));
412 /// ```
413 pub fn to_bytes_20(&self) -> Result<Bytes20, Error> {
414 match self {
415 Value::Text(text) => Bytes20::from_vec(GeneralPurpose::new(&alphabet::STANDARD, PADDING_INDIFFERENT).decode(text).map_err(|e| {
416 Error::StructureError(
417 format!("value was a string, but could not be decoded from base 64 to bytes 20, error: {}", e),
418 )
419 })?),
420 Value::Array(array) => Bytes20::from_vec(
421 array
422 .iter()
423 .map(|byte| byte.to_integer())
424 .collect::<Result<Vec<u8>, Error>>()?,
425 ),
426 Value::Bytes20(bytes) => Ok(Bytes20::new(*bytes)),
427 Value::Bytes(vec) => Bytes20::from_vec(vec.clone()),
428 _other => Err(Error::StructureError(
429 "value are not bytes, a string, or an array of values representing bytes"
430 .to_string(),
431 )),
432 }
433 }
434
435 /// If the `Value` is a `Bytes`, a `Text` using base 64 or Vector of `U8`, returns the
436 /// associated `Bytes32` data as `Ok`.
437 /// Returns `Err(Error::Structure("reason"))` otherwise.
438 ///
439 /// ```
440 /// # use platform_value::{Bytes32, Error, Value};
441 ///
442 /// #
443 /// let value = Value::Bytes(vec![104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50]);
444 /// assert_eq!(value.into_bytes_32(), Ok(Bytes32([104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50]))); ///
445 ///
446 /// let value = Value::Text("ViN2Q6crZW1IYSNjAP5smv6avijtGTr2bxMs142MnHU=".to_string());
447 /// assert_eq!(value.into_bytes_32(), Ok(Bytes32([86, 35, 118, 67, 167, 43, 101, 109, 72, 97, 35, 99, 0, 254, 108, 154, 254, 154, 190, 40, 237, 25, 58, 246, 111, 19, 44, 215, 141, 140, 156, 117])));
448 ///
449 /// let value = Value::Text("a811".to_string());
450 /// assert_eq!(value.into_bytes_32(), Err(Error::ByteLengthNot32BytesError("buffer was not 32 bytes long".to_string())));
451 ///
452 /// let value = Value::Text("a811Ii".to_string());
453 /// assert_eq!(value.into_bytes_32(), Err(Error::StructureError("value was a string, but could not be decoded from base 64 into bytes 32, error: Invalid last symbol 105, offset 5.".to_string())));
454 ///
455 /// let value = Value::Array(vec![Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101)]);
456 /// assert_eq!(value.into_bytes_32(), Ok(Bytes32([104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101])));
457 ///
458 /// let value = Value::Identifier([5u8;32]);
459 /// assert_eq!(value.into_bytes_32(), Ok(Bytes32([5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5])));
460 ///
461 /// let value = Value::Bool(true);
462 /// assert_eq!(value.into_bytes_32(), Err(Error::StructureError("value are not bytes, a string, or an array of values representing bytes".to_string())));
463 /// ```
464 pub fn into_bytes_32(self) -> Result<Bytes32, Error> {
465 match self {
466 Value::Text(text) => Bytes32::from_vec(GeneralPurpose::new(&alphabet::STANDARD, PADDING_INDIFFERENT).decode(text).map_err(|e| {
467 Error::StructureError(
468 format!("value was a string, but could not be decoded from base 64 into bytes 32, error: {}", e),
469 )
470 })?),
471 Value::Array(array) => Bytes32::from_vec(
472 array
473 .into_iter()
474 .map(|byte| byte.into_integer())
475 .collect::<Result<Vec<u8>, Error>>()?,
476 ),
477 Value::Bytes32(bytes) => Ok(Bytes32::new(bytes)),
478 Value::Bytes(vec) => Bytes32::from_vec(vec),
479 Value::Identifier(identifier) => Ok(Bytes32::new(identifier)),
480 _other => Err(Error::StructureError(
481 "value are not bytes, a string, or an array of values representing bytes"
482 .to_string(),
483 )),
484 }
485 }
486
487 /// If the `Value` is a `Bytes`, a `Text` using base 58 or Vector of `U8`, returns the
488 /// associated `Vec<u8>` data as `Ok`.
489 /// Returns `Err(Error::Structure("reason"))` otherwise.
490 ///
491 /// ```
492 /// # use platform_value::{Bytes32, Error, Value};
493 /// #
494 /// let value = Value::Bytes(vec![104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50]);
495 /// assert_eq!(value.to_bytes_32(), Ok(Bytes32::new([104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50]))); ///
496 ///
497 /// let value = Value::Text("ViN2Q6crZW1IYSNjAP5smv6avijtGTr2bxMs142MnHU=".to_string());
498 /// assert_eq!(value.to_bytes_32(), Ok(Bytes32::new([86, 35, 118, 67, 167, 43, 101, 109, 72, 97, 35, 99, 0, 254, 108, 154, 254, 154, 190, 40, 237, 25, 58, 246, 111, 19, 44, 215, 141, 140, 156, 117])));
499 ///
500 /// let value = Value::Text("a811".to_string());
501 /// assert_eq!(value.to_bytes_32(), Err(Error::ByteLengthNot32BytesError("buffer was not 32 bytes long".to_string())));
502 ///
503 /// let value = Value::Text("a811Ii".to_string());
504 /// assert_eq!(value.to_bytes_32(), Err(Error::StructureError("value was a string, but could not be decoded from base 64 to bytes 32, error: Invalid last symbol 105, offset 5.".to_string())));
505 ///
506 /// let value = Value::Array(vec![Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101)]);
507 /// assert_eq!(value.to_bytes_32(), Ok(Bytes32::new([104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101])));
508 ///
509 /// let value = Value::Identifier([5u8;32]);
510 /// assert_eq!(value.to_bytes_32(), Ok(Bytes32::new([5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5])));
511 ///
512 /// let value = Value::Bool(true);
513 /// assert_eq!(value.to_bytes_32(), Err(Error::StructureError("value are not bytes, a string, or an array of values representing bytes".to_string())));
514 /// ```
515 pub fn to_bytes_32(&self) -> Result<Bytes32, Error> {
516 match self {
517 Value::Text(text) => Bytes32::from_vec(GeneralPurpose::new(&alphabet::STANDARD, PADDING_INDIFFERENT).decode(text).map_err(|e| {
518 Error::StructureError(
519 format!("value was a string, but could not be decoded from base 64 to bytes 32, error: {}", e),
520 )
521 })?),
522 Value::Array(array) => Bytes32::from_vec(
523 array
524 .iter()
525 .map(|byte| byte.to_integer())
526 .collect::<Result<Vec<u8>, Error>>()?,
527 ),
528 Value::Bytes32(bytes) => Ok(Bytes32::new(*bytes)),
529 Value::Bytes(vec) => Bytes32::from_vec(vec.clone()),
530 Value::Identifier(identifier) => Ok(Bytes32::new(*identifier)),
531 _other => Err(Error::StructureError(
532 "value are not bytes, a string, or an array of values representing bytes"
533 .to_string(),
534 )),
535 }
536 }
537
538 /// If the `Value` is a `Bytes`, a `Text` using base 64 or Vector of `U8`, returns the
539 /// associated `Bytes36` data as `Ok`.
540 /// Returns `Err(Error::Structure("reason"))` otherwise.
541 ///
542 /// ```
543 /// # use platform_value::{Bytes36, Error, Value};
544 ///
545 /// #
546 /// let value = Value::Bytes(vec![104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 50, 51, 52, 53]);
547 /// assert_eq!(value.into_bytes_36(), Ok(Bytes36([104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 50, 51, 52, 53]))); ///
548 ///
549 /// let value = Value::Text("sNr9aH0VUnzvKDADkuJUlZ4Yj7Yd7gbNnnOR4ANNat2g498D".to_string());
550 /// assert_eq!(value.into_bytes_36(), Ok(Bytes36([176, 218, 253, 104, 125, 21, 82, 124, 239, 40, 48, 3, 146, 226, 84, 149, 158, 24, 143, 182, 29, 238, 6, 205, 158, 115, 145, 224, 3, 77, 106, 221, 160, 227, 223, 3])));
551 ///
552 /// let value = Value::Text("a811".to_string());
553 /// assert_eq!(value.into_bytes_36(), Err(Error::ByteLengthNot36BytesError("buffer was not 36 bytes long".to_string())));
554 ///
555 /// let value = Value::Text("a811Ii".to_string());
556 /// assert_eq!(value.into_bytes_36(), Err(Error::StructureError("value was a string, but could not be decoded from base 64 into bytes 36, error: Invalid last symbol 105, offset 5.".to_string())));
557 ///
558 /// let value = Value::Array(vec![Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(101), Value::U8(101), Value::U8(101), Value::U8(101)]);
559 /// assert_eq!(value.into_bytes_36(), Ok(Bytes36([104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 101, 101, 101, 101])));
560 ///
561 /// let value = Value::Bool(true);
562 /// assert_eq!(value.into_bytes_36(), Err(Error::StructureError("value are not bytes, a string, or an array of values representing bytes".to_string())));
563 /// ```
564 pub fn into_bytes_36(self) -> Result<Bytes36, Error> {
565 match self {
566 Value::Text(text) => Bytes36::from_vec(GeneralPurpose::new(&alphabet::STANDARD, PADDING_INDIFFERENT).decode(text).map_err(|e| {
567 Error::StructureError(
568 format!("value was a string, but could not be decoded from base 64 into bytes 36, error: {}", e),
569 )
570 })?),
571 Value::Array(array) => Bytes36::from_vec(
572 array
573 .into_iter()
574 .map(|byte| byte.into_integer())
575 .collect::<Result<Vec<u8>, Error>>()?,
576 ),
577 Value::Bytes36(bytes) => Ok(Bytes36::new(bytes)),
578 Value::Bytes(vec) => Bytes36::from_vec(vec),
579 _other => Err(Error::StructureError(
580 "value are not bytes, a string, or an array of values representing bytes"
581 .to_string(),
582 )),
583 }
584 }
585
586 /// If the `Value` is a `Bytes`, a `Text` using base 64 or Vector of `U8`, returns the
587 /// associated `Bytes36` data as `Ok`.
588 /// Returns `Err(Error::Structure("reason"))` otherwise.
589 ///
590 /// ```
591 /// # use platform_value::{Bytes36, Error, Value};
592 ///
593 /// #
594 /// let value = Value::Bytes(vec![104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 50, 51, 52, 53]);
595 /// assert_eq!(value.to_bytes_36(), Ok(Bytes36([104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 50, 51, 52, 53]))); ///
596 ///
597 /// let value = Value::Text("sNr9aH0VUnzvKDADkuJUlZ4Yj7Yd7gbNnnOR4ANNat2g498D".to_string());
598 /// assert_eq!(value.to_bytes_36(), Ok(Bytes36([176, 218, 253, 104, 125, 21, 82, 124, 239, 40, 48, 3, 146, 226, 84, 149, 158, 24, 143, 182, 29, 238, 6, 205, 158, 115, 145, 224, 3, 77, 106, 221, 160, 227, 223, 3])));
599 ///
600 /// let value = Value::Text("a811".to_string());
601 /// assert_eq!(value.to_bytes_36(), Err(Error::ByteLengthNot36BytesError("buffer was not 36 bytes long".to_string())));
602 ///
603 /// let value = Value::Text("a811Ii".to_string());
604 /// assert_eq!(value.to_bytes_36(), Err(Error::StructureError("value was a string, but could not be decoded from base 64 to bytes 36, error: Invalid last symbol 105, offset 5.".to_string())));
605 ///
606 /// let value = Value::Array(vec![Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(101), Value::U8(101), Value::U8(101), Value::U8(101)]);
607 /// assert_eq!(value.to_bytes_36(), Ok(Bytes36([104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 101, 101, 101, 101])));
608 ///
609 /// let value = Value::Bool(true);
610 /// assert_eq!(value.to_bytes_36(), Err(Error::StructureError("value are not bytes, a string, or an array of values representing bytes".to_string())));
611 /// ```
612 pub fn to_bytes_36(&self) -> Result<Bytes36, Error> {
613 match self {
614 Value::Text(text) => Bytes36::from_vec(GeneralPurpose::new(&alphabet::STANDARD, PADDING_INDIFFERENT).decode(text).map_err(|e| {
615 Error::StructureError(
616 format!("value was a string, but could not be decoded from base 64 to bytes 36, error: {}", e),
617 )
618 })?),
619 Value::Array(array) => Bytes36::from_vec(
620 array
621 .iter()
622 .map(|byte| byte.to_integer())
623 .collect::<Result<Vec<u8>, Error>>()?,
624 ),
625 Value::Bytes36(bytes) => Ok(Bytes36::new(*bytes)),
626 Value::Bytes(vec) => Bytes36::from_vec(vec.clone()),
627 _other => Err(Error::StructureError(
628 "value are not bytes, a string, or an array of values representing bytes"
629 .to_string(),
630 )),
631 }
632 }
633
634 /// If the `Value` is a `Bytes`, a `Text` using base 58 or Vector of `U8`, returns the
635 /// associated `Identifier` data as `Ok`.
636 /// Returns `Err(Error::Structure("reason"))` otherwise.
637 ///
638 /// ```
639 /// # use platform_value::{Error, Identifier, Value};
640 /// #
641 /// let value = Value::Bytes(vec![104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50]);
642 /// assert_eq!(value.into_identifier(), Ok(Identifier::new([104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50]))); ///
643 ///
644 /// let value = Value::Text("6oFRdsUNiAtXscRn52atKYCiF8RBnH9vbUzhtzY3d83e".to_string());
645 /// assert_eq!(value.into_identifier(), Ok(Identifier::new([86, 35, 118, 67, 167, 43, 101, 109, 72, 97, 35, 99, 0, 254, 108, 154, 254, 154, 190, 40, 237, 25, 58, 246, 111, 19, 44, 215, 141, 140, 156, 117])));
646 ///
647 /// let value = Value::Text("a811".to_string());
648 /// assert_eq!(value.into_identifier(), Err(Error::StructureError("value was a string, could be decoded from base 58, but was not 32 bytes long".to_string())));
649 ///
650 /// let value = Value::Text("a811Ii".to_string());
651 /// assert_eq!(value.into_identifier(), Err(Error::StructureError("value was a string, but could not be decoded from base 58".to_string())));
652 ///
653 /// let value = Value::Array(vec![Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101)]);
654 /// assert_eq!(value.into_identifier(), Ok(Identifier::new([104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101])));
655 ///
656 /// let value = Value::Identifier([5u8;32]);
657 /// assert_eq!(value.into_identifier(), Ok(Identifier::new([5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5])));
658 ///
659 /// let value = Value::Bool(true);
660 /// assert_eq!(value.into_identifier(), Err(Error::StructureError("value are not bytes, a string, or an array of values representing bytes".to_string())));
661 /// ```
662 pub fn into_identifier(self) -> Result<Identifier, Error> {
663 match self {
664 Value::Text(text) => {
665 bs58::decode(text).into_vec()
666 .map_err(|_| Error::StructureError("value was a string, but could not be decoded from base 58".to_string()))?
667 .try_into()
668 .map_err(|_| Error::StructureError("value was a string, could be decoded from base 58, but was not 32 bytes long".to_string()))
669 }
670 Value::Array(array) => {
671 Ok(array
672 .into_iter()
673 .map(|byte| match byte {
674 Value::U8(value_as_u8) => {
675 Ok(value_as_u8)
676 }
677 _ => Err(Error::StructureError("not an array of bytes".to_string())),
678 })
679 .collect::<Result<Vec<u8>, Error>>()?
680 .try_into()
681 .map_err(|_| Error::StructureError("value was an array of bytes, but was not 32 bytes long".to_string()))?)
682 }
683 Value::Bytes(vec) => {
684 vec.try_into()
685 .map_err(|_| Error::StructureError("value was bytes, but was not 32 bytes long".to_string()))
686 },
687 Value::Bytes32(bytes) => Ok(Identifier::new(bytes)),
688 Value::Identifier(identifier) => Ok(Identifier::new(identifier)),
689 _other => Err(Error::StructureError("value are not bytes, a string, or an array of values representing bytes".to_string())),
690 }
691 }
692
693 /// If the `Value` is a `Bytes`, a `Text` using base 58 or Vector of `U8`, returns the
694 /// associated `Identifier` data as `Ok`.
695 /// Returns `Err(Error::Structure("reason"))` otherwise.
696 ///
697 /// ```
698 /// # use platform_value::{Error, Identifier, Value};
699 /// #
700 /// let value = Value::Bytes(vec![104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50]);
701 /// assert_eq!(value.to_identifier(), Ok(Identifier::new([104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50]))); ///
702 ///
703 /// let value = Value::Text("6oFRdsUNiAtXscRn52atKYCiF8RBnH9vbUzhtzY3d83e".to_string());
704 /// assert_eq!(value.to_identifier(), Ok(Identifier::new([86, 35, 118, 67, 167, 43, 101, 109, 72, 97, 35, 99, 0, 254, 108, 154, 254, 154, 190, 40, 237, 25, 58, 246, 111, 19, 44, 215, 141, 140, 156, 117])));
705 ///
706 /// let value = Value::Text("a811".to_string());
707 /// assert_eq!(value.to_identifier(), Err(Error::StructureError("value was a string, could be decoded from base 58, but was not 32 bytes long".to_string())));
708 ///
709 /// let value = Value::Text("a811Ii".to_string());
710 /// assert_eq!(value.to_identifier(), Err(Error::StructureError("value was a string, but could not be decoded from base 58".to_string())));
711 ///
712 /// let value = Value::Array(vec![Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101)]);
713 /// assert_eq!(value.to_identifier(), Ok(Identifier::new([104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101])));
714 ///
715 /// let value = Value::Identifier([5u8;32]);
716 /// assert_eq!(value.to_identifier(), Ok(Identifier::new([5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5])));
717 ///
718 /// let value = Value::Bool(true);
719 /// assert_eq!(value.to_identifier(), Err(Error::StructureError("value are not bytes, a string, or an array of values representing bytes".to_string())));
720 /// ```
721 pub fn to_identifier(&self) -> Result<Identifier, Error> {
722 match self {
723 Value::Text(text) => {
724 bs58::decode(text).into_vec()
725 .map_err(|_| Error::StructureError("value was a string, but could not be decoded from base 58".to_string()))?
726 .try_into()
727 .map_err(|_| Error::StructureError("value was a string, could be decoded from base 58, but was not 32 bytes long".to_string()))
728 },
729 Value::Array(array) => {
730 Ok(array
731 .iter()
732 .map(|byte| byte.to_integer())
733 .collect::<Result<Vec<u8>, Error>>()?
734 .try_into()
735 .map_err(|_| Error::StructureError("value was an array of bytes, but was not 32 bytes long".to_string()))?)
736 },
737 Value::Bytes32(bytes) => Ok(Identifier::new(*bytes)),
738 Value::Bytes(vec) => {
739 vec.clone().try_into()
740 .map_err(|_| Error::StructureError("value was bytes, but was not 32 bytes long".to_string()))
741 },
742 Value::Identifier(identifier) => Ok(Identifier::new(*identifier)),
743 _other => Err(Error::StructureError("value are not bytes, a string, or an array of values representing bytes".to_string())),
744 }
745 }
746}
747
748#[cfg(test)]
749mod tests {
750 use super::*;
751
752 // ---------------------------------------------------------------
753 // into_identifier_bytes / to_identifier_bytes (base-58 encoding)
754 // ---------------------------------------------------------------
755
756 #[test]
757 fn into_identifier_bytes_from_bytes() {
758 let val = Value::Bytes(vec![1, 2, 3]);
759 assert_eq!(val.into_identifier_bytes().unwrap(), vec![1, 2, 3]);
760 }
761
762 #[test]
763 fn into_identifier_bytes_from_text_bs58() {
764 // "a811" is a valid base58 string
765 let val = Value::Text("a811".to_string());
766 let result = val.into_identifier_bytes().unwrap();
767 // Verify round-trip: encode back to base58
768 let encoded = bs58::encode(&result).into_string();
769 assert_eq!(encoded, "a811");
770 }
771
772 #[test]
773 fn into_identifier_bytes_from_array_of_u8() {
774 let val = Value::Array(vec![Value::U8(10), Value::U8(20), Value::U8(30)]);
775 assert_eq!(val.into_identifier_bytes().unwrap(), vec![10, 20, 30]);
776 }
777
778 #[test]
779 fn into_identifier_bytes_from_identifier() {
780 let id = [7u8; 32];
781 let val = Value::Identifier(id);
782 assert_eq!(val.into_identifier_bytes().unwrap(), id.to_vec());
783 }
784
785 #[test]
786 fn into_identifier_bytes_from_bytes32() {
787 let data = [3u8; 32];
788 let val = Value::Bytes32(data);
789 assert_eq!(val.into_identifier_bytes().unwrap(), data.to_vec());
790 }
791
792 #[test]
793 fn into_identifier_bytes_wrong_type_errors() {
794 let val = Value::Bool(true);
795 assert!(val.into_identifier_bytes().is_err());
796 }
797
798 #[test]
799 fn into_identifier_bytes_bad_array_element_errors() {
800 let val = Value::Array(vec![Value::Text("not_a_byte".into())]);
801 assert!(val.into_identifier_bytes().is_err());
802 }
803
804 #[test]
805 fn to_identifier_bytes_round_trip() {
806 let original = vec![50, 100, 150, 200];
807 let val = Value::Bytes(original.clone());
808 assert_eq!(val.to_identifier_bytes().unwrap(), original);
809 }
810
811 #[test]
812 fn to_identifier_bytes_from_text_bs58() {
813 let val = Value::Text("a811".to_string());
814 let result = val.to_identifier_bytes().unwrap();
815 let encoded = bs58::encode(&result).into_string();
816 assert_eq!(encoded, "a811");
817 }
818
819 #[test]
820 fn to_identifier_bytes_from_array() {
821 let val = Value::Array(vec![Value::U8(5), Value::U8(10)]);
822 assert_eq!(val.to_identifier_bytes().unwrap(), vec![5, 10]);
823 }
824
825 #[test]
826 fn to_identifier_bytes_from_bytes32() {
827 let data = [9u8; 32];
828 let val = Value::Bytes32(data);
829 assert_eq!(val.to_identifier_bytes().unwrap(), data.to_vec());
830 }
831
832 #[test]
833 fn to_identifier_bytes_from_identifier() {
834 let id = [11u8; 32];
835 let val = Value::Identifier(id);
836 assert_eq!(val.to_identifier_bytes().unwrap(), id.to_vec());
837 }
838
839 #[test]
840 fn to_identifier_bytes_wrong_type_errors() {
841 let val = Value::Float(1.0);
842 assert!(val.to_identifier_bytes().is_err());
843 }
844
845 // ---------------------------------------------------------------
846 // into_binary_bytes / to_binary_bytes (base-64 encoding)
847 // ---------------------------------------------------------------
848
849 #[test]
850 fn into_binary_bytes_from_bytes() {
851 let val = Value::Bytes(vec![1, 2, 3]);
852 assert_eq!(val.into_binary_bytes().unwrap(), vec![1, 2, 3]);
853 }
854
855 #[test]
856 fn into_binary_bytes_from_text_b64() {
857 // base64 "AQID" = [1, 2, 3]
858 let val = Value::Text("AQID".to_string());
859 assert_eq!(val.into_binary_bytes().unwrap(), vec![1, 2, 3]);
860 }
861
862 #[test]
863 fn into_binary_bytes_from_array_of_u8() {
864 let val = Value::Array(vec![Value::U8(4), Value::U8(5)]);
865 assert_eq!(val.into_binary_bytes().unwrap(), vec![4, 5]);
866 }
867
868 #[test]
869 fn into_binary_bytes_from_identifier() {
870 let id = [2u8; 32];
871 let val = Value::Identifier(id);
872 assert_eq!(val.into_binary_bytes().unwrap(), id.to_vec());
873 }
874
875 #[test]
876 fn into_binary_bytes_from_bytes32() {
877 let data = [6u8; 32];
878 let val = Value::Bytes32(data);
879 assert_eq!(val.into_binary_bytes().unwrap(), data.to_vec());
880 }
881
882 #[test]
883 fn into_binary_bytes_wrong_type_errors() {
884 let val = Value::Bool(false);
885 assert!(val.into_binary_bytes().is_err());
886 }
887
888 #[test]
889 fn into_binary_bytes_bad_b64_errors() {
890 // "!!!!" is not valid base64
891 let val = Value::Text("!!!!".to_string());
892 assert!(val.into_binary_bytes().is_err());
893 }
894
895 #[test]
896 fn to_binary_bytes_round_trip() {
897 let original = vec![10, 20, 30, 40];
898 let val = Value::Bytes(original.clone());
899 assert_eq!(val.to_binary_bytes().unwrap(), original);
900 }
901
902 #[test]
903 fn to_binary_bytes_from_text_b64() {
904 let val = Value::Text("AQID".to_string());
905 assert_eq!(val.to_binary_bytes().unwrap(), vec![1, 2, 3]);
906 }
907
908 #[test]
909 fn to_binary_bytes_from_bytes20() {
910 let data = [8u8; 20];
911 let val = Value::Bytes20(data);
912 assert_eq!(val.to_binary_bytes().unwrap(), data.to_vec());
913 }
914
915 #[test]
916 fn to_binary_bytes_from_bytes32() {
917 let data = [9u8; 32];
918 let val = Value::Bytes32(data);
919 assert_eq!(val.to_binary_bytes().unwrap(), data.to_vec());
920 }
921
922 #[test]
923 fn to_binary_bytes_from_bytes36() {
924 let data = [10u8; 36];
925 let val = Value::Bytes36(data);
926 assert_eq!(val.to_binary_bytes().unwrap(), data.to_vec());
927 }
928
929 #[test]
930 fn to_binary_bytes_from_identifier() {
931 let id = [11u8; 32];
932 let val = Value::Identifier(id);
933 assert_eq!(val.to_binary_bytes().unwrap(), id.to_vec());
934 }
935
936 #[test]
937 fn to_binary_bytes_wrong_type_errors() {
938 let val = Value::Null;
939 assert!(val.to_binary_bytes().is_err());
940 }
941
942 // ---------------------------------------------------------------
943 // into_binary_data / to_binary_data (wraps binary_bytes)
944 // ---------------------------------------------------------------
945
946 #[test]
947 fn into_binary_data_from_bytes() {
948 let val = Value::Bytes(vec![1, 2, 3]);
949 assert_eq!(
950 val.into_binary_data().unwrap(),
951 BinaryData::new(vec![1, 2, 3])
952 );
953 }
954
955 #[test]
956 fn into_binary_data_from_identifier() {
957 let id = [5u8; 32];
958 let val = Value::Identifier(id);
959 assert_eq!(
960 val.into_binary_data().unwrap(),
961 BinaryData::new(id.to_vec())
962 );
963 }
964
965 #[test]
966 fn into_binary_data_wrong_type_errors() {
967 let val = Value::Bool(true);
968 assert!(val.into_binary_data().is_err());
969 }
970
971 // ---------------------------------------------------------------
972 // into_hash256 / to_hash256
973 // ---------------------------------------------------------------
974
975 #[test]
976 fn into_hash256_from_bytes32() {
977 let data = [4u8; 32];
978 let val = Value::Bytes32(data);
979 assert_eq!(val.into_hash256().unwrap(), data);
980 }
981
982 #[test]
983 fn into_hash256_from_identifier() {
984 let data = [5u8; 32];
985 let val = Value::Identifier(data);
986 assert_eq!(val.into_hash256().unwrap(), data);
987 }
988
989 #[test]
990 fn into_hash256_from_bytes_correct_length() {
991 let data = vec![6u8; 32];
992 let val = Value::Bytes(data.clone());
993 assert_eq!(
994 val.into_hash256().unwrap(),
995 <[u8; 32]>::try_from(data.as_slice()).unwrap()
996 );
997 }
998
999 #[test]
1000 fn into_hash256_from_bytes_wrong_length_errors() {
1001 let val = Value::Bytes(vec![1, 2, 3]);
1002 assert!(val.into_hash256().is_err());
1003 }
1004
1005 #[test]
1006 fn into_hash256_from_text_bs58_valid() {
1007 // Create a valid 32-byte bs58 string
1008 let data = [7u8; 32];
1009 let encoded = bs58::encode(data).into_string();
1010 let val = Value::Text(encoded);
1011 assert_eq!(val.into_hash256().unwrap(), data);
1012 }
1013
1014 #[test]
1015 fn into_hash256_from_text_bs58_wrong_length() {
1016 // "a811" decodes to 3 bytes, not 32
1017 let val = Value::Text("a811".to_string());
1018 assert!(val.into_hash256().is_err());
1019 }
1020
1021 #[test]
1022 fn into_hash256_from_text_invalid_bs58() {
1023 let val = Value::Text("a811Ii".to_string());
1024 assert!(val.into_hash256().is_err());
1025 }
1026
1027 #[test]
1028 fn into_hash256_from_array_of_32_u8s() {
1029 let arr: Vec<Value> = (0..32).map(|i| Value::U8(i as u8)).collect();
1030 let val = Value::Array(arr);
1031 let expected: [u8; 32] = core::array::from_fn(|i| i as u8);
1032 assert_eq!(val.into_hash256().unwrap(), expected);
1033 }
1034
1035 #[test]
1036 fn into_hash256_from_array_wrong_length() {
1037 let val = Value::Array(vec![Value::U8(1), Value::U8(2)]);
1038 assert!(val.into_hash256().is_err());
1039 }
1040
1041 #[test]
1042 fn into_hash256_wrong_type_errors() {
1043 let val = Value::Bool(true);
1044 assert!(val.into_hash256().is_err());
1045 }
1046
1047 #[test]
1048 fn to_hash256_from_bytes32() {
1049 let data = [8u8; 32];
1050 let val = Value::Bytes32(data);
1051 assert_eq!(val.to_hash256().unwrap(), data);
1052 }
1053
1054 #[test]
1055 fn to_hash256_from_identifier() {
1056 let data = [9u8; 32];
1057 let val = Value::Identifier(data);
1058 assert_eq!(val.to_hash256().unwrap(), data);
1059 }
1060
1061 #[test]
1062 fn to_hash256_from_bytes_correct_length() {
1063 let data = vec![10u8; 32];
1064 let val = Value::Bytes(data.clone());
1065 assert_eq!(
1066 val.to_hash256().unwrap(),
1067 <[u8; 32]>::try_from(data.as_slice()).unwrap()
1068 );
1069 }
1070
1071 #[test]
1072 fn to_hash256_from_bytes_wrong_length_errors() {
1073 let val = Value::Bytes(vec![1, 2]);
1074 assert!(val.to_hash256().is_err());
1075 }
1076
1077 #[test]
1078 fn to_hash256_round_trip_with_bs58() {
1079 let data = [11u8; 32];
1080 let encoded = bs58::encode(data).into_string();
1081 let val = Value::Text(encoded);
1082 assert_eq!(val.to_hash256().unwrap(), data);
1083 }
1084
1085 #[test]
1086 fn to_hash256_wrong_type_errors() {
1087 let val = Value::Float(1.0);
1088 assert!(val.to_hash256().is_err());
1089 }
1090
1091 // ---------------------------------------------------------------
1092 // into_bytes_20 / to_bytes_20
1093 // ---------------------------------------------------------------
1094
1095 #[test]
1096 fn into_bytes_20_from_bytes20() {
1097 let data = [1u8; 20];
1098 let val = Value::Bytes20(data);
1099 assert_eq!(val.into_bytes_20().unwrap(), Bytes20::new(data));
1100 }
1101
1102 #[test]
1103 fn into_bytes_20_from_bytes_correct_length() {
1104 let data = vec![2u8; 20];
1105 let val = Value::Bytes(data);
1106 assert_eq!(val.into_bytes_20().unwrap(), Bytes20::new([2u8; 20]));
1107 }
1108
1109 #[test]
1110 fn into_bytes_20_from_bytes_wrong_length_errors() {
1111 let val = Value::Bytes(vec![1, 2, 3]);
1112 assert!(val.into_bytes_20().is_err());
1113 }
1114
1115 #[test]
1116 fn into_bytes_20_from_text_b64_round_trip() {
1117 let data = [3u8; 20];
1118 let encoded = base64::engine::general_purpose::STANDARD.encode(data);
1119 let val = Value::Text(encoded);
1120 assert_eq!(val.into_bytes_20().unwrap(), Bytes20::new(data));
1121 }
1122
1123 #[test]
1124 fn into_bytes_20_from_text_b64_wrong_length() {
1125 // "AQID" = 3 bytes, not 20
1126 let val = Value::Text("AQID".to_string());
1127 assert!(val.into_bytes_20().is_err());
1128 }
1129
1130 #[test]
1131 fn into_bytes_20_from_array_of_20_u8s() {
1132 let arr: Vec<Value> = (0..20).map(|i| Value::U8(i as u8)).collect();
1133 let val = Value::Array(arr);
1134 let expected: [u8; 20] = core::array::from_fn(|i| i as u8);
1135 assert_eq!(val.into_bytes_20().unwrap(), Bytes20::new(expected));
1136 }
1137
1138 #[test]
1139 fn into_bytes_20_wrong_type_errors() {
1140 let val = Value::Bool(true);
1141 assert!(val.into_bytes_20().is_err());
1142 }
1143
1144 #[test]
1145 fn to_bytes_20_from_bytes20() {
1146 let data = [4u8; 20];
1147 let val = Value::Bytes20(data);
1148 assert_eq!(val.to_bytes_20().unwrap(), Bytes20::new(data));
1149 }
1150
1151 #[test]
1152 fn to_bytes_20_from_bytes_correct_length() {
1153 let data = vec![5u8; 20];
1154 let val = Value::Bytes(data);
1155 assert_eq!(val.to_bytes_20().unwrap(), Bytes20::new([5u8; 20]));
1156 }
1157
1158 #[test]
1159 fn to_bytes_20_from_text_b64_round_trip() {
1160 let data = [6u8; 20];
1161 let encoded = base64::engine::general_purpose::STANDARD.encode(data);
1162 let val = Value::Text(encoded);
1163 assert_eq!(val.to_bytes_20().unwrap(), Bytes20::new(data));
1164 }
1165
1166 #[test]
1167 fn to_bytes_20_wrong_type_errors() {
1168 let val = Value::Null;
1169 assert!(val.to_bytes_20().is_err());
1170 }
1171
1172 // ---------------------------------------------------------------
1173 // into_bytes_32 / to_bytes_32
1174 // ---------------------------------------------------------------
1175
1176 #[test]
1177 fn into_bytes_32_from_bytes32() {
1178 let data = [1u8; 32];
1179 let val = Value::Bytes32(data);
1180 assert_eq!(val.into_bytes_32().unwrap(), Bytes32::new(data));
1181 }
1182
1183 #[test]
1184 fn into_bytes_32_from_bytes_correct_length() {
1185 let data = vec![2u8; 32];
1186 let val = Value::Bytes(data);
1187 assert_eq!(val.into_bytes_32().unwrap(), Bytes32::new([2u8; 32]));
1188 }
1189
1190 #[test]
1191 fn into_bytes_32_from_bytes_wrong_length_errors() {
1192 let val = Value::Bytes(vec![1, 2, 3]);
1193 assert!(val.into_bytes_32().is_err());
1194 }
1195
1196 #[test]
1197 fn into_bytes_32_from_identifier() {
1198 let data = [3u8; 32];
1199 let val = Value::Identifier(data);
1200 assert_eq!(val.into_bytes_32().unwrap(), Bytes32::new(data));
1201 }
1202
1203 #[test]
1204 fn into_bytes_32_from_text_b64_round_trip() {
1205 let data = [4u8; 32];
1206 let encoded = base64::engine::general_purpose::STANDARD.encode(data);
1207 let val = Value::Text(encoded);
1208 assert_eq!(val.into_bytes_32().unwrap(), Bytes32::new(data));
1209 }
1210
1211 #[test]
1212 fn into_bytes_32_from_text_b64_wrong_length() {
1213 let val = Value::Text("AQID".to_string());
1214 assert!(val.into_bytes_32().is_err());
1215 }
1216
1217 #[test]
1218 fn into_bytes_32_from_text_invalid_b64_errors() {
1219 let val = Value::Text("a811Ii".to_string());
1220 assert!(val.into_bytes_32().is_err());
1221 }
1222
1223 #[test]
1224 fn into_bytes_32_from_array_of_32_u8s() {
1225 let arr: Vec<Value> = (0..32).map(|i| Value::U8(i as u8)).collect();
1226 let val = Value::Array(arr);
1227 let expected: [u8; 32] = core::array::from_fn(|i| i as u8);
1228 assert_eq!(val.into_bytes_32().unwrap(), Bytes32::new(expected));
1229 }
1230
1231 #[test]
1232 fn into_bytes_32_wrong_type_errors() {
1233 let val = Value::Bool(true);
1234 assert!(val.into_bytes_32().is_err());
1235 }
1236
1237 #[test]
1238 fn to_bytes_32_from_bytes32() {
1239 let data = [5u8; 32];
1240 let val = Value::Bytes32(data);
1241 assert_eq!(val.to_bytes_32().unwrap(), Bytes32::new(data));
1242 }
1243
1244 #[test]
1245 fn to_bytes_32_from_identifier() {
1246 let data = [6u8; 32];
1247 let val = Value::Identifier(data);
1248 assert_eq!(val.to_bytes_32().unwrap(), Bytes32::new(data));
1249 }
1250
1251 #[test]
1252 fn to_bytes_32_from_bytes_correct_length() {
1253 let data = vec![7u8; 32];
1254 let val = Value::Bytes(data);
1255 assert_eq!(val.to_bytes_32().unwrap(), Bytes32::new([7u8; 32]));
1256 }
1257
1258 #[test]
1259 fn to_bytes_32_from_text_b64_round_trip() {
1260 let data = [8u8; 32];
1261 let encoded = base64::engine::general_purpose::STANDARD.encode(data);
1262 let val = Value::Text(encoded);
1263 assert_eq!(val.to_bytes_32().unwrap(), Bytes32::new(data));
1264 }
1265
1266 #[test]
1267 fn to_bytes_32_wrong_type_errors() {
1268 let val = Value::Float(1.0);
1269 assert!(val.to_bytes_32().is_err());
1270 }
1271
1272 // ---------------------------------------------------------------
1273 // into_bytes_36 / to_bytes_36
1274 // ---------------------------------------------------------------
1275
1276 #[test]
1277 fn into_bytes_36_from_bytes36() {
1278 let data = [1u8; 36];
1279 let val = Value::Bytes36(data);
1280 assert_eq!(val.into_bytes_36().unwrap(), Bytes36::new(data));
1281 }
1282
1283 #[test]
1284 fn into_bytes_36_from_bytes_correct_length() {
1285 let data = vec![2u8; 36];
1286 let val = Value::Bytes(data);
1287 assert_eq!(val.into_bytes_36().unwrap(), Bytes36::new([2u8; 36]));
1288 }
1289
1290 #[test]
1291 fn into_bytes_36_from_bytes_wrong_length_errors() {
1292 let val = Value::Bytes(vec![1, 2, 3]);
1293 assert!(val.into_bytes_36().is_err());
1294 }
1295
1296 #[test]
1297 fn into_bytes_36_from_text_b64_round_trip() {
1298 let data = [3u8; 36];
1299 let encoded = base64::engine::general_purpose::STANDARD.encode(data);
1300 let val = Value::Text(encoded);
1301 assert_eq!(val.into_bytes_36().unwrap(), Bytes36::new(data));
1302 }
1303
1304 #[test]
1305 fn into_bytes_36_from_text_b64_wrong_length() {
1306 let val = Value::Text("AQID".to_string());
1307 assert!(val.into_bytes_36().is_err());
1308 }
1309
1310 #[test]
1311 fn into_bytes_36_from_text_invalid_b64_errors() {
1312 let val = Value::Text("a811Ii".to_string());
1313 assert!(val.into_bytes_36().is_err());
1314 }
1315
1316 #[test]
1317 fn into_bytes_36_from_array_of_36_u8s() {
1318 let arr: Vec<Value> = (0..36).map(|i| Value::U8(i as u8)).collect();
1319 let val = Value::Array(arr);
1320 let expected: [u8; 36] = core::array::from_fn(|i| i as u8);
1321 assert_eq!(val.into_bytes_36().unwrap(), Bytes36::new(expected));
1322 }
1323
1324 #[test]
1325 fn into_bytes_36_wrong_type_errors() {
1326 let val = Value::Bool(true);
1327 assert!(val.into_bytes_36().is_err());
1328 }
1329
1330 #[test]
1331 fn to_bytes_36_from_bytes36() {
1332 let data = [4u8; 36];
1333 let val = Value::Bytes36(data);
1334 assert_eq!(val.to_bytes_36().unwrap(), Bytes36::new(data));
1335 }
1336
1337 #[test]
1338 fn to_bytes_36_from_bytes_correct_length() {
1339 let data = vec![5u8; 36];
1340 let val = Value::Bytes(data);
1341 assert_eq!(val.to_bytes_36().unwrap(), Bytes36::new([5u8; 36]));
1342 }
1343
1344 #[test]
1345 fn to_bytes_36_from_text_b64_round_trip() {
1346 let data = [6u8; 36];
1347 let encoded = base64::engine::general_purpose::STANDARD.encode(data);
1348 let val = Value::Text(encoded);
1349 assert_eq!(val.to_bytes_36().unwrap(), Bytes36::new(data));
1350 }
1351
1352 #[test]
1353 fn to_bytes_36_wrong_type_errors() {
1354 let val = Value::Null;
1355 assert!(val.to_bytes_36().is_err());
1356 }
1357
1358 // ---------------------------------------------------------------
1359 // into_identifier / to_identifier
1360 // ---------------------------------------------------------------
1361
1362 #[test]
1363 fn into_identifier_from_identifier() {
1364 let data = [1u8; 32];
1365 let val = Value::Identifier(data);
1366 assert_eq!(val.into_identifier().unwrap(), Identifier::new(data));
1367 }
1368
1369 #[test]
1370 fn into_identifier_from_bytes32() {
1371 let data = [2u8; 32];
1372 let val = Value::Bytes32(data);
1373 assert_eq!(val.into_identifier().unwrap(), Identifier::new(data));
1374 }
1375
1376 #[test]
1377 fn into_identifier_from_bytes_correct_length() {
1378 let data = vec![3u8; 32];
1379 let val = Value::Bytes(data);
1380 assert_eq!(val.into_identifier().unwrap(), Identifier::new([3u8; 32]));
1381 }
1382
1383 #[test]
1384 fn into_identifier_from_bytes_wrong_length_errors() {
1385 let val = Value::Bytes(vec![1, 2, 3]);
1386 assert!(val.into_identifier().is_err());
1387 }
1388
1389 #[test]
1390 fn into_identifier_from_text_bs58_round_trip() {
1391 let data = [4u8; 32];
1392 let encoded = bs58::encode(data).into_string();
1393 let val = Value::Text(encoded);
1394 assert_eq!(val.into_identifier().unwrap(), Identifier::new(data));
1395 }
1396
1397 #[test]
1398 fn into_identifier_from_text_bs58_wrong_length() {
1399 let val = Value::Text("a811".to_string());
1400 assert!(val.into_identifier().is_err());
1401 }
1402
1403 #[test]
1404 fn into_identifier_from_text_invalid_bs58() {
1405 let val = Value::Text("a811Ii".to_string());
1406 assert!(val.into_identifier().is_err());
1407 }
1408
1409 #[test]
1410 fn into_identifier_from_array_of_32_u8s() {
1411 let arr: Vec<Value> = (0..32).map(|i| Value::U8(i as u8)).collect();
1412 let val = Value::Array(arr);
1413 let expected: [u8; 32] = core::array::from_fn(|i| i as u8);
1414 assert_eq!(val.into_identifier().unwrap(), Identifier::new(expected));
1415 }
1416
1417 #[test]
1418 fn into_identifier_from_array_wrong_length() {
1419 let val = Value::Array(vec![Value::U8(1), Value::U8(2)]);
1420 assert!(val.into_identifier().is_err());
1421 }
1422
1423 #[test]
1424 fn into_identifier_wrong_type_errors() {
1425 let val = Value::Bool(true);
1426 assert!(val.into_identifier().is_err());
1427 }
1428
1429 #[test]
1430 fn to_identifier_from_identifier() {
1431 let data = [5u8; 32];
1432 let val = Value::Identifier(data);
1433 assert_eq!(val.to_identifier().unwrap(), Identifier::new(data));
1434 }
1435
1436 #[test]
1437 fn to_identifier_from_bytes32() {
1438 let data = [6u8; 32];
1439 let val = Value::Bytes32(data);
1440 assert_eq!(val.to_identifier().unwrap(), Identifier::new(data));
1441 }
1442
1443 #[test]
1444 fn to_identifier_from_bytes_correct_length() {
1445 let data = vec![7u8; 32];
1446 let val = Value::Bytes(data);
1447 assert_eq!(val.to_identifier().unwrap(), Identifier::new([7u8; 32]));
1448 }
1449
1450 #[test]
1451 fn to_identifier_from_text_bs58_round_trip() {
1452 let data = [8u8; 32];
1453 let encoded = bs58::encode(data).into_string();
1454 let val = Value::Text(encoded);
1455 assert_eq!(val.to_identifier().unwrap(), Identifier::new(data));
1456 }
1457
1458 #[test]
1459 fn to_identifier_from_array_of_32_u8s() {
1460 let arr: Vec<Value> = (0..32).map(|i| Value::U8(i as u8)).collect();
1461 let val = Value::Array(arr.clone());
1462 let expected: [u8; 32] = core::array::from_fn(|i| i as u8);
1463 assert_eq!(val.to_identifier().unwrap(), Identifier::new(expected));
1464 }
1465
1466 #[test]
1467 fn to_identifier_wrong_type_errors() {
1468 let val = Value::Float(1.0);
1469 assert!(val.to_identifier().is_err());
1470 }
1471}