identity_verification/verification_method/
method_scope.rs

1// Copyright 2020-2023 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4use core::fmt;
5use core::fmt::Display;
6use core::str::FromStr;
7
8use identity_core::convert::FmtJson;
9
10use crate::error::Error;
11use crate::error::Result;
12use crate::verification_method::MethodRelationship;
13
14/// The scope of a [`VerificationMethod`](crate::VerificationMethod).
15///
16/// Can either refer to a generic method embedded in the verification method field,
17/// or to a verification relationship.
18#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize, Default)]
19pub enum MethodScope {
20  /// The scope of generic verification methods.
21  #[default]
22  VerificationMethod,
23  /// The scope of a specific [`MethodRelationship`].
24  VerificationRelationship(MethodRelationship),
25}
26
27impl MethodScope {
28  /// Returns the string representation of the scope.
29  pub fn as_str(&self) -> &'static str {
30    match self {
31      Self::VerificationMethod => "VerificationMethod",
32      Self::VerificationRelationship(relationship) => relationship.into(),
33    }
34  }
35
36  /// The verification relationship scope of [`MethodRelationship::Authentication`].
37  pub const fn authentication() -> Self {
38    Self::VerificationRelationship(MethodRelationship::Authentication)
39  }
40
41  /// The verification relationship scope of [`MethodRelationship::CapabilityDelegation`].
42  pub const fn capability_delegation() -> Self {
43    Self::VerificationRelationship(MethodRelationship::CapabilityDelegation)
44  }
45
46  /// The verification relationship scope of [`MethodRelationship::CapabilityInvocation`].
47  pub const fn capability_invocation() -> Self {
48    Self::VerificationRelationship(MethodRelationship::CapabilityInvocation)
49  }
50
51  /// The verification relationship scope of [`MethodRelationship::AssertionMethod`].
52  pub const fn assertion_method() -> Self {
53    Self::VerificationRelationship(MethodRelationship::AssertionMethod)
54  }
55
56  /// The verification relationship scope of [`MethodRelationship::KeyAgreement`].
57  pub const fn key_agreement() -> Self {
58    Self::VerificationRelationship(MethodRelationship::KeyAgreement)
59  }
60}
61
62impl FromStr for MethodScope {
63  type Err = Error;
64
65  fn from_str(string: &str) -> Result<Self, Self::Err> {
66    match string {
67      "VerificationMethod" => Ok(Self::VerificationMethod),
68      "Authentication" => Ok(Self::VerificationRelationship(MethodRelationship::Authentication)),
69      "AssertionMethod" => Ok(Self::VerificationRelationship(MethodRelationship::AssertionMethod)),
70      "KeyAgreement" => Ok(Self::VerificationRelationship(MethodRelationship::KeyAgreement)),
71      "CapabilityDelegation" => Ok(Self::VerificationRelationship(MethodRelationship::CapabilityDelegation)),
72      "CapabilityInvocation" => Ok(Self::VerificationRelationship(MethodRelationship::CapabilityInvocation)),
73      _ => Err(Error::UnknownMethodScope),
74    }
75  }
76}
77
78impl From<MethodRelationship> for MethodScope {
79  fn from(relationship: MethodRelationship) -> Self {
80    Self::VerificationRelationship(relationship)
81  }
82}
83
84impl Display for MethodScope {
85  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
86    self.fmt_json(f)
87  }
88}