pub struct DidMethod { /* private fields */ }Expand description
A DID method, named with an optional namespace: did:demo or did:demo:dev.
Parties that declare or negotiate support for DID methods (SIOPv2’s
subject_syntax_types_supported, a wallet’s configuration, a resolver’s registry) exchange
method identifiers rather than DIDs. A DidMethod is such an identifier: the method name of
DID Core §3.1 (method-name = 1*method-char, lowercase ASCII letters and digits only),
optionally followed by one idchar segment that names a namespace within the method, as
dev in did:demo:dev or zXYZ in did:ebsi:zXYZ. SIOPv2 lists methods without a
namespace; wallets and issuers that distinguish networks of one method exchange the namespaced
form; both parse.
A DidMethod is validated when it is constructed and keeps the input unchanged. It displays,
serializes and deserializes as did:<name> or did:<name>:<namespace>, where
deserialization validates. Equality is field-wise; the ordering is by name, then namespace,
with the bare method before its namespaced forms.
DidMethod::matches tells whether a Did belongs to the method: its method name is
equal and, when a namespace is set, the first colon-separated segment of its method-specific
id is that namespace. Did::method keeps returning the name as a &str;
DidMethod::from(&did) is the method of a DID, without a namespace.
§Errors
A method name that is empty or has a character outside a-z and 0-9 is
DidError::InvalidMethodName, as it is for a Did. A namespace that is empty, has a
second segment (did:demo:dev:x) or a character that is not an idchar is
DidError::InvalidNamespace; an incomplete percent-encoding in it is
DidError::InvalidPercentEncoding. Parsing a string also reports
DidError::MissingScheme and DidError::TooLong as Did::parse does. Positions are
byte offsets into the input that was given: the string for the parsing forms, the name for
DidMethod::new, the namespace for DidMethod::with_namespace.
§Examples
use identity_kit::did::{Did, DidError, DidMethod};
let demo: DidMethod = "did:demo".parse()?;
let namespaced = DidMethod::new("demo")?.with_namespace("dev")?;
assert_eq!(demo.name(), "demo");
assert_eq!(demo.namespace(), None);
assert_eq!(namespaced.namespace(), Some("dev"));
assert_eq!(namespaced.to_string(), "did:demo:dev");
let did = Did::parse("did:demo:dev:0xabc")?;
assert!(demo.matches(&did));
assert!(namespaced.matches(&did));
assert!(!DidMethod::parse("did:demo:alt")?.matches(&did));
assert_eq!(DidMethod::from(&did), demo);
// The method name is lowercase; the namespace is one non-empty segment.
assert_eq!(
DidMethod::parse("did:Example"),
Err(DidError::InvalidMethodName { position: 4 })
);
assert_eq!(
DidMethod::parse("did:demo:"),
Err(DidError::InvalidNamespace { position: 9 })
);
assert_eq!(
DidMethod::parse("did:demo:dev:x"),
Err(DidError::InvalidNamespace { position: 12 })
);Implementations§
Source§impl DidMethod
impl DidMethod
Sourcepub fn new(name: impl Into<String>) -> Result<Self, DidError>
pub fn new(name: impl Into<String>) -> Result<Self, DidError>
A method named name, without a namespace.
§Errors
Returns DidError::InvalidMethodName when name is empty or has a character outside
a-z and 0-9, with the byte offset into name.
§Examples
use identity_kit::did::{DidError, DidMethod};
assert_eq!(DidMethod::new("web")?.to_string(), "did:web");
assert_eq!(
DidMethod::new("ex_ample"),
Err(DidError::InvalidMethodName { position: 2 })
);Sourcepub fn with_namespace(
self,
namespace: impl Into<String>,
) -> Result<Self, DidError>
pub fn with_namespace( self, namespace: impl Into<String>, ) -> Result<Self, DidError>
This method with its namespace set to namespace.
§Errors
Returns DidError::InvalidNamespace when namespace is empty or has a character that
is not an idchar (a : among them), or DidError::InvalidPercentEncoding when a %
is not followed by two hexadecimal digits, with the byte offset into namespace.
§Examples
use identity_kit::did::{DidError, DidMethod};
let ebsi = DidMethod::new("ebsi")?;
assert_eq!(ebsi.clone().with_namespace("zXYZ")?.to_string(), "did:ebsi:zXYZ");
assert_eq!(
ebsi.with_namespace("a:b"),
Err(DidError::InvalidNamespace { position: 1 })
);Sourcepub fn parse(s: &str) -> Result<Self, DidError>
pub fn parse(s: &str) -> Result<Self, DidError>
Parses did:<name> or did:<name>:<namespace>, rejecting input longer than
DEFAULT_MAX_LEN bytes.
§Errors
Returns a DidError describing the first violation, as listed for DidMethod.
§Examples
use identity_kit::did::{DidError, DidMethod};
let method = DidMethod::parse("did:ebsi:zXYZ")?;
assert_eq!(method.name(), "ebsi");
assert_eq!(method.namespace(), Some("zXYZ"));
assert_eq!(DidMethod::parse("ebsi"), Err(DidError::MissingScheme));Sourcepub fn namespace(&self) -> Option<&str>
pub fn namespace(&self) -> Option<&str>
The namespace within the method: dev in did:demo:dev, None in did:demo.
Sourcepub fn matches(&self, did: &Did) -> bool
pub fn matches(&self, did: &Did) -> bool
Whether did belongs to this method: its method name is name and, when a
namespace is set, the first colon-separated segment of its method-specific id is that
namespace.
§Examples
use identity_kit::did::{Did, DidMethod};
let did = Did::parse("did:demo:dev:0xabc")?;
assert!(DidMethod::parse("did:demo")?.matches(&did));
assert!(DidMethod::parse("did:demo:dev")?.matches(&did));
assert!(!DidMethod::parse("did:demo:alt")?.matches(&did));
assert!(!DidMethod::parse("did:web")?.matches(&did));Trait Implementations§
Source§impl<'de> Deserialize<'de> for DidMethod
impl<'de> Deserialize<'de> for DidMethod
Source§fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
impl Eq for DidMethod
Source§impl From<&Did> for DidMethod
The method of a DID, without a namespace: did:demo for did:demo:dev:0xabc.
impl From<&Did> for DidMethod
The method of a DID, without a namespace: did:demo for did:demo:dev:0xabc.
Source§impl Ord for DidMethod
impl Ord for DidMethod
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
1.21.0 (const: unstable) · Source§fn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialOrd for DidMethod
impl PartialOrd for DidMethod
impl StructuralPartialEq for DidMethod
Auto Trait Implementations§
impl Freeze for DidMethod
impl RefUnwindSafe for DidMethod
impl Send for DidMethod
impl Sync for DidMethod
impl Unpin for DidMethod
impl UnsafeUnpin for DidMethod
impl UnwindSafe for DidMethod
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.