Skip to main content

dpp/document/accessors/v0/
mod.rs

1use platform_value::btreemap_extensions::{
2    BTreeValueMapInsertionPathHelper, BTreeValueMapPathHelper,
3};
4use platform_value::Value;
5use std::collections::BTreeMap;
6
7use crate::identity::TimestampMillis;
8use crate::prelude::Identifier;
9use crate::prelude::Revision;
10
11pub trait DocumentV0Getters {
12    /// Returns the unique document ID.
13    fn id(&self) -> Identifier;
14
15    /// Returns the ID of the document's owner.
16    fn owner_id(&self) -> Identifier;
17
18    /// Returns the document's properties (data).
19    fn properties(&self) -> &BTreeMap<String, Value>;
20
21    /// Returns a mutable reference to the document's properties (data).
22    fn properties_mut(&mut self) -> &mut BTreeMap<String, Value>;
23
24    /// Returns the document revision.
25    fn revision(&self) -> Option<Revision>;
26
27    /// Returns the time in milliseconds that the document was created.
28    fn created_at(&self) -> Option<TimestampMillis>;
29
30    /// Returns the time in milliseconds that the document was last updated.
31    fn updated_at(&self) -> Option<TimestampMillis>;
32
33    /// Returns the time in milliseconds that the document was last transferred.
34    fn transferred_at(&self) -> Option<TimestampMillis>;
35
36    /// Retrieves the field specified by the path.
37    /// Returns `None` if the path is empty or if the field is not present.
38    fn get(&self, path: &str) -> Option<&Value> {
39        self.properties().get_optional_at_path(path).ok().flatten()
40    }
41    fn id_ref(&self) -> &Identifier;
42    fn owner_id_ref(&self) -> &Identifier;
43    fn properties_consumed(self) -> BTreeMap<String, Value>;
44    fn created_at_block_height(&self) -> Option<u64>;
45    fn updated_at_block_height(&self) -> Option<u64>;
46    fn transferred_at_block_height(&self) -> Option<u64>;
47    fn created_at_core_block_height(&self) -> Option<u32>;
48    fn updated_at_core_block_height(&self) -> Option<u32>;
49    fn transferred_at_core_block_height(&self) -> Option<u32>;
50    fn creator_id(&self) -> Option<Identifier>;
51    /// The data contract version this document's bytes conform to (the
52    /// serialization format 3 stamp); `None` for pre-stamp documents.
53    fn contract_version(&self) -> Option<u32>;
54}
55
56pub trait DocumentV0Setters: DocumentV0Getters {
57    /// Sets the unique document ID.
58    fn set_id(&mut self, id: Identifier);
59
60    /// Sets the ID of the document's owner.
61    fn set_owner_id(&mut self, owner_id: Identifier);
62
63    /// Sets the document's properties (data).
64    fn set_properties(&mut self, properties: BTreeMap<String, Value>);
65
66    /// Sets the document revision.
67    fn set_revision(&mut self, revision: Option<Revision>);
68
69    /// Sets the time in milliseconds that the document was created.
70    fn set_created_at(&mut self, created_at: Option<TimestampMillis>);
71
72    /// Sets the time in milliseconds that the document was last updated.
73    fn set_updated_at(&mut self, updated_at: Option<TimestampMillis>);
74
75    /// Set the value under the given path.
76    /// The path supports syntax from the `lodash` JS library. Example: "root.people[0].name".
77    /// If parents are not present, they will be automatically created.
78    fn set(&mut self, path: &str, value: Value) {
79        if !path.is_empty() {
80            self.properties_mut()
81                .insert_at_path(path, value)
82                .expect("path should not be empty, we checked");
83        }
84    }
85
86    /// Removes the value under the given path.
87    /// The path supports syntax from the `lodash` JS library. Example: "root.people[0].name".
88    /// If parents are not present, they will be automatically created.
89    fn remove(&mut self, path: &str) -> Option<Value> {
90        self.properties_mut().remove(path)
91    }
92
93    /// Sets a `u8` value for the specified property name.
94    fn set_u8(&mut self, property_name: &str, value: u8) {
95        self.properties_mut()
96            .insert(property_name.to_string(), Value::U8(value));
97    }
98
99    /// Sets an `i8` value for the specified property name.
100    fn set_i8(&mut self, property_name: &str, value: i8) {
101        self.properties_mut()
102            .insert(property_name.to_string(), Value::I8(value));
103    }
104
105    /// Sets a `u16` value for the specified property name.
106    fn set_u16(&mut self, property_name: &str, value: u16) {
107        self.properties_mut()
108            .insert(property_name.to_string(), Value::U16(value));
109    }
110
111    /// Sets an `i16` value for the specified property name.
112    fn set_i16(&mut self, property_name: &str, value: i16) {
113        self.properties_mut()
114            .insert(property_name.to_string(), Value::I16(value));
115    }
116
117    /// Sets a `u32` value for the specified property name.
118    fn set_u32(&mut self, property_name: &str, value: u32) {
119        self.properties_mut()
120            .insert(property_name.to_string(), Value::U32(value));
121    }
122
123    /// Sets an `i32` value for the specified property name.
124    fn set_i32(&mut self, property_name: &str, value: i32) {
125        self.properties_mut()
126            .insert(property_name.to_string(), Value::I32(value));
127    }
128
129    /// Sets a `u64` value for the specified property name.
130    fn set_u64(&mut self, property_name: &str, value: u64) {
131        self.properties_mut()
132            .insert(property_name.to_string(), Value::U64(value));
133    }
134
135    /// Sets an `i64` value for the specified property name.
136    fn set_i64(&mut self, property_name: &str, value: i64) {
137        self.properties_mut()
138            .insert(property_name.to_string(), Value::I64(value));
139    }
140
141    /// Sets a `Vec<u8>` (byte array) value for the specified property name.
142    fn set_bytes(&mut self, property_name: &str, value: Vec<u8>) {
143        self.properties_mut()
144            .insert(property_name.to_string(), Value::Bytes(value));
145    }
146    fn set_created_at_block_height(&mut self, created_at_block_height: Option<u64>);
147    fn set_updated_at_block_height(&mut self, updated_at_block_height: Option<u64>);
148    fn set_created_at_core_block_height(&mut self, created_at_core_block_height: Option<u32>);
149    fn set_updated_at_core_block_height(&mut self, updated_at_core_block_height: Option<u32>);
150    fn set_transferred_at_core_block_height(
151        &mut self,
152        transferred_at_core_block_height: Option<u32>,
153    );
154    fn set_transferred_at_block_height(&mut self, transferred_at_block_height: Option<u64>);
155    fn set_transferred_at(&mut self, transferred_at: Option<TimestampMillis>);
156    fn bump_revision(&mut self);
157    /// Sets the creator identifier of the document. This is applicable if the document's
158    /// schema requires this information.
159    ///
160    /// # Parameters
161    /// - `creator_id`: An `Option<Identifier>` to set as the document's creator ID.
162    ///   `None` indicates the creator ID is not available.
163    fn set_creator_id(&mut self, creator_id: Option<Identifier>);
164    /// Sets the contract-version stamp: the data contract version this
165    /// document's bytes conform to.
166    fn set_contract_version(&mut self, contract_version: Option<u32>);
167}