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