Skip to content

Commit

Permalink
Refactor ServiceBuilder
Browse files Browse the repository at this point in the history
Signed-off-by: Patrik Stas <[email protected]>
  • Loading branch information
Patrik-Stas committed Oct 21, 2023
1 parent 1a414f0 commit d96d585
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 51 deletions.
61 changes: 14 additions & 47 deletions did_doc/src/schema/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ use super::{
};
use crate::error::DidDocumentBuilderError;

pub type ServiceTypeAlias = OneOrList<String>;

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Service<E> {
id: Uri,
#[serde(rename = "type")]
service_type: ServiceTypeAlias,
service_type: OneOrList<String>,
service_endpoint: Url,
#[serde(flatten)]
extra: E,
Expand All @@ -30,7 +28,7 @@ impl<E> Service<E> {
&self.id
}

pub fn service_type(&self) -> &ServiceTypeAlias {
pub fn service_type(&self) -> &OneOrList<String> {
&self.service_type
}

Expand All @@ -45,13 +43,6 @@ impl<E> Service<E> {

#[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 @@ -62,6 +53,7 @@ impl<E> ServiceBuilder<E> {
pub fn new(id: Uri, service_endpoint: Url, extra: E) -> Self {
Self {
id,
service_type: Default::default(),
service_endpoint,
extra,
}
Expand All @@ -70,54 +62,29 @@ impl<E> ServiceBuilder<E> {
pub fn add_service_type(
self,
service_type: String,
) -> Result<ServiceBuilderWithServiceType<E>, DidDocumentBuilderError> {
) -> Result<ServiceBuilder<E>, DidDocumentBuilderError> {
if service_type.is_empty() {
return Err(DidDocumentBuilderError::MissingField("type"));
return Err(DidDocumentBuilderError::InvalidInput(
"Invalid service type: empty string".into(),
));
}
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"));
if self.service_type.contains(&service_type) {
return Err(DidDocumentBuilderError::InvalidInput(
"Service type was already included".into(),
));
}
let service_types = service_types.into_iter().collect::<HashSet<_>>();
Ok(ServiceBuilderWithServiceType {
let mut service_types = self.service_type.clone();
service_types.insert(service_type);
Ok(ServiceBuilder {
id: self.id,
service_type: service_types,
service_endpoint: self.service_endpoint,
extra: self.extra,
})
}
}

impl<E> ServiceBuilderWithServiceType<E> {
pub fn add_service_type(
mut self,
service_type: String,
) -> Result<Self, DidDocumentBuilderError> {
if service_type.is_empty() {
return Err(DidDocumentBuilderError::MissingField("type"));
}
self.service_type.insert(service_type);
Ok(self)
}

pub fn build(self) -> Service<E> {
let service_type = match self.service_type.len() {
// SAFETY: The only way to get to this state is to add at least one service type
0 => unreachable!(),
// SAFETY: We know that the length is non-zero
1 => OneOrList::One(self.service_type.into_iter().next().unwrap()),
_ => OneOrList::List(self.service_type.into_iter().collect()),
};
Expand Down
10 changes: 6 additions & 4 deletions did_resolver_sov/src/resolution/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,15 @@ pub(super) async fn ledger_response_to_ddo<E: Default>(
.filter(|t| *t != DidSovServiceType::Unknown)
.map(|t| t.to_string())
.collect();
Service::builder(
let mut builder = Service::builder(
service_id,
endpoint.endpoint.as_str().try_into()?,
Default::default(),
)
.add_service_types(service_types)?
.build()
);
for service_type in service_types {
builder = builder.add_service_type(service_type)?;
}
builder.build()
};

// TODO: Use multibase instead of base58
Expand Down

0 comments on commit d96d585

Please sign in to comment.