Skip to content

Commit

Permalink
Fail early - try converting all services
Browse files Browse the repository at this point in the history
Signed-off-by: Miroslav Kovar <miroslav.kovar@absa.africa>
  • Loading branch information
mirgee committed Jun 12, 2023
1 parent 022e2d0 commit 820fef6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 21 deletions.
10 changes: 8 additions & 2 deletions did_doc_sov/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,14 @@ impl DidDocumentSov {
self.did_doc.authentication()
}

pub fn service(&self) -> ServicesList {
ServicesList::new(self.did_doc.service())
pub fn service(&self) -> Result<ServicesList, DidDocumentSovError> {
Ok(ServicesList::new(
self.did_doc
.service()
.iter()
.map(|s| ServiceSov::try_from(s.to_owned()))
.collect::<Result<Vec<ServiceSov>, _>>()?,
))
}

pub fn assertion_method(&self) -> &[VerificationMethodKind] {
Expand Down
15 changes: 6 additions & 9 deletions did_doc_sov/src/service/services_list.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
use did_doc::schema::service::Service;

use crate::{error::DidDocumentSovError, extra_fields::ExtraFieldsSov};
use crate::error::DidDocumentSovError;

use super::ServiceSov;

pub struct ServicesList<'a> {
services: &'a [Service<ExtraFieldsSov>],
pub struct ServicesList {
services: Vec<ServiceSov>,
}

impl<'a> ServicesList<'a> {
pub fn new(services: &'a [Service<ExtraFieldsSov>]) -> Self {
impl ServicesList {
pub fn new(services: Vec<ServiceSov>) -> Self {
Self { services }
}

pub fn get(&self, index: usize) -> Result<ServiceSov, DidDocumentSovError> {
pub fn get(&self, index: usize) -> Result<&ServiceSov, DidDocumentSovError> {
self.services
.get(index)
.ok_or(DidDocumentSovError::IndexOutOfBounds(index))
.and_then(|service| ServiceSov::try_from(service.to_owned()))
}

pub fn len(&self) -> usize {
Expand Down
15 changes: 9 additions & 6 deletions did_doc_sov/tests/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ fn test_service_build_aip1() {
.add_service(ServiceSov::AIP1(service))
.unwrap()
.build();
assert_eq!(did_doc.service().len(), 1);
let first_service = did_doc.service().get(0).unwrap();
let services = did_doc.service().unwrap();
assert_eq!(services.len(), 1);
let first_service = services.get(0).unwrap();
assert_eq!(first_service.id().clone(), ID.parse::<Uri>().unwrap());
assert_eq!(
first_service.service_endpoint().clone(),
Expand Down Expand Up @@ -50,8 +51,9 @@ fn test_service_build_didcommv1() {
.add_service(ServiceSov::DIDCommV1(service))
.unwrap()
.build();
assert_eq!(did_doc.service().len(), 1);
let first_service = did_doc.service().get(0).unwrap();
let services = did_doc.service().unwrap();
assert_eq!(services.len(), 1);
let first_service = services.get(0).unwrap();
assert_eq!(first_service.id().clone(), ID.parse::<Uri>().unwrap());
assert_eq!(
first_service.service_endpoint().clone(),
Expand All @@ -78,8 +80,9 @@ fn test_service_build_didcommv2() {
.add_service(ServiceSov::DIDCommV2(service))
.unwrap()
.build();
assert_eq!(did_doc.service().len(), 1);
let first_service = did_doc.service().get(0).unwrap();
let services = did_doc.service().unwrap();
assert_eq!(services.len(), 1);
let first_service = services.get(0).unwrap();
assert_eq!(first_service.id().clone(), ID.parse::<Uri>().unwrap());
assert_eq!(
first_service.service_endpoint().clone(),
Expand Down
9 changes: 5 additions & 4 deletions did_doc_sov/tests/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,17 @@ fn test_serde() {
assert_eq!(did_doc.authentication().len(), 1);
assert_eq!(did_doc.assertion_method().len(), 1);
assert_eq!(did_doc.key_agreement().len(), 1);
assert_eq!(did_doc.service().len(), 3);
assert_eq!(did_doc.service().unwrap().len(), 3);

let first_service = did_doc.service().get(0).unwrap();
let services = did_doc.service().unwrap();
let first_service = services.get(0).unwrap();
assert_eq!(
first_service.service_endpoint().to_string(),
"https://example.com/endpoint"
);
assert_eq!(first_service.service_type(), ServiceType::AIP1);

let second_service = did_doc.service().get(1).unwrap();
let second_service = services.get(1).unwrap();
assert_eq!(
second_service.id().to_string(),
"did:sov:HR6vs6GEZ8rHaVgjg2WodM#did-communication"
Expand All @@ -91,7 +92,7 @@ fn test_serde() {
"https://example.com/endpoint"
);

let third_service = did_doc.service().get(2).unwrap();
let third_service = services.get(2).unwrap();
assert_eq!(
third_service.id().to_string(),
"did:sov:HR6vs6GEZ8rHaVgjg2WodM#didcomm-1"
Expand Down

0 comments on commit 820fef6

Please sign in to comment.