1use crate::value_map::{ValueMap, ValueMapHelper};
2use crate::{BinaryData, Bytes32, Identifier};
3use crate::{Error, Value};
4use indexmap::IndexMap;
5use std::cmp::Ordering;
6use std::collections::{BTreeMap, BTreeSet};
7
8impl Value {
9 pub fn has(&self, key: &str) -> Result<bool, Error> {
10 self.get_optional_value(key).map(|v| v.is_some())
11 }
12
13 pub fn get<'a>(&'a self, key: &'a str) -> Result<Option<&'a Value>, Error> {
14 self.get_optional_value(key)
15 }
16
17 pub fn get_mut<'a>(&'a mut self, key: &'a str) -> Result<Option<&'a mut Value>, Error> {
18 self.get_optional_value_mut(key)
19 }
20
21 pub fn get_value<'a>(&'a self, key: &'a str) -> Result<&'a Value, Error> {
22 let map = self.to_map()?;
23 Self::get_from_map(map, key)
24 }
25
26 pub fn get_value_mut<'a>(&'a mut self, key: &'a str) -> Result<&'a mut Value, Error> {
27 let map = self.to_map_mut()?;
28 Self::get_mut_from_map(map, key)
29 }
30
31 pub fn get_optional_value<'a>(&'a self, key: &'a str) -> Result<Option<&'a Value>, Error> {
32 let map = self.to_map()?;
33 Ok(Self::get_optional_from_map(map, key))
34 }
35
36 pub fn get_optional_value_mut<'a>(
37 &'a mut self,
38 key: &'a str,
39 ) -> Result<Option<&'a mut Value>, Error> {
40 let map = self.to_map_mut()?;
41 Ok(Self::get_optional_mut_from_map(map, key))
42 }
43
44 pub fn set_into_value<T>(&mut self, key: &str, value: T) -> Result<(), Error>
45 where
46 T: Into<Value>,
47 {
48 let map = self.as_map_mut_ref()?;
49 Self::insert_in_map(map, key, value.into());
50 Ok(())
51 }
52
53 pub fn set_into_binary_data(&mut self, key: &str, value: Vec<u8>) -> Result<(), Error> {
54 let map = self.as_map_mut_ref()?;
55 Self::insert_in_map(map, key, Value::Bytes(value));
56 Ok(())
57 }
58
59 pub fn set_value(&mut self, key: &str, value: Value) -> Result<(), Error> {
60 let map = self.as_map_mut_ref()?;
61 Self::insert_in_map(map, key, value);
62 Ok(())
63 }
64
65 pub fn insert(&mut self, key: String, value: Value) -> Result<(), Error> {
66 let map = self.as_map_mut_ref()?;
67 Self::insert_in_map_string_value(map, key, value);
68 Ok(())
69 }
70
71 pub fn insert_at_end(&mut self, key: String, value: Value) -> Result<(), Error> {
72 let map = self.as_map_mut_ref()?;
73 Self::push_to_map_string_value(map, key, value);
74 Ok(())
75 }
76
77 pub fn remove(&mut self, key: &str) -> Result<Value, Error> {
78 let map = self.as_map_mut_ref()?;
79 map.remove_key(key)
80 }
81
82 pub fn remove_many(&mut self, keys: &[&str]) -> Result<(), Error> {
83 let map = self.as_map_mut_ref()?;
84 keys.iter()
85 .try_for_each(|key| map.remove_key(key).map(|_| ()))
86 }
87
88 pub fn remove_optional_value(&mut self, key: &str) -> Result<Option<Value>, Error> {
89 let map = self.as_map_mut_ref()?;
90 Ok(map.remove_optional_key(key))
91 }
92
93 pub fn remove_optional_value_if_null(&mut self, key: &str) -> Result<(), Error> {
94 let map = self.as_map_mut_ref()?;
95 map.remove_optional_key_if_null(key);
96 Ok(())
97 }
98
99 pub fn remove_optional_value_if_empty_array(&mut self, key: &str) -> Result<(), Error> {
100 let map = self.as_map_mut_ref()?;
101 map.remove_optional_key_if_empty_array(key);
102 Ok(())
103 }
104
105 pub fn remove_integer<T>(&mut self, key: &str) -> Result<T, Error>
106 where
107 T: TryFrom<i128>
108 + TryFrom<u128>
109 + TryFrom<u64>
110 + TryFrom<i64>
111 + TryFrom<u32>
112 + TryFrom<i32>
113 + TryFrom<u16>
114 + TryFrom<i16>
115 + TryFrom<u8>
116 + TryFrom<i8>,
117 {
118 let map = self.as_map_mut_ref()?;
119 let value = map.remove_key(key)?;
120 value.into_integer()
121 }
122
123 pub fn remove_optional_integer<T>(&mut self, key: &str) -> Result<Option<T>, Error>
124 where
125 T: TryFrom<i128>
126 + TryFrom<u128>
127 + TryFrom<u64>
128 + TryFrom<i64>
129 + TryFrom<u32>
130 + TryFrom<i32>
131 + TryFrom<u16>
132 + TryFrom<i16>
133 + TryFrom<u8>
134 + TryFrom<i8>,
135 {
136 let map = self.as_map_mut_ref()?;
137 map.remove_optional_key(key)
138 .and_then(|v| {
139 if v.is_null() {
140 None
141 } else {
142 Some(v.into_integer())
143 }
144 })
145 .transpose()
146 }
147
148 pub fn remove_identifier(&mut self, key: &str) -> Result<Identifier, Error> {
149 let map = self.as_map_mut_ref()?;
150 let value = map.remove_key(key)?;
151 value.into_identifier()
152 }
153
154 pub fn remove_optional_identifier(&mut self, key: &str) -> Result<Option<Identifier>, Error> {
155 let map = self.as_map_mut_ref()?;
156 map.remove_optional_key(key)
157 .and_then(|v| {
158 if v.is_null() {
159 None
160 } else {
161 Some(v.into_identifier())
162 }
163 })
164 .transpose()
165 }
166
167 pub fn remove_bytes_32(&mut self, key: &str) -> Result<Bytes32, Error> {
168 let map = self.as_map_mut_ref()?;
169 let value = map.remove_key(key)?;
170 value.into_bytes_32()
171 }
172
173 pub fn remove_optional_bytes_32(&mut self, key: &str) -> Result<Option<Bytes32>, Error> {
174 let map = self.as_map_mut_ref()?;
175 map.remove_optional_key(key)
176 .map(|v| v.into_bytes_32())
177 .transpose()
178 }
179
180 pub fn remove_hash256_bytes(&mut self, key: &str) -> Result<[u8; 32], Error> {
181 let map = self.as_map_mut_ref()?;
182 let value = map.remove_key(key)?;
183 value.into_hash256()
184 }
185
186 pub fn remove_optional_hash256_bytes(&mut self, key: &str) -> Result<Option<[u8; 32]>, Error> {
187 let map = self.as_map_mut_ref()?;
188 map.remove_optional_key(key)
189 .map(|v| v.into_hash256())
190 .transpose()
191 }
192
193 pub fn remove_bytes(&mut self, key: &str) -> Result<Vec<u8>, Error> {
194 let map = self.as_map_mut_ref()?;
195 let value = map.remove_key(key)?;
196 value.into_bytes()
197 }
198
199 pub fn remove_optional_bytes(&mut self, key: &str) -> Result<Option<Vec<u8>>, Error> {
200 let map = self.as_map_mut_ref()?;
201 map.remove_optional_key(key)
202 .map(|v| v.into_bytes())
203 .transpose()
204 }
205
206 pub fn remove_binary_data(&mut self, key: &str) -> Result<BinaryData, Error> {
207 let map = self.as_map_mut_ref()?;
208 let value = map.remove_key(key)?;
209 value.into_binary_data()
210 }
211
212 pub fn remove_optional_binary_data(&mut self, key: &str) -> Result<Option<BinaryData>, Error> {
213 let map = self.as_map_mut_ref()?;
214 map.remove_optional_key(key)
215 .map(|v| v.into_binary_data())
216 .transpose()
217 }
218
219 pub fn remove_array(&mut self, key: &str) -> Result<Vec<Value>, Error> {
220 let map = self.as_map_mut_ref()?;
221 let value = map.remove_key(key)?;
222 value.into_array()
223 }
224
225 pub fn remove_optional_array(&mut self, key: &str) -> Result<Option<Vec<Value>>, Error> {
226 let map = self.as_map_mut_ref()?;
227 map.remove_optional_key(key)
228 .map(|v| v.into_array())
229 .transpose()
230 }
231
232 pub fn get_optional_integer<T>(&self, key: &str) -> Result<Option<T>, Error>
233 where
234 T: TryFrom<i128>
235 + TryFrom<u128>
236 + TryFrom<u64>
237 + TryFrom<i64>
238 + TryFrom<u32>
239 + TryFrom<i32>
240 + TryFrom<u16>
241 + TryFrom<i16>
242 + TryFrom<u8>
243 + TryFrom<i8>,
244 {
245 let map = self.to_map()?;
246 Self::inner_optional_integer_value(map, key)
247 }
248
249 pub fn get_integer<T>(&self, key: &str) -> Result<T, Error>
250 where
251 T: TryFrom<i128>
252 + TryFrom<u128>
253 + TryFrom<u64>
254 + TryFrom<i64>
255 + TryFrom<u32>
256 + TryFrom<i32>
257 + TryFrom<u16>
258 + TryFrom<i16>
259 + TryFrom<u8>
260 + TryFrom<i8>,
261 {
262 let map = self.to_map()?;
263 Self::inner_integer_value(map, key)
264 }
265
266 pub fn get_optional_str<'a>(&'a self, key: &'a str) -> Result<Option<&'a str>, Error> {
267 let map = self.to_map()?;
268 Self::inner_optional_text_value(map, key)
269 }
270
271 pub fn get_str<'a>(&'a self, key: &'a str) -> Result<&'a str, Error> {
272 let map = self.to_map()?;
273 Self::inner_text_value(map, key)
274 }
275
276 pub fn get_optional_bool(&self, key: &str) -> Result<Option<bool>, Error> {
277 let map = self.to_map()?;
278 Self::inner_optional_bool_value(map, key)
279 }
280
281 pub fn get_bool<'a>(&'a self, key: &'a str) -> Result<bool, Error> {
282 let map = self.to_map()?;
283 Self::inner_bool_value(map, key)
284 }
285
286 pub fn get_optional_array(&self, key: &str) -> Result<Option<Vec<Value>>, Error> {
287 let map = self.to_map()?;
288 Self::inner_optional_array(map, key)
289 }
290
291 pub fn get_array<'a>(&'a self, key: &'a str) -> Result<Vec<Value>, Error> {
292 let map = self.to_map()?;
293 Self::inner_array_owned(map, key)
294 }
295
296 pub fn get_optional_string_ref_map<'a, I: FromIterator<(String, &'a Value)>>(
297 &'a self,
298 key: &'a str,
299 ) -> Result<Option<I>, Error> {
300 let map = self.to_map()?;
301 Self::inner_optional_string_ref_map(map, key)
302 }
303
304 pub fn get_string_ref_map<'a, I: FromIterator<(String, &'a Value)>>(
305 &'a self,
306 key: &'a str,
307 ) -> Result<I, Error> {
308 let map = self.to_map()?;
309 Self::inner_string_ref_map(map, key)
310 }
311
312 pub fn get_optional_string_mut_ref_map<'a, I: FromIterator<(String, &'a mut Value)>>(
313 &'a mut self,
314 key: &'a str,
315 ) -> Result<Option<I>, Error> {
316 let map = self.to_map_mut()?;
317 Self::inner_optional_string_mut_ref_map(map, key)
318 }
319
320 pub fn get_string_mut_ref_map<'a, I: FromIterator<(String, &'a mut Value)>>(
321 &'a mut self,
322 key: &'a str,
323 ) -> Result<I, Error> {
324 let map = self.to_map_mut()?;
325 Self::inner_string_mut_ref_map(map, key)
326 }
327
328 pub fn get_optional_array_slice<'a>(
339 &'a self,
340 key: &'a str,
341 ) -> Result<Option<&'a [Value]>, Error> {
342 let map = self.to_map()?;
343 Self::inner_optional_array_slice(map, key)
344 }
345
346 pub fn get_array_ref<'a>(&'a self, key: &'a str) -> Result<&'a Vec<Value>, Error> {
347 let map = self.to_map()?;
348 Self::inner_array_ref(map, key)
349 }
350
351 pub fn get_optional_array_mut_ref<'a>(
352 &'a mut self,
353 key: &'a str,
354 ) -> Result<Option<&'a mut Vec<Value>>, Error> {
355 let map = self.to_map_mut()?;
356 Self::inner_optional_array_mut_ref(map, key)
357 }
358
359 pub fn get_array_mut_ref<'a>(&'a mut self, key: &'a str) -> Result<&'a mut Vec<Value>, Error> {
360 let map = self.to_map_mut()?;
361 Self::inner_array_mut_ref(map, key)
362 }
363
364 pub fn get_array_slice<'a>(&'a self, key: &'a str) -> Result<&'a [Value], Error> {
365 let map = self.to_map()?;
366 Self::inner_array_slice(map, key)
367 }
368
369 pub fn get_optional_binary_data<'a>(
370 &'a self,
371 key: &'a str,
372 ) -> Result<Option<BinaryData>, Error> {
373 let map = self.to_map()?;
374 Self::inner_optional_binary_data_value(map, key)
375 }
376
377 pub fn get_binary_data<'a>(&'a self, key: &'a str) -> Result<BinaryData, Error> {
378 let map = self.to_map()?;
379 Self::inner_binary_data_value(map, key)
380 }
381
382 pub fn get_optional_bytes<'a>(&'a self, key: &'a str) -> Result<Option<Vec<u8>>, Error> {
383 let map = self.to_map()?;
384 Self::inner_optional_bytes_value(map, key)
385 }
386
387 pub fn get_bytes<'a>(&'a self, key: &'a str) -> Result<Vec<u8>, Error> {
388 let map = self.to_map()?;
389 Self::inner_bytes_value(map, key)
390 }
391
392 pub fn get_optional_bytes_into<T: From<Vec<u8>>>(&self, key: &str) -> Result<Option<T>, Error> {
393 let map = self.to_map()?;
394 Ok(Self::inner_optional_bytes_value(map, key)?.map(|bytes| bytes.into()))
395 }
396
397 pub fn get_optional_bytes_try_into<T: TryFrom<Vec<u8>, Error = Error>>(
398 &self,
399 key: &str,
400 ) -> Result<Option<T>, Error> {
401 let map = self.to_map()?;
402 Self::inner_optional_bytes_value(map, key)?
403 .map(|bytes| bytes.try_into())
404 .transpose()
405 }
406
407 pub fn get_bytes_into<T: From<Vec<u8>>>(&self, key: &str) -> Result<T, Error> {
408 let map = self.to_map()?;
409 Ok(Self::inner_bytes_value(map, key)?.into())
410 }
411
412 pub fn get_bytes_try_into<T: TryFrom<Vec<u8>, Error = Error>>(
413 &self,
414 key: &str,
415 ) -> Result<T, Error> {
416 let map = self.to_map()?;
417 Self::inner_bytes_value(map, key)?.try_into()
418 }
419
420 pub fn get_optional_hash256<'a>(&'a self, key: &'a str) -> Result<Option<[u8; 32]>, Error> {
421 let map = self.to_map()?;
422 Self::inner_optional_hash256_value(map, key)
423 }
424
425 pub fn get_identifier<'a>(&'a self, key: &'a str) -> Result<Identifier, Error> {
426 let map = self.to_map()?;
427 Ok(Identifier::new(Self::inner_hash256_value(map, key)?))
428 }
429
430 pub fn get_optional_identifier<'a>(
431 &'a self,
432 key: &'a str,
433 ) -> Result<Option<Identifier>, Error> {
434 let map = self.to_map()?;
435 Ok(Self::inner_optional_hash256_value(map, key)?.map(Identifier::new))
436 }
437
438 pub fn get_hash256<'a>(&'a self, key: &'a str) -> Result<[u8; 32], Error> {
439 let map = self.to_map()?;
440 Self::inner_hash256_value(map, key)
441 }
442
443 pub fn get_hash256_as_bs58_string<'a>(&'a self, key: &'a str) -> Result<String, Error> {
444 let map = self.to_map()?;
445 let value = Self::inner_hash256_value(map, key)?;
446 Ok(bs58::encode(value).into_string())
447 }
448
449 pub fn inner_optional_array_of_strings<'a, I: FromIterator<String>>(
451 document_type: &'a [(Value, Value)],
452 key: &'a str,
453 ) -> Option<I> {
454 let key_value = Self::get_optional_from_map(document_type, key)?;
455 if let Value::Array(key_value) = key_value {
456 Some(
457 key_value
458 .iter()
459 .filter_map(|v| {
460 if let Value::Text(text) = v {
461 Some(text.clone())
462 } else {
463 None
464 }
465 })
466 .collect(),
467 )
468 } else {
469 None
470 }
471 }
472
473 pub fn inner_recursive_optional_array_of_strings<'a>(
486 document_type: &'a [(Value, Value)],
487 prefix: String,
488 recursive_key: &'a str,
489 key: &'a str,
490 ) -> BTreeSet<String> {
491 let mut result = if let Some(Value::Array(key_value)) =
492 Self::get_optional_from_map(document_type, key)
493 {
494 key_value
495 .iter()
496 .filter_map(|v| {
497 if let Value::Text(text) = v {
498 Some(format!("{prefix}{text}"))
499 } else {
500 None
501 }
502 })
503 .collect()
504 } else {
505 BTreeSet::new()
506 };
507 if let Some(Value::Map(lower_level)) =
508 Self::get_optional_from_map(document_type, recursive_key)
509 {
510 for (inner_key, value) in lower_level {
511 let level_prefix = if let Value::Text(text) = inner_key {
512 text.as_str()
513 } else {
514 continue;
515 };
516 let Value::Map(level_map) = value else {
517 continue;
518 };
519
520 let prefix = format!("{prefix}{level_prefix}.");
521 result.extend(Self::inner_recursive_optional_array_of_strings(
522 level_map,
523 prefix,
524 recursive_key,
525 key,
526 ))
527 }
528 }
529 result
530 }
531
532 pub fn inner_optional_array(
534 document_type: &[(Value, Value)],
535 key: &str,
536 ) -> Result<Option<Vec<Value>>, Error> {
537 Self::get_optional_from_map(document_type, key)
538 .map(|value| value.to_array_owned())
539 .transpose()
540 }
541
542 pub fn inner_array_mut_ref<'a>(
544 document_type: &'a mut [(Value, Value)],
545 key: &'a str,
546 ) -> Result<&'a mut Vec<Value>, Error> {
547 Self::get_mut_from_map(document_type, key).map(|value| value.to_array_mut())?
548 }
549
550 pub fn inner_optional_array_mut_ref<'a>(
552 document_type: &'a mut [(Value, Value)],
553 key: &'a str,
554 ) -> Result<Option<&'a mut Vec<Value>>, Error> {
555 Self::get_optional_mut_from_map(document_type, key)
556 .map(|value| value.to_array_mut())
557 .transpose()
558 }
559
560 pub fn inner_array_ref<'a>(
562 document_type: &'a [(Value, Value)],
563 key: &'a str,
564 ) -> Result<&'a Vec<Value>, Error> {
565 Self::get_from_map(document_type, key).map(|value| value.to_array_ref())?
566 }
567
568 pub fn inner_array_owned(
570 document_type: &[(Value, Value)],
571 key: &str,
572 ) -> Result<Vec<Value>, Error> {
573 Self::get_from_map(document_type, key).map(|value| value.to_array_owned())?
574 }
575
576 pub fn inner_optional_array_slice<'a>(
578 document_type: &'a [(Value, Value)],
579 key: &'a str,
580 ) -> Result<Option<&'a [Value]>, Error> {
581 Self::get_optional_from_map(document_type, key)
582 .map(|value| value.to_array_slice())
583 .transpose()
584 }
585
586 pub fn inner_array_slice<'a>(
588 document_type: &'a [(Value, Value)],
589 key: &'a str,
590 ) -> Result<&'a [Value], Error> {
591 Self::get_from_map(document_type, key).map(|value| value.to_array_slice())?
592 }
593
594 pub fn inner_string_ref_map<'a, I: FromIterator<(String, &'a Value)>>(
596 document_type: &'a [(Value, Value)],
597 key: &'a str,
598 ) -> Result<I, Error> {
599 Self::get_from_map(document_type, key).map(|value| value.to_ref_string_map())?
600 }
601
602 pub fn inner_optional_string_ref_map<'a, I: FromIterator<(String, &'a Value)>>(
604 document_type: &'a [(Value, Value)],
605 key: &'a str,
606 ) -> Result<Option<I>, Error> {
607 let Some(key_value) = Self::get_optional_from_map(document_type, key) else {
608 return Ok(None);
609 };
610 if let Value::Map(map_value) = key_value {
611 return Ok(Some(Value::map_ref_into_string_map(map_value)?));
612 }
613 Ok(None)
614 }
615
616 pub fn inner_string_mut_ref_map<'a, I: FromIterator<(String, &'a mut Value)>>(
618 document_type: &'a mut [(Value, Value)],
619 key: &'a str,
620 ) -> Result<I, Error> {
621 Self::get_mut_from_map(document_type, key).map(|value| value.to_ref_string_map_mut())?
622 }
623
624 pub fn inner_optional_string_mut_ref_map<'a, I: FromIterator<(String, &'a mut Value)>>(
626 document_type: &'a mut [(Value, Value)],
627 key: &'a str,
628 ) -> Result<Option<I>, Error> {
629 let Some(key_value) = Self::get_optional_mut_from_map(document_type, key) else {
630 return Ok(None);
631 };
632 Ok(Some(key_value.to_ref_string_map_mut()?))
633 }
634
635 pub fn inner_optional_btree_map<'a>(
637 document_type: &'a [(Value, Value)],
638 key: &'a str,
639 ) -> Result<Option<BTreeMap<String, &'a Value>>, Error> {
640 let Some(key_value) = Self::get_optional_from_map(document_type, key) else {
641 return Ok(None);
642 };
643 if let Value::Map(map_value) = key_value {
644 return Ok(Some(Value::map_ref_into_btree_string_map(map_value)?));
645 }
646 Ok(None)
647 }
648
649 pub fn inner_optional_index_map<'a, T>(
651 document_type: &'a [(Value, Value)],
652 key: &'a str,
653 sort_property: &'a str,
654 ) -> Result<Option<IndexMap<String, &'a Value>>, Error>
655 where
656 T: TryFrom<i128>
657 + TryFrom<u128>
658 + TryFrom<u64>
659 + TryFrom<i64>
660 + TryFrom<u32>
661 + TryFrom<i32>
662 + TryFrom<u16>
663 + TryFrom<i16>
664 + TryFrom<u8>
665 + TryFrom<i8>
666 + Ord,
667 {
668 let Some(key_value) = Self::get_optional_from_map(document_type, key) else {
669 return Ok(None);
670 };
671 if let Value::Map(map_value) = key_value {
672 return Ok(Some(Value::map_ref_into_indexed_string_map::<T>(
673 map_value,
674 sort_property,
675 )?));
676 }
677 Ok(None)
678 }
679
680 pub fn inner_optional_bool_value(
682 document_type: &[(Value, Value)],
683 key: &str,
684 ) -> Result<Option<bool>, Error> {
685 Self::get_optional_from_map(document_type, key)
686 .and_then(|value| {
687 if value.is_null() {
688 None
689 } else {
690 Some(value.to_bool())
691 }
692 })
693 .transpose()
694 }
695
696 pub fn inner_bool_value(document_type: &[(Value, Value)], key: &str) -> Result<bool, Error> {
698 Self::get_from_map(document_type, key).map(|value| value.to_bool())?
699 }
700
701 pub fn inner_optional_integer_value<T>(
703 document_type: &[(Value, Value)],
704 key: &str,
705 ) -> Result<Option<T>, Error>
706 where
707 T: TryFrom<i128>
708 + TryFrom<u128>
709 + TryFrom<u64>
710 + TryFrom<i64>
711 + TryFrom<u32>
712 + TryFrom<i32>
713 + TryFrom<u16>
714 + TryFrom<i16>
715 + TryFrom<u8>
716 + TryFrom<i8>,
717 {
718 Self::get_optional_from_map(document_type, key)
719 .and_then(|key_value| {
720 if key_value.is_null() {
721 None
722 } else {
723 Some(key_value.to_integer())
724 }
725 })
726 .transpose()
727 }
728
729 pub fn inner_integer_value<T>(document_type: &[(Value, Value)], key: &str) -> Result<T, Error>
731 where
732 T: TryFrom<i128>
733 + TryFrom<u128>
734 + TryFrom<u64>
735 + TryFrom<i64>
736 + TryFrom<u32>
737 + TryFrom<i32>
738 + TryFrom<u16>
739 + TryFrom<i16>
740 + TryFrom<u8>
741 + TryFrom<i8>,
742 {
743 let key_value = Self::get_from_map(document_type, key)?;
744 key_value.to_integer()
745 }
746
747 pub fn inner_optional_text_value<'a>(
749 document_type: &'a [(Value, Value)],
750 key: &'a str,
751 ) -> Result<Option<&'a str>, Error> {
752 Self::get_optional_from_map(document_type, key)
753 .map(|v| v.to_str())
754 .transpose()
755 }
756
757 pub fn inner_text_value<'a>(
759 document_type: &'a [(Value, Value)],
760 key: &'a str,
761 ) -> Result<&'a str, Error> {
762 Self::get_from_map(document_type, key).map(|v| v.to_str())?
763 }
764
765 pub fn inner_optional_hash256_value<'a>(
767 document_type: &'a [(Value, Value)],
768 key: &'a str,
769 ) -> Result<Option<[u8; 32]>, Error> {
770 Self::get_optional_from_map(document_type, key)
771 .map(|v| v.to_hash256())
772 .transpose()
773 }
774
775 pub fn inner_hash256_value<'a>(
777 document_type: &'a [(Value, Value)],
778 key: &'a str,
779 ) -> Result<[u8; 32], Error> {
780 Self::get_from_map(document_type, key).map(|v| v.to_hash256())?
781 }
782
783 pub fn inner_optional_binary_data_value<'a>(
785 document_type: &'a [(Value, Value)],
786 key: &'a str,
787 ) -> Result<Option<BinaryData>, Error> {
788 Self::get_optional_from_map(document_type, key)
789 .map(|v| v.to_binary_data())
790 .transpose()
791 }
792
793 pub fn inner_binary_data_value<'a>(
795 document_type: &'a [(Value, Value)],
796 key: &'a str,
797 ) -> Result<BinaryData, Error> {
798 Self::get_from_map(document_type, key).map(|v| v.to_binary_data())?
799 }
800
801 pub fn inner_optional_bytes_value<'a>(
805 document_type: &'a [(Value, Value)],
806 key: &'a str,
807 ) -> Result<Option<Vec<u8>>, Error> {
808 Self::get_optional_from_map(document_type, key)
809 .map(|v| v.to_bytes())
810 .transpose()
811 }
812
813 pub fn inner_bytes_value<'a>(
815 document_type: &'a [(Value, Value)],
816 key: &'a str,
817 ) -> Result<Vec<u8>, Error> {
818 Self::get_from_map(document_type, key).map(|v| v.to_bytes())?
819 }
820
821 pub fn inner_optional_bytes_slice_value<'a>(
823 document_type: &'a [(Value, Value)],
824 key: &'a str,
825 ) -> Result<Option<&'a [u8]>, Error> {
826 Self::get_optional_from_map(document_type, key)
827 .map(|v| v.as_bytes_slice())
828 .transpose()
829 }
830
831 pub fn inner_optional_array_slice_value<'a>(
833 document_type: &'a [(Value, Value)],
834 key: &'a str,
835 ) -> Result<Option<&'a [Value]>, Error> {
836 Self::get_optional_from_map(document_type, key)
837 .map(|v| v.as_slice())
838 .transpose()
839 }
840
841 pub fn get_from_map<'a>(
842 map: &'a [(Value, Value)],
843 search_key: &'a str,
844 ) -> Result<&'a Value, Error> {
845 Self::get_optional_from_map(map, search_key)
846 .ok_or_else(|| Error::StructureError(format!("{} not found in map", search_key)))
847 }
848
849 pub fn get_mut_from_map<'a>(
850 map: &'a mut [(Value, Value)],
851 search_key: &'a str,
852 ) -> Result<&'a mut Value, Error> {
853 Self::get_optional_mut_from_map(map, search_key).ok_or(Error::StructureError(format!(
854 "{} not found in map",
855 search_key
856 )))
857 }
858
859 pub fn get_optional_from_map<'a>(
861 map: &'a [(Value, Value)],
862 search_key: &'a str,
863 ) -> Option<&'a Value> {
864 for (key, value) in map.iter() {
865 if !key.is_text() {
866 continue;
867 }
868
869 if key.as_text().expect("confirmed as text") == search_key {
870 return if value.is_null() { None } else { Some(value) };
871 }
872 }
873 None
874 }
875
876 pub fn get_optional_mut_from_map<'a>(
878 map: &'a mut [(Value, Value)],
879 search_key: &'a str,
880 ) -> Option<&'a mut Value> {
881 for (key, value) in map.iter_mut() {
882 if !key.is_text() {
883 continue;
884 }
885
886 if key.as_text().expect("confirmed as text") == search_key {
887 return if value.is_null() { None } else { Some(value) };
888 }
889 }
890 None
891 }
892
893 pub fn insert_in_map<'a>(
896 map: &'a mut ValueMap,
897 inserting_key: &'a str,
898 inserting_value: Value,
899 ) {
900 let mut found_value = None;
901 for (key, value) in map.iter_mut() {
902 if !key.is_text() {
903 continue;
904 }
905
906 if key.as_text().expect("confirmed as text") == inserting_key {
907 found_value = Some(value);
908 break;
909 }
910 }
911 if let Some(value) = found_value {
912 *value = inserting_value;
913 } else {
914 map.push((Value::Text(inserting_key.to_string()), inserting_value))
915 }
916 }
917
918 pub fn insert_in_map_string_value(
921 map: &mut ValueMap,
922 inserting_key: String,
923 inserting_value: Value,
924 ) {
925 let mut found_value = None;
926 let mut pos = 0;
927 for (key, value) in map.iter_mut() {
928 if let Value::Text(text) = key {
929 match inserting_key.cmp(text) {
930 Ordering::Less => {}
931 Ordering::Equal => {
932 found_value = Some(value);
933 break;
934 }
935 Ordering::Greater => {
936 pos += 1;
937 }
938 }
939 }
940 }
941 if let Some(value) = found_value {
942 *value = inserting_value;
943 } else {
944 map.insert(pos, (Value::Text(inserting_key), inserting_value))
945 }
946 }
947
948 pub fn push_to_map_string_value(
951 map: &mut ValueMap,
952 inserting_key: String,
953 inserting_value: Value,
954 ) {
955 let mut found_value = None;
956 for (key, value) in map.iter_mut() {
957 if !key.is_text() {
958 continue;
959 }
960
961 if key.as_text().expect("confirmed as text") == inserting_key {
962 found_value = Some(value);
963 break;
964 }
965 }
966 if let Some(value) = found_value {
967 *value = inserting_value;
968 } else {
969 map.push((Value::Text(inserting_key), inserting_value))
970 }
971 }
972}
973
974#[cfg(test)]
975mod tests {
976 use super::*;
977
978 fn make_map(entries: Vec<(&str, Value)>) -> Value {
980 Value::Map(
981 entries
982 .into_iter()
983 .map(|(k, v)| (Value::Text(k.to_string()), v))
984 .collect(),
985 )
986 }
987
988 #[test]
993 fn has_returns_true_for_existing_key() {
994 let val = make_map(vec![("name", Value::Text("alice".into()))]);
995 assert!(val.has("name").unwrap());
996 }
997
998 #[test]
999 fn has_returns_false_for_missing_key() {
1000 let val = make_map(vec![("name", Value::Text("alice".into()))]);
1001 assert!(!val.has("age").unwrap());
1002 }
1003
1004 #[test]
1005 fn has_errors_on_non_map() {
1006 let val = Value::U64(42);
1007 assert!(val.has("key").is_err());
1008 }
1009
1010 #[test]
1011 fn get_returns_some_for_existing_key() {
1012 let val = make_map(vec![("x", Value::U32(7))]);
1013 let result = val.get("x").unwrap();
1014 assert_eq!(result, Some(&Value::U32(7)));
1015 }
1016
1017 #[test]
1018 fn get_returns_none_for_missing_key() {
1019 let val = make_map(vec![("x", Value::U32(7))]);
1020 assert_eq!(val.get("y").unwrap(), None);
1021 }
1022
1023 #[test]
1024 fn get_mut_returns_mutable_ref() {
1025 let mut val = make_map(vec![("x", Value::U32(7))]);
1026 let inner = val.get_mut("x").unwrap().unwrap();
1027 *inner = Value::U32(99);
1028 assert_eq!(val.get("x").unwrap(), Some(&Value::U32(99)));
1029 }
1030
1031 #[test]
1032 fn get_value_returns_ref() {
1033 let val = make_map(vec![("k", Value::Bool(true))]);
1034 assert_eq!(val.get_value("k").unwrap(), &Value::Bool(true));
1035 }
1036
1037 #[test]
1038 fn get_value_errors_on_missing_key() {
1039 let val = make_map(vec![("k", Value::Bool(true))]);
1040 assert!(val.get_value("missing").is_err());
1041 }
1042
1043 #[test]
1044 fn get_value_mut_modifies_value() {
1045 let mut val = make_map(vec![("k", Value::Bool(true))]);
1046 let inner = val.get_value_mut("k").unwrap();
1047 *inner = Value::Bool(false);
1048 assert_eq!(val.get_value("k").unwrap(), &Value::Bool(false));
1049 }
1050
1051 #[test]
1056 fn set_into_value_adds_new_key() {
1057 let mut val = make_map(vec![]);
1058 val.set_into_value("age", 42u64).unwrap();
1059 let result: u64 = val.get_integer("age").unwrap();
1060 assert_eq!(result, 42);
1061 }
1062
1063 #[test]
1064 fn set_into_value_replaces_existing() {
1065 let mut val = make_map(vec![("age", Value::U64(10))]);
1066 val.set_into_value("age", 42u64).unwrap();
1067 let result: u64 = val.get_integer("age").unwrap();
1068 assert_eq!(result, 42);
1069 }
1070
1071 #[test]
1072 fn set_into_binary_data_works() {
1073 let mut val = make_map(vec![]);
1074 val.set_into_binary_data("data", vec![1, 2, 3]).unwrap();
1075 assert_eq!(val.get_value("data").unwrap(), &Value::Bytes(vec![1, 2, 3]));
1076 }
1077
1078 #[test]
1079 fn set_value_works() {
1080 let mut val = make_map(vec![]);
1081 val.set_value("flag", Value::Bool(true)).unwrap();
1082 assert_eq!(val.get_value("flag").unwrap(), &Value::Bool(true));
1083 }
1084
1085 #[test]
1086 fn insert_sorted_position() {
1087 let mut val = make_map(vec![("b", Value::U8(2))]);
1088 val.insert("a".to_string(), Value::U8(1)).unwrap();
1089 assert_eq!(val.get_value("a").unwrap(), &Value::U8(1));
1091 }
1092
1093 #[test]
1094 fn insert_at_end_appends() {
1095 let mut val = make_map(vec![("a", Value::U8(1))]);
1096 val.insert_at_end("z".to_string(), Value::U8(26)).unwrap();
1097 assert_eq!(val.get_value("z").unwrap(), &Value::U8(26));
1098 }
1099
1100 #[test]
1105 fn remove_existing_key() {
1106 let mut val = make_map(vec![("k", Value::U64(1))]);
1107 let removed = val.remove("k").unwrap();
1108 assert_eq!(removed, Value::U64(1));
1109 assert!(!val.has("k").unwrap());
1110 }
1111
1112 #[test]
1113 fn remove_missing_key_errors() {
1114 let mut val = make_map(vec![("k", Value::U64(1))]);
1115 assert!(val.remove("missing").is_err());
1116 }
1117
1118 #[test]
1119 fn remove_many_removes_all() {
1120 let mut val = make_map(vec![
1121 ("a", Value::U8(1)),
1122 ("b", Value::U8(2)),
1123 ("c", Value::U8(3)),
1124 ]);
1125 val.remove_many(&["a", "c"]).unwrap();
1126 assert!(!val.has("a").unwrap());
1127 assert!(val.has("b").unwrap());
1128 assert!(!val.has("c").unwrap());
1129 }
1130
1131 #[test]
1132 fn remove_optional_value_returns_some() {
1133 let mut val = make_map(vec![("k", Value::U64(1))]);
1134 let result = val.remove_optional_value("k").unwrap();
1135 assert_eq!(result, Some(Value::U64(1)));
1136 }
1137
1138 #[test]
1139 fn remove_optional_value_returns_none_for_missing() {
1140 let mut val = make_map(vec![("k", Value::U64(1))]);
1141 let result = val.remove_optional_value("missing").unwrap();
1142 assert_eq!(result, None);
1143 }
1144
1145 #[test]
1146 fn remove_optional_value_if_null_removes_null() {
1147 let mut val = make_map(vec![("k", Value::Null)]);
1148 val.remove_optional_value_if_null("k").unwrap();
1150 }
1151
1152 #[test]
1153 fn remove_optional_value_if_empty_array_removes() {
1154 let mut val = make_map(vec![("k", Value::Array(vec![]))]);
1155 val.remove_optional_value_if_empty_array("k").unwrap();
1156 }
1157
1158 #[test]
1163 fn remove_integer_works() {
1164 let mut val = make_map(vec![("n", Value::U64(42))]);
1165 let result: u64 = val.remove_integer("n").unwrap();
1166 assert_eq!(result, 42);
1167 }
1168
1169 #[test]
1170 fn remove_optional_integer_some() {
1171 let mut val = make_map(vec![("n", Value::U32(99))]);
1172 let result: Option<u32> = val.remove_optional_integer("n").unwrap();
1173 assert_eq!(result, Some(99));
1174 }
1175
1176 #[test]
1177 fn remove_optional_integer_none_for_missing() {
1178 let mut val = make_map(vec![("n", Value::U32(99))]);
1179 let result: Option<u32> = val.remove_optional_integer("missing").unwrap();
1180 assert_eq!(result, None);
1181 }
1182
1183 #[test]
1188 fn remove_identifier_works() {
1189 let id_bytes = [7u8; 32];
1190 let mut val = make_map(vec![("id", Value::Identifier(id_bytes))]);
1191 let result = val.remove_identifier("id").unwrap();
1192 assert_eq!(result, Identifier::new(id_bytes));
1193 }
1194
1195 #[test]
1196 fn remove_optional_identifier_some() {
1197 let id_bytes = [8u8; 32];
1198 let mut val = make_map(vec![("id", Value::Identifier(id_bytes))]);
1199 let result = val.remove_optional_identifier("id").unwrap();
1200 assert_eq!(result, Some(Identifier::new(id_bytes)));
1201 }
1202
1203 #[test]
1204 fn remove_optional_identifier_none_for_missing() {
1205 let mut val = make_map(vec![("other", Value::U8(0))]);
1206 let result = val.remove_optional_identifier("id").unwrap();
1207 assert_eq!(result, None);
1208 }
1209
1210 #[test]
1215 fn remove_bytes_32_works() {
1216 let data = [3u8; 32];
1217 let mut val = make_map(vec![("b", Value::Bytes32(data))]);
1218 let result = val.remove_bytes_32("b").unwrap();
1219 assert_eq!(result, Bytes32::new(data));
1220 }
1221
1222 #[test]
1223 fn remove_optional_bytes_32_returns_some() {
1224 let data = [4u8; 32];
1225 let mut val = make_map(vec![("b", Value::Bytes32(data))]);
1226 let result = val.remove_optional_bytes_32("b").unwrap();
1227 assert_eq!(result, Some(Bytes32::new(data)));
1228 }
1229
1230 #[test]
1231 fn remove_optional_bytes_32_returns_none_for_missing() {
1232 let mut val = make_map(vec![]);
1233 let result = val.remove_optional_bytes_32("b").unwrap();
1234 assert_eq!(result, None);
1235 }
1236
1237 #[test]
1242 fn remove_hash256_bytes_works() {
1243 let data = [9u8; 32];
1244 let mut val = make_map(vec![("h", Value::Bytes32(data))]);
1245 let result = val.remove_hash256_bytes("h").unwrap();
1246 assert_eq!(result, data);
1247 }
1248
1249 #[test]
1250 fn remove_optional_hash256_bytes_some() {
1251 let data = [10u8; 32];
1252 let mut val = make_map(vec![("h", Value::Bytes32(data))]);
1253 let result = val.remove_optional_hash256_bytes("h").unwrap();
1254 assert_eq!(result, Some(data));
1255 }
1256
1257 #[test]
1258 fn remove_optional_hash256_bytes_none() {
1259 let mut val = make_map(vec![]);
1260 let result = val.remove_optional_hash256_bytes("h").unwrap();
1261 assert_eq!(result, None);
1262 }
1263
1264 #[test]
1269 fn remove_bytes_works() {
1270 let mut val = make_map(vec![("b", Value::Bytes(vec![1, 2, 3]))]);
1271 let result = val.remove_bytes("b").unwrap();
1272 assert_eq!(result, vec![1, 2, 3]);
1273 }
1274
1275 #[test]
1276 fn remove_optional_bytes_some() {
1277 let mut val = make_map(vec![("b", Value::Bytes(vec![4, 5]))]);
1278 let result = val.remove_optional_bytes("b").unwrap();
1279 assert_eq!(result, Some(vec![4, 5]));
1280 }
1281
1282 #[test]
1283 fn remove_optional_bytes_none_for_missing() {
1284 let mut val = make_map(vec![]);
1285 let result = val.remove_optional_bytes("b").unwrap();
1286 assert_eq!(result, None);
1287 }
1288
1289 #[test]
1294 fn remove_binary_data_works() {
1295 let mut val = make_map(vec![("d", Value::Bytes(vec![10, 20]))]);
1296 let result = val.remove_binary_data("d").unwrap();
1297 assert_eq!(result, BinaryData::new(vec![10, 20]));
1298 }
1299
1300 #[test]
1301 fn remove_optional_binary_data_some() {
1302 let mut val = make_map(vec![("d", Value::Bytes(vec![30]))]);
1303 let result = val.remove_optional_binary_data("d").unwrap();
1304 assert_eq!(result, Some(BinaryData::new(vec![30])));
1305 }
1306
1307 #[test]
1308 fn remove_optional_binary_data_none() {
1309 let mut val = make_map(vec![]);
1310 let result = val.remove_optional_binary_data("d").unwrap();
1311 assert_eq!(result, None);
1312 }
1313
1314 #[test]
1319 fn remove_array_works() {
1320 let mut val = make_map(vec![(
1321 "arr",
1322 Value::Array(vec![Value::U8(1), Value::U8(2)]),
1323 )]);
1324 let result = val.remove_array("arr").unwrap();
1325 assert_eq!(result, vec![Value::U8(1), Value::U8(2)]);
1326 }
1327
1328 #[test]
1329 fn remove_optional_array_some() {
1330 let mut val = make_map(vec![("arr", Value::Array(vec![Value::U8(3)]))]);
1331 let result = val.remove_optional_array("arr").unwrap();
1332 assert_eq!(result, Some(vec![Value::U8(3)]));
1333 }
1334
1335 #[test]
1336 fn remove_optional_array_none() {
1337 let mut val = make_map(vec![]);
1338 let result = val.remove_optional_array("arr").unwrap();
1339 assert_eq!(result, None);
1340 }
1341
1342 #[test]
1347 fn get_integer_works() {
1348 let val = make_map(vec![("n", Value::I64(-5))]);
1349 let result: i64 = val.get_integer("n").unwrap();
1350 assert_eq!(result, -5);
1351 }
1352
1353 #[test]
1354 fn get_integer_wrong_type() {
1355 let val = make_map(vec![("n", Value::Text("hello".into()))]);
1356 let result: Result<i64, Error> = val.get_integer("n");
1357 assert!(result.is_err());
1358 }
1359
1360 #[test]
1361 fn get_optional_integer_some() {
1362 let val = make_map(vec![("n", Value::U16(100))]);
1363 let result: Option<u16> = val.get_optional_integer("n").unwrap();
1364 assert_eq!(result, Some(100));
1365 }
1366
1367 #[test]
1368 fn get_optional_integer_none_for_missing() {
1369 let val = make_map(vec![]);
1370 let result: Option<u16> = val.get_optional_integer("n").unwrap();
1371 assert_eq!(result, None);
1372 }
1373
1374 #[test]
1379 fn get_str_works() {
1380 let val = make_map(vec![("name", Value::Text("bob".into()))]);
1381 assert_eq!(val.get_str("name").unwrap(), "bob");
1382 }
1383
1384 #[test]
1385 fn get_str_wrong_type() {
1386 let val = make_map(vec![("name", Value::U64(5))]);
1387 assert!(val.get_str("name").is_err());
1388 }
1389
1390 #[test]
1391 fn get_optional_str_some() {
1392 let val = make_map(vec![("name", Value::Text("alice".into()))]);
1393 assert_eq!(val.get_optional_str("name").unwrap(), Some("alice"));
1394 }
1395
1396 #[test]
1397 fn get_optional_str_none_for_missing() {
1398 let val = make_map(vec![]);
1399 assert_eq!(val.get_optional_str("name").unwrap(), None);
1400 }
1401
1402 #[test]
1407 fn get_bool_works() {
1408 let val = make_map(vec![("flag", Value::Bool(true))]);
1409 assert!(val.get_bool("flag").unwrap());
1410 }
1411
1412 #[test]
1413 fn get_bool_wrong_type() {
1414 let val = make_map(vec![("flag", Value::U8(1))]);
1415 assert!(val.get_bool("flag").is_err());
1416 }
1417
1418 #[test]
1419 fn get_optional_bool_some() {
1420 let val = make_map(vec![("flag", Value::Bool(false))]);
1421 assert_eq!(val.get_optional_bool("flag").unwrap(), Some(false));
1422 }
1423
1424 #[test]
1425 fn get_optional_bool_none_for_missing() {
1426 let val = make_map(vec![]);
1427 assert_eq!(val.get_optional_bool("flag").unwrap(), None);
1428 }
1429
1430 #[test]
1435 fn get_array_works() {
1436 let val = make_map(vec![("a", Value::Array(vec![Value::U8(1)]))]);
1437 let result = val.get_array("a").unwrap();
1438 assert_eq!(result, vec![Value::U8(1)]);
1439 }
1440
1441 #[test]
1442 fn get_optional_array_some() {
1443 let val = make_map(vec![("a", Value::Array(vec![Value::U8(2)]))]);
1444 let result = val.get_optional_array("a").unwrap();
1445 assert_eq!(result, Some(vec![Value::U8(2)]));
1446 }
1447
1448 #[test]
1449 fn get_optional_array_none() {
1450 let val = make_map(vec![]);
1451 let result = val.get_optional_array("a").unwrap();
1452 assert_eq!(result, None);
1453 }
1454
1455 #[test]
1456 fn get_array_ref_works() {
1457 let val = make_map(vec![("a", Value::Array(vec![Value::U8(3)]))]);
1458 let result = val.get_array_ref("a").unwrap();
1459 assert_eq!(result, &vec![Value::U8(3)]);
1460 }
1461
1462 #[test]
1463 fn get_array_slice_works() {
1464 let val = make_map(vec![("a", Value::Array(vec![Value::U8(4)]))]);
1465 let result = val.get_array_slice("a").unwrap();
1466 assert_eq!(result, &[Value::U8(4)]);
1467 }
1468
1469 #[test]
1470 fn get_optional_array_slice_some() {
1471 let val = make_map(vec![("a", Value::Array(vec![Value::U8(5)]))]);
1472 let result = val.get_optional_array_slice("a").unwrap();
1473 assert_eq!(result, Some([Value::U8(5)].as_slice()));
1474 }
1475
1476 #[test]
1477 fn get_optional_array_slice_none() {
1478 let val = make_map(vec![]);
1479 let result = val.get_optional_array_slice("a").unwrap();
1480 assert_eq!(result, None);
1481 }
1482
1483 #[test]
1484 fn get_array_mut_ref_works() {
1485 let mut val = make_map(vec![("a", Value::Array(vec![Value::U8(6)]))]);
1486 let arr = val.get_array_mut_ref("a").unwrap();
1487 arr.push(Value::U8(7));
1488 assert_eq!(
1489 val.get_array("a").unwrap(),
1490 vec![Value::U8(6), Value::U8(7)]
1491 );
1492 }
1493
1494 #[test]
1495 fn get_optional_array_mut_ref_some() {
1496 let mut val = make_map(vec![("a", Value::Array(vec![Value::U8(8)]))]);
1497 let result = val.get_optional_array_mut_ref("a").unwrap();
1498 assert!(result.is_some());
1499 }
1500
1501 #[test]
1502 fn get_optional_array_mut_ref_none() {
1503 let mut val = make_map(vec![]);
1504 let result = val.get_optional_array_mut_ref("a").unwrap();
1505 assert!(result.is_none());
1506 }
1507
1508 #[test]
1513 fn get_binary_data_works() {
1514 let val = make_map(vec![("d", Value::Bytes(vec![1, 2]))]);
1515 let result = val.get_binary_data("d").unwrap();
1516 assert_eq!(result, BinaryData::new(vec![1, 2]));
1517 }
1518
1519 #[test]
1520 fn get_optional_binary_data_some() {
1521 let val = make_map(vec![("d", Value::Bytes(vec![3]))]);
1522 let result = val.get_optional_binary_data("d").unwrap();
1523 assert_eq!(result, Some(BinaryData::new(vec![3])));
1524 }
1525
1526 #[test]
1527 fn get_optional_binary_data_none() {
1528 let val = make_map(vec![]);
1529 let result = val.get_optional_binary_data("d").unwrap();
1530 assert_eq!(result, None);
1531 }
1532
1533 #[test]
1538 fn get_bytes_works() {
1539 let val = make_map(vec![("b", Value::Bytes(vec![10, 11]))]);
1540 let result = val.get_bytes("b").unwrap();
1541 assert_eq!(result, vec![10, 11]);
1542 }
1543
1544 #[test]
1545 fn get_optional_bytes_some() {
1546 let val = make_map(vec![("b", Value::Bytes(vec![12]))]);
1547 let result = val.get_optional_bytes("b").unwrap();
1548 assert_eq!(result, Some(vec![12]));
1549 }
1550
1551 #[test]
1552 fn get_optional_bytes_none() {
1553 let val = make_map(vec![]);
1554 let result = val.get_optional_bytes("b").unwrap();
1555 assert_eq!(result, None);
1556 }
1557
1558 #[test]
1563 fn get_hash256_works() {
1564 let data = [5u8; 32];
1565 let val = make_map(vec![("h", Value::Bytes32(data))]);
1566 let result = val.get_hash256("h").unwrap();
1567 assert_eq!(result, data);
1568 }
1569
1570 #[test]
1571 fn get_optional_hash256_some() {
1572 let data = [6u8; 32];
1573 let val = make_map(vec![("h", Value::Bytes32(data))]);
1574 let result = val.get_optional_hash256("h").unwrap();
1575 assert_eq!(result, Some(data));
1576 }
1577
1578 #[test]
1579 fn get_optional_hash256_none() {
1580 let val = make_map(vec![]);
1581 let result = val.get_optional_hash256("h").unwrap();
1582 assert_eq!(result, None);
1583 }
1584
1585 #[test]
1586 fn get_identifier_works() {
1587 let data = [11u8; 32];
1588 let val = make_map(vec![("id", Value::Identifier(data))]);
1589 let result = val.get_identifier("id").unwrap();
1590 assert_eq!(result, Identifier::new(data));
1591 }
1592
1593 #[test]
1594 fn get_optional_identifier_some() {
1595 let data = [12u8; 32];
1596 let val = make_map(vec![("id", Value::Identifier(data))]);
1597 let result = val.get_optional_identifier("id").unwrap();
1598 assert_eq!(result, Some(Identifier::new(data)));
1599 }
1600
1601 #[test]
1602 fn get_optional_identifier_none() {
1603 let val = make_map(vec![]);
1604 let result = val.get_optional_identifier("id").unwrap();
1605 assert_eq!(result, None);
1606 }
1607
1608 #[test]
1609 fn get_hash256_as_bs58_string_works() {
1610 let data = [1u8; 32];
1611 let val = make_map(vec![("h", Value::Bytes32(data))]);
1612 let result = val.get_hash256_as_bs58_string("h").unwrap();
1613 let expected = bs58::encode(data).into_string();
1614 assert_eq!(result, expected);
1615 }
1616
1617 #[test]
1622 fn get_string_ref_map_works() {
1623 let inner = Value::Map(vec![(Value::Text("nested_key".into()), Value::U8(42))]);
1624 let val = make_map(vec![("m", inner)]);
1625 let result: BTreeMap<String, &Value> = val.get_string_ref_map("m").unwrap();
1626 assert_eq!(result.get("nested_key"), Some(&&Value::U8(42)));
1627 }
1628
1629 #[test]
1630 fn get_optional_string_ref_map_some() {
1631 let inner = Value::Map(vec![(Value::Text("k".into()), Value::Bool(true))]);
1632 let val = make_map(vec![("m", inner)]);
1633 let result: Option<BTreeMap<String, &Value>> =
1634 val.get_optional_string_ref_map("m").unwrap();
1635 assert!(result.is_some());
1636 }
1637
1638 #[test]
1639 fn get_optional_string_ref_map_none_for_missing() {
1640 let val = make_map(vec![]);
1641 let result: Option<BTreeMap<String, &Value>> =
1642 val.get_optional_string_ref_map("m").unwrap();
1643 assert!(result.is_none());
1644 }
1645
1646 #[test]
1651 fn get_string_mut_ref_map_works() {
1652 let inner = Value::Map(vec![(Value::Text("nested".into()), Value::U8(1))]);
1653 let mut val = make_map(vec![("m", inner)]);
1654 let result: BTreeMap<String, &mut Value> = val.get_string_mut_ref_map("m").unwrap();
1655 assert!(result.contains_key("nested"));
1656 }
1657
1658 #[test]
1659 fn get_optional_string_mut_ref_map_some() {
1660 let inner = Value::Map(vec![(Value::Text("k".into()), Value::Bool(true))]);
1661 let mut val = make_map(vec![("m", inner)]);
1662 let result: Option<BTreeMap<String, &mut Value>> =
1663 val.get_optional_string_mut_ref_map("m").unwrap();
1664 assert!(result.is_some());
1665 }
1666
1667 #[test]
1668 fn get_optional_string_mut_ref_map_none_for_missing() {
1669 let mut val = make_map(vec![]);
1670 let result: Option<BTreeMap<String, &mut Value>> =
1671 val.get_optional_string_mut_ref_map("m").unwrap();
1672 assert!(result.is_none());
1673 }
1674
1675 #[test]
1680 fn get_optional_bytes_into_some() {
1681 let val = make_map(vec![("b", Value::Bytes(vec![1, 2, 3]))]);
1682 let result: Option<BinaryData> = val.get_optional_bytes_into("b").unwrap();
1683 assert_eq!(result, Some(BinaryData::new(vec![1, 2, 3])));
1684 }
1685
1686 #[test]
1687 fn get_optional_bytes_into_none() {
1688 let val = make_map(vec![]);
1689 let result: Option<BinaryData> = val.get_optional_bytes_into("b").unwrap();
1690 assert_eq!(result, None);
1691 }
1692
1693 #[test]
1694 fn get_bytes_into_works() {
1695 let val = make_map(vec![("b", Value::Bytes(vec![4, 5]))]);
1696 let result: BinaryData = val.get_bytes_into("b").unwrap();
1697 assert_eq!(result, BinaryData::new(vec![4, 5]));
1698 }
1699
1700 #[test]
1705 fn inner_optional_array_of_strings_returns_some() {
1706 let map: Vec<(Value, Value)> = vec![(
1707 Value::Text("strs".into()),
1708 Value::Array(vec![Value::Text("a".into()), Value::Text("b".into())]),
1709 )];
1710 let result: Option<Vec<String>> = Value::inner_optional_array_of_strings(&map, "strs");
1711 assert_eq!(result, Some(vec!["a".to_string(), "b".to_string()]));
1712 }
1713
1714 #[test]
1715 fn inner_optional_array_of_strings_returns_none_for_missing() {
1716 let map: Vec<(Value, Value)> = vec![];
1717 let result: Option<Vec<String>> = Value::inner_optional_array_of_strings(&map, "strs");
1718 assert_eq!(result, None);
1719 }
1720
1721 #[test]
1722 fn inner_optional_array_of_strings_returns_none_for_non_array() {
1723 let map: Vec<(Value, Value)> = vec![(Value::Text("strs".into()), Value::U8(1))];
1724 let result: Option<Vec<String>> = Value::inner_optional_array_of_strings(&map, "strs");
1725 assert_eq!(result, None);
1726 }
1727
1728 #[test]
1733 fn inner_recursive_optional_array_of_strings_basic() {
1734 let map: Vec<(Value, Value)> = vec![(
1735 Value::Text("required".into()),
1736 Value::Array(vec![Value::Text("field1".into())]),
1737 )];
1738 let result = Value::inner_recursive_optional_array_of_strings(
1739 &map,
1740 "".to_string(),
1741 "properties",
1742 "required",
1743 );
1744 assert!(result.contains("field1"));
1745 }
1746
1747 #[test]
1748 fn inner_recursive_optional_array_of_strings_recursive() {
1749 let inner_map: Vec<(Value, Value)> = vec![(
1750 Value::Text("required".into()),
1751 Value::Array(vec![Value::Text("sub_field".into())]),
1752 )];
1753 let map: Vec<(Value, Value)> = vec![
1754 (
1755 Value::Text("required".into()),
1756 Value::Array(vec![Value::Text("top_field".into())]),
1757 ),
1758 (
1759 Value::Text("properties".into()),
1760 Value::Map(vec![(Value::Text("nested".into()), Value::Map(inner_map))]),
1761 ),
1762 ];
1763 let result = Value::inner_recursive_optional_array_of_strings(
1764 &map,
1765 "".to_string(),
1766 "properties",
1767 "required",
1768 );
1769 assert!(result.contains("top_field"));
1770 assert!(result.contains("nested.sub_field"));
1771 }
1772
1773 #[test]
1778 fn get_from_map_returns_value() {
1779 let map: Vec<(Value, Value)> = vec![(Value::Text("k".into()), Value::U64(99))];
1780 let result = Value::get_from_map(&map, "k").unwrap();
1781 assert_eq!(result, &Value::U64(99));
1782 }
1783
1784 #[test]
1785 fn get_from_map_errors_for_missing() {
1786 let map: Vec<(Value, Value)> = vec![];
1787 assert!(Value::get_from_map(&map, "k").is_err());
1788 }
1789
1790 #[test]
1791 fn get_optional_from_map_returns_none_for_null() {
1792 let map: Vec<(Value, Value)> = vec![(Value::Text("k".into()), Value::Null)];
1793 let result = Value::get_optional_from_map(&map, "k");
1794 assert!(result.is_none());
1795 }
1796
1797 #[test]
1798 fn get_optional_from_map_skips_non_text_keys() {
1799 let map: Vec<(Value, Value)> = vec![(Value::U8(1), Value::U64(99))];
1800 let result = Value::get_optional_from_map(&map, "1");
1801 assert!(result.is_none());
1802 }
1803
1804 #[test]
1805 fn get_mut_from_map_works() {
1806 let mut map: Vec<(Value, Value)> = vec![(Value::Text("k".into()), Value::U64(1))];
1807 let result = Value::get_mut_from_map(&mut map, "k").unwrap();
1808 *result = Value::U64(2);
1809 assert_eq!(map[0].1, Value::U64(2));
1810 }
1811
1812 #[test]
1813 fn get_optional_mut_from_map_returns_none_for_null() {
1814 let mut map: Vec<(Value, Value)> = vec![(Value::Text("k".into()), Value::Null)];
1815 let result = Value::get_optional_mut_from_map(&mut map, "k");
1816 assert!(result.is_none());
1817 }
1818
1819 #[test]
1824 fn insert_in_map_adds_new_entry() {
1825 let mut map: Vec<(Value, Value)> = vec![];
1826 Value::insert_in_map(&mut map, "key", Value::U8(1));
1827 assert_eq!(map.len(), 1);
1828 assert_eq!(map[0].1, Value::U8(1));
1829 }
1830
1831 #[test]
1832 fn insert_in_map_replaces_existing() {
1833 let mut map: Vec<(Value, Value)> = vec![(Value::Text("key".into()), Value::U8(1))];
1834 Value::insert_in_map(&mut map, "key", Value::U8(2));
1835 assert_eq!(map.len(), 1);
1836 assert_eq!(map[0].1, Value::U8(2));
1837 }
1838
1839 #[test]
1840 fn insert_in_map_string_value_adds_new() {
1841 let mut map: Vec<(Value, Value)> = vec![];
1842 Value::insert_in_map_string_value(&mut map, "b".to_string(), Value::U8(2));
1843 assert_eq!(map.len(), 1);
1844 }
1845
1846 #[test]
1847 fn insert_in_map_string_value_replaces_existing() {
1848 let mut map: Vec<(Value, Value)> = vec![(Value::Text("b".into()), Value::U8(1))];
1849 Value::insert_in_map_string_value(&mut map, "b".to_string(), Value::U8(99));
1850 assert_eq!(map.len(), 1);
1851 assert_eq!(map[0].1, Value::U8(99));
1852 }
1853
1854 #[test]
1855 fn push_to_map_string_value_adds_new() {
1856 let mut map: Vec<(Value, Value)> = vec![];
1857 Value::push_to_map_string_value(&mut map, "z".to_string(), Value::U8(26));
1858 assert_eq!(map.len(), 1);
1859 assert_eq!(map[0].1, Value::U8(26));
1860 }
1861
1862 #[test]
1863 fn push_to_map_string_value_replaces_existing() {
1864 let mut map: Vec<(Value, Value)> = vec![(Value::Text("z".into()), Value::U8(1))];
1865 Value::push_to_map_string_value(&mut map, "z".to_string(), Value::U8(100));
1866 assert_eq!(map.len(), 1);
1867 assert_eq!(map[0].1, Value::U8(100));
1868 }
1869
1870 #[test]
1875 fn get_str_on_non_map_errors() {
1876 let val = Value::U64(42);
1877 assert!(val.get_str("key").is_err());
1878 }
1879
1880 #[test]
1881 fn get_bool_on_non_map_errors() {
1882 let val = Value::U64(42);
1883 assert!(val.get_bool("key").is_err());
1884 }
1885
1886 #[test]
1887 fn get_bytes_on_non_map_errors() {
1888 let val = Value::U64(42);
1889 assert!(val.get_bytes("key").is_err());
1890 }
1891
1892 #[test]
1893 fn remove_on_non_map_errors() {
1894 let mut val = Value::U64(42);
1895 assert!(val.remove("key").is_err());
1896 }
1897
1898 #[test]
1899 fn set_value_on_non_map_errors() {
1900 let mut val = Value::U64(42);
1901 assert!(val.set_value("key", Value::Bool(true)).is_err());
1902 }
1903
1904 #[test]
1905 fn insert_on_non_map_errors() {
1906 let mut val = Value::U64(42);
1907 assert!(val.insert("key".to_string(), Value::Bool(true)).is_err());
1908 }
1909}