Skip to main content

dpp/document/v0/
platform_value_conversion.rs

1use crate::document::serialization_traits::DocumentPlatformValueMethodsV0;
2use crate::document::DocumentV0;
3use crate::ProtocolError;
4use platform_value::Value;
5use std::collections::BTreeMap;
6
7impl DocumentPlatformValueMethodsV0<'_> for DocumentV0 {
8    fn to_map_value(&self) -> Result<BTreeMap<String, Value>, ProtocolError> {
9        Ok(platform_value::to_value(self)?.into_btree_string_map()?)
10    }
11
12    fn into_map_value(self) -> Result<BTreeMap<String, Value>, ProtocolError> {
13        Ok(platform_value::to_value(self)?.into_btree_string_map()?)
14    }
15}
16
17#[cfg(test)]
18mod tests {
19    use super::*;
20    use crate::document::property_names;
21    use platform_value::Identifier;
22
23    fn minimal_doc() -> DocumentV0 {
24        DocumentV0 {
25            id: Identifier::new([1u8; 32]),
26            owner_id: Identifier::new([2u8; 32]),
27            properties: BTreeMap::new(),
28            revision: None,
29            created_at: None,
30            updated_at: None,
31            transferred_at: None,
32            created_at_block_height: None,
33            updated_at_block_height: None,
34            transferred_at_block_height: None,
35            created_at_core_block_height: None,
36            updated_at_core_block_height: None,
37            transferred_at_core_block_height: None,
38            creator_id: None,
39        }
40    }
41
42    fn full_doc() -> DocumentV0 {
43        let mut props = BTreeMap::new();
44        props.insert("name".into(), Value::Text("Eve".into()));
45        props.insert("score".into(), Value::U64(42));
46        DocumentV0 {
47            id: Identifier::new([7u8; 32]),
48            owner_id: Identifier::new([8u8; 32]),
49            properties: props,
50            revision: Some(3),
51            created_at: Some(1_700_000_000_000),
52            updated_at: Some(1_700_000_100_000),
53            transferred_at: Some(1_700_000_200_000),
54            created_at_block_height: Some(10),
55            updated_at_block_height: Some(20),
56            transferred_at_block_height: Some(30),
57            created_at_core_block_height: Some(1),
58            updated_at_core_block_height: Some(2),
59            transferred_at_core_block_height: Some(3),
60            creator_id: Some(Identifier::new([9u8; 32])),
61        }
62    }
63
64    // ================================================================
65    //  to_map_value: produces a BTreeMap<String, Value> keyed by the
66    //  documented serde field names (with the $-prefixed renames).
67    // ================================================================
68
69    #[test]
70    fn to_map_value_contains_id_and_owner_id_keys() {
71        let doc = minimal_doc();
72        let map = doc.to_map_value().expect("to_map_value should succeed");
73        assert!(map.contains_key(property_names::ID));
74        assert!(map.contains_key(property_names::OWNER_ID));
75    }
76
77    #[test]
78    fn to_map_value_contains_all_set_optional_fields() {
79        let doc = full_doc();
80        let map = doc.to_map_value().expect("to_map_value should succeed");
81        assert!(map.contains_key(property_names::REVISION));
82        assert!(map.contains_key(property_names::CREATED_AT));
83        assert!(map.contains_key(property_names::UPDATED_AT));
84        assert!(map.contains_key(property_names::TRANSFERRED_AT));
85        assert!(map.contains_key(property_names::CREATED_AT_BLOCK_HEIGHT));
86        assert!(map.contains_key(property_names::UPDATED_AT_BLOCK_HEIGHT));
87        assert!(map.contains_key(property_names::TRANSFERRED_AT_BLOCK_HEIGHT));
88        assert!(map.contains_key(property_names::CREATED_AT_CORE_BLOCK_HEIGHT));
89        assert!(map.contains_key(property_names::UPDATED_AT_CORE_BLOCK_HEIGHT));
90        assert!(map.contains_key(property_names::TRANSFERRED_AT_CORE_BLOCK_HEIGHT));
91        assert!(map.contains_key(property_names::CREATOR_ID));
92        // User-defined properties are flattened into the map
93        assert!(map.contains_key("name"));
94        assert!(map.contains_key("score"));
95    }
96
97    // ================================================================
98    //  into_map_value: same shape as to_map_value, consumes self
99    // ================================================================
100
101    #[test]
102    fn into_map_value_consumes_and_returns_same_shape_as_to_map_value() {
103        let doc = full_doc();
104        let from_ref = doc.to_map_value().expect("to_map_value");
105        let from_owned = doc.into_map_value().expect("into_map_value");
106        assert_eq!(from_ref, from_owned);
107    }
108
109    // After Phase D step 8 slice A, the `to_object` / `into_value` /
110    // `from_platform_value` tests on this V0 inner moved to the outer
111    // `Document` enum's canonical-trait round-trip tests in
112    // `serialization_traits/platform_value_conversion/mod.rs`. The
113    // `to_map_value` / `into_map_value` tests stay because those are
114    // the methods this trait still defines.
115}