Skip to content

Commit

Permalink
Remove ServicesList
Browse files Browse the repository at this point in the history
Signed-off-by: Miroslav Kovar <miroslav.kovar@absa.africa>
  • Loading branch information
mirgee authored and bobozaur committed Jun 20, 2023
1 parent fc3dc2f commit 87498b4
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 51 deletions.
56 changes: 40 additions & 16 deletions did_doc_sov/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@ use did_doc::{
verification_method::{VerificationMethod, VerificationMethodKind},
},
};
use error::DidDocumentSovError;
use extra_fields::ExtraFieldsSov;
use serde::{Deserialize, Serialize};
use serde::{de, Deserialize, Deserializer, Serialize};
use serde_json::Value;
use service::{services_list::ServicesList, ServiceSov};
use service::ServiceSov;

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[derive(Serialize, Clone, Debug, PartialEq)]
pub struct DidDocumentSov {
#[serde(flatten)]
did_doc: DidDocument<ExtraFieldsSov>,
services: Vec<ServiceSov>,
}

impl DidDocumentSov {
Expand Down Expand Up @@ -47,14 +46,8 @@ impl DidDocumentSov {
self.did_doc.authentication()
}

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 service(&self) -> &[ServiceSov] {
self.services.as_ref()
}

pub fn assertion_method(&self) -> &[VerificationMethodKind] {
Expand Down Expand Up @@ -85,12 +78,14 @@ impl DidDocumentSov {
#[derive(Default)]
pub struct DidDocumentSovBuilder {
ddo_builder: DidDocumentBuilder<ExtraFieldsSov>,
services: Vec<ServiceSov>,
}

impl DidDocumentSovBuilder {
pub fn new(id: Did) -> Self {
Self {
ddo_builder: DidDocumentBuilder::new(id),
services: Vec::new(),
}
}

Expand All @@ -104,14 +99,43 @@ impl DidDocumentSovBuilder {
self
}

pub fn add_service(mut self, service: ServiceSov) -> Result<Self, DidDocumentSovError> {
self.ddo_builder = self.ddo_builder.add_service(service.try_into()?);
Ok(self)
pub fn add_service(mut self, service: ServiceSov) -> Self {
self.services.push(service);
self
}

pub fn build(self) -> DidDocumentSov {
DidDocumentSov {
did_doc: self.ddo_builder.build(),
services: self.services,
}
}
}

impl<'de> Deserialize<'de> for DidDocumentSov {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize, Clone, Debug, PartialEq)]
struct TempDidDocumentSov {
#[serde(flatten)]
did_doc: DidDocument<ExtraFieldsSov>,
}

let temp = TempDidDocumentSov::deserialize(deserializer)?;

let services = temp
.did_doc
.service()
.iter()
.map(|s| ServiceSov::try_from(s.clone()))
.collect::<Result<Vec<ServiceSov>, _>>()
.map_err(|_| de::Error::custom("Failed to convert service"))?;

Ok(DidDocumentSov {
did_doc: temp.did_doc,
services,
})
}
}
1 change: 0 additions & 1 deletion did_doc_sov/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use crate::{error::DidDocumentSovError, extra_fields::ExtraFieldsSov};
pub mod aip1;
pub mod didcommv1;
pub mod didcommv2;
pub mod services_list;

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub enum ServiceType {
Expand Down
23 changes: 0 additions & 23 deletions did_doc_sov/src/service/services_list.rs

This file was deleted.

13 changes: 4 additions & 9 deletions did_doc_sov/tests/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@ fn test_service_build_aip1() {
ExtraFieldsAIP1::default(),
)
.unwrap();
let did_doc = DidDocumentSov::builder()
.add_service(ServiceSov::AIP1(service))
.unwrap()
.build();
let services = did_doc.service().unwrap();
let did_doc = DidDocumentSov::builder().add_service(ServiceSov::AIP1(service)).build();
let services = did_doc.service();
assert_eq!(services.len(), 1);
let first_service = services.get(0).unwrap();
assert_eq!(first_service.id().clone(), ID.parse::<Uri>().unwrap());
Expand Down Expand Up @@ -49,9 +46,8 @@ fn test_service_build_didcommv1() {
.unwrap();
let did_doc = DidDocumentSov::builder()
.add_service(ServiceSov::DIDCommV1(service))
.unwrap()
.build();
let services = did_doc.service().unwrap();
let services = did_doc.service();
assert_eq!(services.len(), 1);
let first_service = services.get(0).unwrap();
assert_eq!(first_service.id().clone(), ID.parse::<Uri>().unwrap());
Expand Down Expand Up @@ -81,9 +77,8 @@ fn test_service_build_didcommv2() {
.unwrap();
let did_doc = DidDocumentSov::builder()
.add_service(ServiceSov::DIDCommV2(service))
.unwrap()
.build();
let services = did_doc.service().unwrap();
let services = did_doc.service();
assert_eq!(services.len(), 1);
let first_service = services.get(0).unwrap();
assert_eq!(first_service.id().clone(), ID.parse::<Uri>().unwrap());
Expand Down
4 changes: 2 additions & 2 deletions did_doc_sov/tests/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ 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().unwrap().len(), 3);
assert_eq!(did_doc.service().len(), 3);

let services = did_doc.service().unwrap();
let services = did_doc.service();
let first_service = services.get(0).unwrap();
assert_eq!(
first_service.service_endpoint().to_string(),
Expand Down

0 comments on commit 87498b4

Please sign in to comment.