Skip to content

Commit

Permalink
Improve service struct builder
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 7, 2023
1 parent c742ee7 commit 4107359
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 47 deletions.
4 changes: 1 addition & 3 deletions did_doc/src/schema/did_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,9 @@ mod tests {
let service_type = "test-service".to_string();
let service_endpoint = "https://example.com/service";
let service = ServiceBuilder::<()>::new(service_id, service_endpoint.try_into().unwrap())
.unwrap()
.add_service_type(service_type)
.unwrap()
.build()
.unwrap();
.build();

let document = DidDocumentBuilder::new(id.clone())
.add_also_known_as(also_known_as.clone())
Expand Down
84 changes: 57 additions & 27 deletions did_doc/src/schema/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ impl<E> Service<E>
where
E: Default,
{
pub fn builder(
id: Uri,
service_endpoint: Url,
) -> Result<ServiceBuilder<E>, DidDocumentBuilderError> {
pub fn builder(id: Uri, service_endpoint: Url) -> ServiceBuilder<E> {
ServiceBuilder::new(id, service_endpoint)
}

Expand All @@ -55,6 +52,13 @@ where

#[derive(Debug)]
pub struct ServiceBuilder<E> {
id: Uri,
service_endpoint: Url,
extra: E,
}

#[derive(Debug)]
pub struct ServiceBuilderWithServiceType<E> {
id: Uri,
service_type: HashSet<String>,
service_endpoint: Url,
Expand All @@ -65,15 +69,52 @@ impl<E> ServiceBuilder<E>
where
E: Default,
{
pub fn new(id: Uri, service_endpoint: Url) -> Result<Self, DidDocumentBuilderError> {
Ok(Self {
pub fn new(id: Uri, service_endpoint: Url) -> Self {
Self {
id,
service_endpoint,
service_type: HashSet::new(),
extra: E::default(),
}
}

pub fn add_service_type(
self,
service_type: String,
) -> Result<ServiceBuilderWithServiceType<E>, DidDocumentBuilderError> {
if service_type.is_empty() {
return Err(DidDocumentBuilderError::MissingField("type"));
}
let mut service_types = HashSet::new();
service_types.insert(service_type);
Ok(ServiceBuilderWithServiceType {
id: self.id,
service_type: service_types,
service_endpoint: self.service_endpoint,
extra: self.extra,
})
}

pub fn add_service_types(
self,
service_types: Vec<String>,
) -> Result<ServiceBuilderWithServiceType<E>, DidDocumentBuilderError> {
if service_types.is_empty() {
return Err(DidDocumentBuilderError::MissingField("type"));
}
let service_types = service_types.into_iter().collect::<HashSet<_>>();
Ok(ServiceBuilderWithServiceType {
id: self.id,
service_type: service_types,
service_endpoint: self.service_endpoint,
extra: self.extra,
})
}
}

impl<E> ServiceBuilderWithServiceType<E>
where
E: Default,
{
pub fn add_service_type(
mut self,
service_type: String,
Expand All @@ -90,16 +131,12 @@ where
self
}

pub fn build(self) -> Result<Service<E>, DidDocumentBuilderError> {
if self.service_type.is_empty() {
Err(DidDocumentBuilderError::MissingField("type"))
} else {
Ok(Service {
id: self.id,
service_type: OneOrList::List(self.service_type.into_iter().collect()),
service_endpoint: self.service_endpoint,
extra: self.extra,
})
pub fn build(self) -> Service<E> {
Service {
id: self.id,
service_type: OneOrList::List(self.service_type.into_iter().collect()),
service_endpoint: self.service_endpoint,
extra: self.extra,
}
}
}
Expand Down Expand Up @@ -128,11 +165,9 @@ mod tests {

let service =
ServiceBuilder::<ExtraSov>::new(id.clone(), service_endpoint.try_into().unwrap())
.unwrap()
.add_service_type(service_type.clone())
.unwrap()
.build()
.unwrap();
.build();

assert_eq!(service.id(), &id);
assert_eq!(service.service_endpoint().as_ref(), service_endpoint);
Expand All @@ -153,12 +188,10 @@ mod tests {
};

let service = ServiceBuilder::<ExtraSov>::new(id, service_endpoint.try_into().unwrap())
.unwrap()
.add_service_type(service_type)
.unwrap()
.add_extra(extra)
.build()
.unwrap();
.build();

assert_eq!(service.extra().recipient_keys, recipient_keys);
assert_eq!(service.extra().routing_keys, routing_keys);
Expand All @@ -171,13 +204,11 @@ mod tests {
let service_type = "DIDCommMessaging".to_string();

let service = ServiceBuilder::<ExtraSov>::new(id, service_endpoint.try_into().unwrap())
.unwrap()
.add_service_type(service_type.clone())
.unwrap()
.add_service_type(service_type.clone())
.unwrap()
.build()
.unwrap();
.build();

assert_eq!(service.service_type(), &OneOrList::List(vec![service_type]));
}
Expand All @@ -188,7 +219,6 @@ mod tests {
let service_endpoint = "http://example.com/endpoint";

let res = ServiceBuilder::<ExtraSov>::new(id, service_endpoint.try_into().unwrap())
.unwrap()
.add_service_type("".to_string());
assert!(res.is_err());
}
Expand Down
12 changes: 3 additions & 9 deletions did_resolver_sov/src/dereferencing/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,21 +117,17 @@ mod tests {
"did:example:123456789abcdefghi#agent".parse().unwrap(),
"https://agent.example.com/8377464".try_into().unwrap(),
)
.unwrap()
.add_service_type("AgentService".to_string())
.unwrap()
.build()
.unwrap();
.build();

let messaging_service = Service::builder(
"did:example:123456789abcdefghi#messages".parse().unwrap(),
"https://example.com/messages/8377464".try_into().unwrap(),
)
.unwrap()
.add_service_type("MessagingService".to_string())
.unwrap()
.build()
.unwrap();
.build();

DidDocument::builder(Default::default())
.add_verification_method(verification_method)
Expand Down Expand Up @@ -207,11 +203,9 @@ mod tests {
"did:example:123456789abcdefghi#keys-1".parse().unwrap(),
"https://example.com/duplicated/8377464".try_into().unwrap(),
)
.unwrap()
.add_service_type("DuplicatedService".to_string())
.unwrap()
.build()
.unwrap();
.build();
did_document_builder.add_service(additional_service).build()
};

Expand Down
17 changes: 9 additions & 8 deletions did_resolver_sov/src/resolution/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,15 @@ pub(super) async fn ledger_response_to_ddo<E: Default>(
let datetime = unix_to_datetime(txn_time);

let service = {
let mut service_builder =
Service::builder(service_id, endpoint.endpoint.as_str().try_into()?)?;
for t in endpoint.types {
if t != DidSovServiceType::Unknown {
service_builder = service_builder.add_service_type(t.to_string())?;
};
}
service_builder.build()?
let service_types: Vec<String> = endpoint
.types
.into_iter()
.filter(|t| *t != DidSovServiceType::Unknown)
.map(|t| t.to_string())
.collect();
Service::builder(service_id, endpoint.endpoint.as_str().try_into()?)
.add_service_types(service_types)?
.build()
};

// TODO: Use multibase instead of base58
Expand Down

0 comments on commit 4107359

Please sign in to comment.