identity_iota_core/document/
iota_document_metadata.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright 2020-2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use core::fmt::Debug;
use core::fmt::Display;
use core::fmt::Formatter;

use identity_core::common::Object;
use identity_core::common::Timestamp;
use identity_core::convert::FmtJson;
use serde::Deserialize;
use serde::Serialize;

/// Additional attributes related to a [`IotaDocument`][crate::IotaDocument].
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct IotaDocumentMetadata {
  /// The timestamp of document creation.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub created: Option<Timestamp>,
  /// The timestamp of the last update to the document.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub updated: Option<Timestamp>,
  /// Signals whether the document is deactivated.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub deactivated: Option<bool>,
  /// Bech32-encoded address of the governor unlock condition.
  #[serde(rename = "governorAddress", skip_serializing_if = "Option::is_none")]
  pub governor_address: Option<String>,
  /// Bech32-encoded address of the state controller unlock condition.
  #[serde(rename = "stateControllerAddress", skip_serializing_if = "Option::is_none")]
  pub state_controller_address: Option<String>,
  #[serde(flatten)]
  properties: Object,
}

impl IotaDocumentMetadata {
  /// Creates a new `IotaDocumentMetadata` with the current system datetime used for `created`
  /// and `updated` timestamps.
  pub fn new() -> Self {
    let now: Timestamp = Timestamp::now_utc();
    Self {
      created: Some(now),
      updated: Some(now),
      deactivated: None,
      governor_address: None,
      state_controller_address: None,
      properties: Object::default(),
    }
  }

  /// Returns a reference to the custom metadata properties.
  pub fn properties(&self) -> &Object {
    &self.properties
  }

  /// Returns a mutable reference to the custom metadata properties.
  pub fn properties_mut(&mut self) -> &mut Object {
    &mut self.properties
  }
}

impl Default for IotaDocumentMetadata {
  fn default() -> Self {
    Self::new()
  }
}

impl Display for IotaDocumentMetadata {
  fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
    self.fmt_json(f)
  }
}