platform_value/btreemap_extensions/
btreemap_mut_value_extensions.rs

1use crate::{Error, Value};
2use std::borrow::BorrowMut;
3use std::collections::BTreeMap;
4
5pub trait BTreeMutValueMapHelper {
6    fn get_optional_inner_map_in_array_mut<
7        'a,
8        M: FromIterator<(String, &'a mut Value)>,
9        I: FromIterator<M>,
10    >(
11        &'a mut self,
12        key: &str,
13    ) -> Result<Option<I>, Error>;
14    fn get_inner_map_in_array_mut<
15        'a,
16        M: FromIterator<(String, &'a mut Value)>,
17        I: FromIterator<M>,
18    >(
19        &'a mut self,
20        key: &str,
21    ) -> Result<I, Error>;
22}
23
24impl<V> BTreeMutValueMapHelper for BTreeMap<String, V>
25where
26    V: BorrowMut<Value>,
27{
28    fn get_optional_inner_map_in_array_mut<
29        'a,
30        M: FromIterator<(String, &'a mut Value)>,
31        I: FromIterator<M>,
32    >(
33        &'a mut self,
34        key: &str,
35    ) -> Result<Option<I>, Error> {
36        self.get_mut(key)
37            .map(|v| {
38                v.borrow_mut()
39                    .as_array_mut()
40                    .map(|vec| {
41                        vec.iter_mut()
42                            .map(|v| v.to_ref_string_map_mut::<M>())
43                            .collect::<Result<I, Error>>()
44                    })
45                    .ok_or_else(|| Error::StructureError(format!("{key} must be a an array")))
46            })
47            .transpose()?
48            .transpose()
49    }
50
51    fn get_inner_map_in_array_mut<
52        'a,
53        M: FromIterator<(String, &'a mut Value)>,
54        I: FromIterator<M>,
55    >(
56        &'a mut self,
57        key: &str,
58    ) -> Result<I, Error> {
59        self.get_optional_inner_map_in_array_mut(key)?
60            .ok_or_else(|| {
61                Error::StructureError(format!("unable to get inner value array property {key}"))
62            })
63    }
64}