identity_credential/credential/
refresh.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
// Copyright 2020-2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use serde::Deserialize;
use serde::Serialize;

use identity_core::common::Object;
use identity_core::common::OneOrMany;
use identity_core::common::Url;

/// Information used to refresh or assert the status of a [`Credential`][crate::credential::Credential].
///
/// [More Info](https://www.w3.org/TR/vc-data-model/#refreshing)
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct RefreshService {
  /// The Url of the credential refresh service.
  pub id: Url,
  /// The type(s) of the credential refresh service.
  #[serde(rename = "type")]
  pub types: OneOrMany<String>,
  /// Additional properties of the credential refresh service.
  #[serde(flatten)]
  pub properties: Object,
}

impl RefreshService {
  /// Creates a new `RefreshService`.
  pub fn new<T>(id: Url, types: T) -> Self
  where
    T: Into<OneOrMany<String>>,
  {
    Self::with_properties(id, types, Object::new())
  }

  /// Creates a new `RefreshService` with the given `properties`.
  pub fn with_properties<T>(id: Url, types: T, properties: Object) -> Self
  where
    T: Into<OneOrMany<String>>,
  {
    Self {
      id,
      types: types.into(),
      properties,
    }
  }
}

#[cfg(test)]
mod tests {
  use identity_core::convert::FromJson;

  use crate::credential::RefreshService;

  const JSON: &str = include_str!("../../tests/fixtures/refresh-1.json");

  #[test]
  fn test_from_json() {
    let service: RefreshService = RefreshService::from_json(JSON).unwrap();
    assert_eq!(service.id, "https://example.edu/refresh/3732");
    assert_eq!(service.types.as_slice(), ["ManualRefreshService2018"]);
  }
}