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
impl Registry
Sourcepub fn new_with_remote_cache() -> Self
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.
Sourcepub fn load_document(&mut self, filename: PathBuf) -> Result<(), Error>
pub fn load_document(&mut self, filename: PathBuf) -> Result<(), Error>
Load a document from the filesystem as JSON.
Examples found in repository?
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(())
}
Sourcepub fn load_document_cbor(&mut self, filename: PathBuf) -> Result<(), Error>
pub fn load_document_cbor(&mut self, filename: PathBuf) -> Result<(), Error>
Load a document from the filesystem as CBOR.
Examples found in repository?
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(())
}
Sourcepub fn iter<'a>(&'a self) -> impl Iterator<Item = (&DID, &Document)> + 'a
pub fn iter<'a>(&'a self) -> impl Iterator<Item = (&DID, &Document)> + 'a
Get an iterator into the ordered pairs of the registry.
Sourcepub fn insert(&mut self, doc: Document) -> Result<(), Error>
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.
Sourcepub fn verification_method_for_url(
&self,
did: &DID,
url: URL,
) -> Option<VerificationMethod>
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.
Sourcepub fn controls(&self, did: &DID, controller: &DID) -> Result<bool, Error>
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.
Sourcepub fn equivalent_to_did(
&mut self,
did: &DID,
other: &DID,
) -> Result<bool, Error>
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§
Auto Trait Implementations§
impl Freeze for Registry
impl RefUnwindSafe for Registry
impl Send for Registry
impl Sync for Registry
impl Unpin for Registry
impl UnwindSafe for Registry
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> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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