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