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    /// The block time at which a moderator last wrote the document's moderator fields.
55    fn moderated_at(&self) -> Option<TimestampMillis>;
56    /// The moderator who last wrote the document's moderator fields.
57    fn moderated_by(&self) -> Option<Identifier>;
58}
59
60pub trait DocumentV0Setters: DocumentV0Getters {
61    /// Sets the unique document ID.
62    fn set_id(&mut self, id: Identifier);
63
64    /// Sets the ID of the document's owner.
65    fn set_owner_id(&mut self, owner_id: Identifier);
66
67    /// Sets the document's properties (data).
68    fn set_properties(&mut self, properties: BTreeMap<String, Value>);
69
70    /// Sets the document revision.
71    fn set_revision(&mut self, revision: Option<Revision>);
72
73    /// Sets the time in milliseconds that the document was created.
74    fn set_created_at(&mut self, created_at: Option<TimestampMillis>);
75
76    /// Sets the time in milliseconds that the document was last updated.
77    fn set_updated_at(&mut self, updated_at: Option<TimestampMillis>);
78
79    /// Set the value under the given path.
80    /// The path supports syntax from the `lodash` JS library. Example: "root.people[0].name".
81    /// If parents are not present, they will be automatically created.
82    fn set(&mut self, path: &str, value: Value) {
83        if !path.is_empty() {
84            self.properties_mut()
85                .insert_at_path(path, value)
86                .expect("path should not be empty, we checked");
87        }
88    }
89
90    /// Removes the value under the given path.
91    /// The path supports syntax from the `lodash` JS library. Example: "root.people[0].name".
92    /// If parents are not present, they will be automatically created.
93    fn remove(&mut self, path: &str) -> Option<Value> {
94        self.properties_mut().remove(path)
95    }
96
97    /// Sets a `u8` value for the specified property name.
98    fn set_u8(&mut self, property_name: &str, value: u8) {
99        self.properties_mut()
100            .insert(property_name.to_string(), Value::U8(value));
101    }
102
103    /// Sets an `i8` value for the specified property name.
104    fn set_i8(&mut self, property_name: &str, value: i8) {
105        self.properties_mut()
106            .insert(property_name.to_string(), Value::I8(value));
107    }
108
109    /// Sets a `u16` value for the specified property name.
110    fn set_u16(&mut self, property_name: &str, value: u16) {
111        self.properties_mut()
112            .insert(property_name.to_string(), Value::U16(value));
113    }
114
115    /// Sets an `i16` value for the specified property name.
116    fn set_i16(&mut self, property_name: &str, value: i16) {
117        self.properties_mut()
118            .insert(property_name.to_string(), Value::I16(value));
119    }
120
121    /// Sets a `u32` value for the specified property name.
122    fn set_u32(&mut self, property_name: &str, value: u32) {
123        self.properties_mut()
124            .insert(property_name.to_string(), Value::U32(value));
125    }
126
127    /// Sets an `i32` value for the specified property name.
128    fn set_i32(&mut self, property_name: &str, value: i32) {
129        self.properties_mut()
130            .insert(property_name.to_string(), Value::I32(value));
131    }
132
133    /// Sets a `u64` value for the specified property name.
134    fn set_u64(&mut self, property_name: &str, value: u64) {
135        self.properties_mut()
136            .insert(property_name.to_string(), Value::U64(value));
137    }
138
139    /// Sets an `i64` value for the specified property name.
140    fn set_i64(&mut self, property_name: &str, value: i64) {
141        self.properties_mut()
142            .insert(property_name.to_string(), Value::I64(value));
143    }
144
145    /// Sets a `Vec<u8>` (byte array) value for the specified property name.
146    fn set_bytes(&mut self, property_name: &str, value: Vec<u8>) {
147        self.properties_mut()
148            .insert(property_name.to_string(), Value::Bytes(value));
149    }
150    fn set_created_at_block_height(&mut self, created_at_block_height: Option<u64>);
151    fn set_updated_at_block_height(&mut self, updated_at_block_height: Option<u64>);
152    fn set_created_at_core_block_height(&mut self, created_at_core_block_height: Option<u32>);
153    fn set_updated_at_core_block_height(&mut self, updated_at_core_block_height: Option<u32>);
154    fn set_transferred_at_core_block_height(
155        &mut self,
156        transferred_at_core_block_height: Option<u32>,
157    );
158    fn set_transferred_at_block_height(&mut self, transferred_at_block_height: Option<u64>);
159    fn set_transferred_at(&mut self, transferred_at: Option<TimestampMillis>);
160    fn bump_revision(&mut self);
161    /// Sets the creator identifier of the document. This is applicable if the document's
162    /// schema requires this information.
163    ///
164    /// # Parameters
165    /// - `creator_id`: An `Option<Identifier>` to set as the document's creator ID.
166    ///   `None` indicates the creator ID is not available.
167    fn set_creator_id(&mut self, creator_id: Option<Identifier>);
168    /// Sets the contract-version stamp: the data contract version this
169    /// document's bytes conform to.
170    fn set_contract_version(&mut self, contract_version: Option<u32>);
171    /// Sets the block time at which a moderator last wrote the document's moderator fields.
172    fn set_moderated_at(&mut self, moderated_at: Option<TimestampMillis>);
173    /// Sets the moderator who last wrote the document's moderator fields.
174    fn set_moderated_by(&mut self, moderated_by: Option<Identifier>);
175}