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)]
19pub enum MethodScope {
20  /// The scope of generic verification methods.
21  VerificationMethod,
22  /// The scope of a specific [`MethodRelationship`].
23  VerificationRelationship(MethodRelationship),
24}
25
26impl MethodScope {
27  /// Returns the string representation of the scope.
28  pub fn as_str(&self) -> &'static str {
29    match self {
30      Self::VerificationMethod => "VerificationMethod",
31      Self::VerificationRelationship(relationship) => relationship.into(),
32    }
33  }
34
35  /// The verification relationship scope of [`MethodRelationship::Authentication`].
36  pub const fn authentication() -> Self {
37    Self::VerificationRelationship(MethodRelationship::Authentication)
38  }
39
40  /// The verification relationship scope of [`MethodRelationship::CapabilityDelegation`].
41  pub const fn capability_delegation() -> Self {
42    Self::VerificationRelationship(MethodRelationship::CapabilityDelegation)
43  }
44
45  /// The verification relationship scope of [`MethodRelationship::CapabilityInvocation`].
46  pub const fn capability_invocation() -> Self {
47    Self::VerificationRelationship(MethodRelationship::CapabilityInvocation)
48  }
49
50  /// The verification relationship scope of [`MethodRelationship::AssertionMethod`].
51  pub const fn assertion_method() -> Self {
52    Self::VerificationRelationship(MethodRelationship::AssertionMethod)
53  }
54
55  /// The verification relationship scope of [`MethodRelationship::KeyAgreement`].
56  pub const fn key_agreement() -> Self {
57    Self::VerificationRelationship(MethodRelationship::KeyAgreement)
58  }
59}
60
61impl Default for MethodScope {
62  fn default() -> Self {
63    Self::VerificationMethod
64  }
65}
66
67impl FromStr for MethodScope {
68  type Err = Error;
69
70  fn from_str(string: &str) -> Result<Self, Self::Err> {
71    match string {
72      "VerificationMethod" => Ok(Self::VerificationMethod),
73      "Authentication" => Ok(Self::VerificationRelationship(MethodRelationship::Authentication)),
74      "AssertionMethod" => Ok(Self::VerificationRelationship(MethodRelationship::AssertionMethod)),
75      "KeyAgreement" => Ok(Self::VerificationRelationship(MethodRelationship::KeyAgreement)),
76      "CapabilityDelegation" => Ok(Self::VerificationRelationship(MethodRelationship::CapabilityDelegation)),
77      "CapabilityInvocation" => Ok(Self::VerificationRelationship(MethodRelationship::CapabilityInvocation)),
78      _ => Err(Error::UnknownMethodScope),
79    }
80  }
81}
82
83impl From<MethodRelationship> for MethodScope {
84  fn from(relationship: MethodRelationship) -> Self {
85    Self::VerificationRelationship(relationship)
86  }
87}
88
89impl Display for MethodScope {
90  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
91    self.fmt_json(f)
92  }
93}