identity_verification/verification_method/
method_scope.rs1use 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#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize, Default)]
19pub enum MethodScope {
20 #[default]
22 VerificationMethod,
23 VerificationRelationship(MethodRelationship),
25}
26
27impl MethodScope {
28 pub fn as_str(&self) -> &'static str {
30 match self {
31 Self::VerificationMethod => "VerificationMethod",
32 Self::VerificationRelationship(relationship) => relationship.into(),
33 }
34 }
35
36 pub const fn authentication() -> Self {
38 Self::VerificationRelationship(MethodRelationship::Authentication)
39 }
40
41 pub const fn capability_delegation() -> Self {
43 Self::VerificationRelationship(MethodRelationship::CapabilityDelegation)
44 }
45
46 pub const fn capability_invocation() -> Self {
48 Self::VerificationRelationship(MethodRelationship::CapabilityInvocation)
49 }
50
51 pub const fn assertion_method() -> Self {
53 Self::VerificationRelationship(MethodRelationship::AssertionMethod)
54 }
55
56 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}