did_toolkit::registry

Struct Registry

Source
pub struct Registry { /* private fields */ }
Expand description

Registry is a basic, in-memory Document registry that is able to load documents directly as well as cross-reference them in some ways. It can also optionally fetch remote documents and cache them as a part of its implementation. Documents can be loaded via the JSON or CBOR formats. JSON loading is provided by serde_json and CBOR is provided by ciborium.

Document validity checks (via Document::valid) are not performed at loading time. DID keying is automatically performed based on the Document id property.

Accessing the registry is provided by a few methods in the implementation, but can also be indexed by DID reference or usize. Iterators are provided as ordered pairs via Registry::iter. The underlying storage is a BTreeMap and awareness of the performance characteristics of this implementation may be important for larger registries.

There are examples in the apporpriate part of this crate which go into loading documents from disk.

use did_toolkit::prelude::*;
use either::Either;

let mut reg = Registry::default();
let did = DID::parse("did:mymethod:alice").unwrap();
let did2 = DID::parse("did:mymethod:bob").unwrap();
let doc = Document{
  id: did.clone(),
  controller: Some(Controller(Either::Left(did2.clone()))),
  ..Default::default()
};

reg.insert(doc.clone());

reg.insert(Document{
  id: did2.clone(),
  ..Default::default()
});

assert!(reg.controls(&did, &did2).unwrap());
assert_eq!(reg[0], doc);
assert_eq!(reg[&did], doc);

Implementations§

Source§

impl Registry

Source

pub fn new_with_remote_cache() -> Self

Create a Registry with the remote cache enabled. Use Registry::default for one that does not use the remote cache.

Source

pub fn load_document(&mut self, filename: PathBuf) -> Result<(), Error>

Load a document from the filesystem as JSON.

Examples found in repository?
examples/ingest-registry.rs (line 13)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
fn main() -> Result<(), anyhow::Error> {
    let mut args = std::env::args();
    let path = args.nth(1).unwrap();

    let dir = std::fs::read_dir(std::path::PathBuf::from(path))?;
    let mut reg = Registry::default();

    for item in dir {
        let item = item?;
        println!("Loading: {}", item.path().display());
        reg.load_document(item.path())?;
    }

    Ok(())
}
Source

pub fn load_document_cbor(&mut self, filename: PathBuf) -> Result<(), Error>

Load a document from the filesystem as CBOR.

Examples found in repository?
examples/ingest-registry-cbor.rs (line 13)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
fn main() -> Result<(), anyhow::Error> {
    let mut args = std::env::args();
    let path = args.nth(1).unwrap();

    let dir = std::fs::read_dir(std::path::PathBuf::from(path))?;
    let mut reg = Registry::default();

    for item in dir {
        let item = item?;
        println!("Loading: {}", item.path().display());
        reg.load_document_cbor(item.path())?;
    }

    Ok(())
}
Source

pub fn iter<'a>(&'a self) -> impl Iterator<Item = (&DID, &Document)> + 'a

Get an iterator into the ordered pairs of the registry.

Source

pub fn len(&self) -> usize

Compute the size of the registry.

Source

pub fn insert(&mut self, doc: Document) -> Result<(), Error>

Insert a document into the registry. The registry will automatically be keyed by the Document’s id property. Will fail if the document already exists.

Source

pub fn remove(&mut self, did: &DID) -> Option<Document>

Remove a document by DID.

Source

pub fn get(&self, did: &DID) -> Option<Document>

Retreive a document by DID.

Source

pub fn follow(&self, url: URL) -> Option<Document>

Retrieve a document by DID URL.

Source

pub fn verification_method_for_url( &self, did: &DID, url: URL, ) -> Option<VerificationMethod>

Looks up a VerificationMethod by URL for the DID. There must be a VerificationMethod in the DID’s document, otherwise this will return None.

Source

pub fn controls(&self, did: &DID, controller: &DID) -> Result<bool, Error>

For a given DID, determine if another DID is designated as a controller. Follows the rules specified in https://www.w3.org/TR/did-core/#did-controller. Will fail if either DID is missing from the registry.

Source

pub fn equivalent_to_did( &mut self, did: &DID, other: &DID, ) -> Result<bool, Error>

For two given DIDs, determine if they can be treated the same according to the rules for the alsoKnownAs property, which you can read here: https://www.w3.org/TR/did-core/#also-known-as

Both DIDs must exist in the registry, otherwise an error will be returned.

Trait Implementations§

Source§

impl Default for Registry

Source§

fn default() -> Registry

Returns the “default value” for a type. Read more
Source§

impl<'a> Index<&'a DID> for Registry

Source§

type Output = Document

The returned type after indexing.
Source§

fn index(&self, index: &'a DID) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl Index<usize> for Registry

Source§

type Output = Document

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<'a> IndexMut<&'a DID> for Registry

Source§

fn index_mut(&mut self, index: &'a DID) -> &mut Document

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl IndexMut<usize> for Registry

Source§

fn index_mut(&mut self, index: usize) -> &mut Document

Performs the mutable indexing (container[index]) operation. Read more

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> 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T