dpp/document/v0/
mod.rs

1//! Documents.
2//!
3//! This module defines the `Document` struct and implements its functions.
4//!
5
6mod accessors;
7#[cfg(feature = "document-cbor-conversion")]
8pub(super) mod cbor_conversion;
9#[cfg(feature = "json-conversion")]
10pub(super) mod json_conversion;
11#[cfg(feature = "value-conversion")]
12mod platform_value_conversion;
13pub mod serialize;
14
15use chrono::DateTime;
16use std::collections::BTreeMap;
17use std::fmt;
18
19use platform_value::Value;
20
21use crate::document::document_methods::{
22    DocumentGetRawForContractV0, DocumentGetRawForDocumentTypeV0, DocumentHashV0Method,
23    DocumentIsEqualIgnoringTimestampsV0,
24};
25
26use crate::identity::TimestampMillis;
27use crate::prelude::Revision;
28use crate::prelude::{BlockHeight, CoreBlockHeight, Identifier};
29#[cfg(feature = "json-conversion")]
30use crate::serialization::json_safe_fields;
31
32/// Documents contain the data that goes into data contracts.
33#[cfg_attr(feature = "json-conversion", json_safe_fields)]
34#[derive(Clone, Debug, PartialEq, Default)]
35#[cfg_attr(
36    any(feature = "serde-conversion", feature = "serde-conversion"),
37    derive(serde::Serialize, serde::Deserialize)
38)]
39pub struct DocumentV0 {
40    /// The unique document ID.
41    #[cfg_attr(
42        any(feature = "serde-conversion", feature = "serde-conversion"),
43        serde(rename = "$id")
44    )]
45    pub id: Identifier,
46    /// The ID of the document's owner.
47    #[cfg_attr(
48        any(feature = "serde-conversion", feature = "serde-conversion"),
49        serde(rename = "$ownerId")
50    )]
51    pub owner_id: Identifier,
52    /// The document's properties (data).
53    #[cfg_attr(
54        any(feature = "serde-conversion", feature = "serde-conversion"),
55        serde(flatten)
56    )]
57    pub properties: BTreeMap<String, Value>,
58    /// The document revision, if the document is mutable.
59    #[cfg_attr(
60        any(feature = "serde-conversion", feature = "serde-conversion"),
61        serde(rename = "$revision", default)
62    )]
63    pub revision: Option<Revision>,
64    /// The time in milliseconds that the document was created, if it is set as required by the document type schema.
65    #[cfg_attr(
66        any(feature = "serde-conversion", feature = "serde-conversion"),
67        serde(rename = "$createdAt", default)
68    )]
69    pub created_at: Option<TimestampMillis>,
70    /// The time in milliseconds that the document was last updated, if it is set as required by the document type schema.
71    #[cfg_attr(
72        any(feature = "serde-conversion", feature = "serde-conversion"),
73        serde(rename = "$updatedAt", default)
74    )]
75    pub updated_at: Option<TimestampMillis>,
76    /// The time in milliseconds that the document was last transferred, if it is set as required by the document type schema.
77    #[cfg_attr(
78        any(feature = "serde-conversion", feature = "serde-conversion"),
79        serde(rename = "$transferredAt", default)
80    )]
81    pub transferred_at: Option<TimestampMillis>,
82    /// The block that the document was created, if it is set as required by the document type schema.
83    #[cfg_attr(
84        any(feature = "serde-conversion", feature = "serde-conversion"),
85        serde(rename = "$createdAtBlockHeight", default)
86    )]
87    pub created_at_block_height: Option<BlockHeight>,
88    /// The block that the document was last updated, if it is set as required by the document type schema.
89    #[cfg_attr(
90        any(feature = "serde-conversion", feature = "serde-conversion"),
91        serde(rename = "$updatedAtBlockHeight", default)
92    )]
93    pub updated_at_block_height: Option<BlockHeight>,
94    /// The block that the document was last transferred to a new identity, if it is set as required by the document type schema.
95    #[cfg_attr(
96        any(feature = "serde-conversion", feature = "serde-conversion"),
97        serde(rename = "$transferredAtBlockHeight", default)
98    )]
99    pub transferred_at_block_height: Option<BlockHeight>,
100    /// The core block that the document was created, if it is set as required by the document type schema.
101    #[cfg_attr(
102        any(feature = "serde-conversion", feature = "serde-conversion"),
103        serde(rename = "$createdAtCoreBlockHeight", default)
104    )]
105    pub created_at_core_block_height: Option<CoreBlockHeight>,
106    /// The core block that the document was last updated, if it is set as required by the document type schema.
107    #[cfg_attr(
108        any(feature = "serde-conversion", feature = "serde-conversion"),
109        serde(rename = "$updatedAtCoreBlockHeight", default)
110    )]
111    pub updated_at_core_block_height: Option<CoreBlockHeight>,
112    /// The core block that the document was last transferred to a new identity, if it is set as required by the document type schema.
113    #[cfg_attr(
114        any(feature = "serde-conversion", feature = "serde-conversion"),
115        serde(rename = "$transferredAtCoreBlockHeight", default)
116    )]
117    pub transferred_at_core_block_height: Option<CoreBlockHeight>,
118    /// The creator id.
119    #[cfg_attr(
120        any(feature = "serde-conversion", feature = "serde-conversion"),
121        serde(rename = "$creatorId", default)
122    )]
123    pub creator_id: Option<Identifier>,
124}
125
126impl DocumentGetRawForContractV0 for DocumentV0 {
127    //automatically done
128}
129
130impl DocumentIsEqualIgnoringTimestampsV0 for DocumentV0 {
131    //automatically done
132}
133
134impl DocumentGetRawForDocumentTypeV0 for DocumentV0 {
135    //automatically done
136}
137
138impl DocumentHashV0Method for DocumentV0 {
139    //automatically done
140}
141
142impl fmt::Display for DocumentV0 {
143    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
144        write!(f, "id:{} ", self.id)?;
145        write!(f, "owner_id:{} ", self.owner_id)?;
146        if let Some(created_at) = self.created_at {
147            let datetime = DateTime::from_timestamp_millis(created_at as i64).unwrap_or_default();
148            write!(f, "created_at:{} ", datetime.format("%Y-%m-%d %H:%M:%S"))?;
149        }
150        if let Some(updated_at) = self.updated_at {
151            let datetime = DateTime::from_timestamp_millis(updated_at as i64).unwrap_or_default();
152            write!(f, "updated_at:{} ", datetime.format("%Y-%m-%d %H:%M:%S"))?;
153        }
154        if let Some(transferred_at) = self.transferred_at {
155            let datetime =
156                DateTime::from_timestamp_millis(transferred_at as i64).unwrap_or_default();
157            write!(
158                f,
159                "transferred_at:{} ",
160                datetime.format("%Y-%m-%d %H:%M:%S")
161            )?;
162        }
163
164        if let Some(created_at_block_height) = self.created_at_block_height {
165            write!(f, "created_at_block_height:{} ", created_at_block_height)?;
166        }
167        if let Some(updated_at_block_height) = self.updated_at_block_height {
168            write!(f, "updated_at_block_height:{} ", updated_at_block_height)?;
169        }
170        if let Some(transferred_at_block_height) = self.transferred_at_block_height {
171            write!(
172                f,
173                "transferred_at_block_height:{} ",
174                transferred_at_block_height
175            )?;
176        }
177        if let Some(created_at_core_block_height) = self.created_at_core_block_height {
178            write!(
179                f,
180                "created_at_core_block_height:{} ",
181                created_at_core_block_height
182            )?;
183        }
184        if let Some(updated_at_core_block_height) = self.updated_at_core_block_height {
185            write!(
186                f,
187                "updated_at_core_block_height:{} ",
188                updated_at_core_block_height
189            )?;
190        }
191        if let Some(transferred_at_core_block_height) = self.transferred_at_core_block_height {
192            write!(
193                f,
194                "transferred_at_core_block_height:{} ",
195                transferred_at_core_block_height
196            )?;
197        }
198
199        if let Some(creator_id) = self.creator_id {
200            write!(f, "creator_id:{} ", creator_id)?;
201        }
202
203        if self.properties.is_empty() {
204            write!(f, "no properties")?;
205        } else {
206            for (key, value) in self.properties.iter() {
207                write!(f, "{}:{} ", key, value)?
208            }
209        }
210        Ok(())
211    }
212}