identity_iota_core/document/
iota_document_metadata.rs

1// Copyright 2020-2022 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4use core::fmt::Debug;
5use core::fmt::Display;
6use core::fmt::Formatter;
7
8use identity_core::common::Object;
9use identity_core::common::Timestamp;
10use identity_core::convert::FmtJson;
11use serde::Deserialize;
12use serde::Serialize;
13
14/// Additional attributes related to a [`IotaDocument`][crate::IotaDocument].
15#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
16pub struct IotaDocumentMetadata {
17  /// The timestamp of document creation.
18  #[serde(skip_serializing_if = "Option::is_none")]
19  pub created: Option<Timestamp>,
20  /// The timestamp of the last update to the document.
21  #[serde(skip_serializing_if = "Option::is_none")]
22  pub updated: Option<Timestamp>,
23  /// Signals whether the document is deactivated.
24  #[serde(skip_serializing_if = "Option::is_none")]
25  pub deactivated: Option<bool>,
26  #[serde(flatten)]
27  properties: Object,
28}
29
30impl IotaDocumentMetadata {
31  /// Creates a new `IotaDocumentMetadata` with the current system datetime used for `created`
32  /// and `updated` timestamps.
33  pub fn new() -> Self {
34    let now: Timestamp = Timestamp::now_utc();
35    Self {
36      created: Some(now),
37      updated: Some(now),
38      deactivated: None,
39      properties: Object::default(),
40    }
41  }
42
43  /// Returns a reference to the custom metadata properties.
44  pub fn properties(&self) -> &Object {
45    &self.properties
46  }
47
48  /// Returns a mutable reference to the custom metadata properties.
49  pub fn properties_mut(&mut self) -> &mut Object {
50    &mut self.properties
51  }
52}
53
54impl Default for IotaDocumentMetadata {
55  fn default() -> Self {
56    Self::new()
57  }
58}
59
60impl Display for IotaDocumentMetadata {
61  fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
62    self.fmt_json(f)
63  }
64}