Skip to main content

DidMethod

Struct DidMethod 

Source
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

Source

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 })
);
Source

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 })
);
Source

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));
Source

pub fn name(&self) -> &str

The method name: demo in did:demo:dev.

Source

pub fn namespace(&self) -> Option<&str>

The namespace within the method: dev in did:demo:dev, None in did:demo.

Source

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 Clone for DidMethod

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for DidMethod

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for DidMethod

Source§

fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for DidMethod

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

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.

Source§

fn from(did: &Did) -> Self

Converts to this type from the input type.
Source§

impl FromStr for DidMethod

Source§

type Err = DidError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for DidMethod

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for DidMethod

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl PartialEq for DidMethod

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialOrd for DidMethod

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serialize for DidMethod

Source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for DidMethod

Source§

impl TryFrom<&str> for DidMethod

Source§

type Error = DidError

The type returned in the event of a conversion error.
Source§

fn try_from(s: &str) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<String> for DidMethod

Source§

type Error = DidError

The type returned in the event of a conversion error.
Source§

fn try_from(s: String) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more