Skip to content

Commit

Permalink
Remove unnecessary bound on ddo service (#879)
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 Jun 9, 2023
1 parent 65cc7a4 commit f17c05a
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 67 deletions.
52 changes: 31 additions & 21 deletions did_doc/src/schema/did_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ type ControllerAlias = OneOrList<Did>;
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
#[serde(default)]
#[serde(rename_all = "camelCase")]
pub struct DidDocument<E>
where
E: Default,
{
pub struct DidDocument<E> {
id: Did,
#[serde(skip_serializing_if = "Vec::is_empty")]
also_known_as: Vec<Uri>,
Expand All @@ -46,10 +43,7 @@ where
extra: HashMap<String, Value>,
}

impl<E> DidDocument<E>
where
E: Default,
{
impl<E> DidDocument<E> {
pub fn builder(id: Did) -> DidDocumentBuilder<E> {
DidDocumentBuilder::new(id)
}
Expand Down Expand Up @@ -103,11 +97,8 @@ where
}
}

#[derive(Debug, Default)]
pub struct DidDocumentBuilder<E>
where
E: Default,
{
#[derive(Debug)]
pub struct DidDocumentBuilder<E> {
id: Did,
also_known_as: Vec<Uri>,
controller: Vec<Did>,
Expand All @@ -121,10 +112,25 @@ where
extra: HashMap<String, Value>,
}

impl<E> DidDocumentBuilder<E>
where
E: Default,
{
impl<E> Default for DidDocumentBuilder<E> {
fn default() -> Self {
Self {
id: Default::default(),
also_known_as: Default::default(),
controller: Default::default(),
verification_method: Default::default(),
authentication: Default::default(),
assertion_method: Default::default(),
key_agreement: Default::default(),
capability_invocation: Default::default(),
capability_delegation: Default::default(),
service: Default::default(),
extra: Default::default(),
}
}
}

impl<E> DidDocumentBuilder<E> {
pub fn new(id: Did) -> Self {
Self {
id,
Expand Down Expand Up @@ -267,10 +273,14 @@ mod tests {
let service_id = Uri::new("did:example:123456789abcdefghi;service-1").unwrap();
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())
.add_service_type(service_type)
.unwrap()
.build();
let service = ServiceBuilder::<()>::new(
service_id,
service_endpoint.try_into().unwrap(),
Default::default(),
)
.add_service_type(service_type)
.unwrap()
.build();

let document = DidDocumentBuilder::new(id.clone())
.add_also_known_as(also_known_as.clone())
Expand Down
80 changes: 37 additions & 43 deletions did_doc/src/schema/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ type ServiceTypeAlias = OneOrList<String>;

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Service<E>
where
E: Default,
{
pub struct Service<E> {
id: Uri,
#[serde(rename = "type")]
service_type: ServiceTypeAlias,
Expand All @@ -25,12 +22,9 @@ where
extra: E,
}

impl<E> Service<E>
where
E: Default,
{
pub fn builder(id: Uri, service_endpoint: Url) -> ServiceBuilder<E> {
ServiceBuilder::new(id, service_endpoint)
impl<E> Service<E> {
pub fn builder(id: Uri, service_endpoint: Url, extra: E) -> ServiceBuilder<E> {
ServiceBuilder::new(id, service_endpoint, extra)
}

pub fn id(&self) -> &Uri {
Expand Down Expand Up @@ -65,15 +59,12 @@ pub struct ServiceBuilderWithServiceType<E> {
extra: E,
}

impl<E> ServiceBuilder<E>
where
E: Default,
{
pub fn new(id: Uri, service_endpoint: Url) -> Self {
impl<E> ServiceBuilder<E> {
pub fn new(id: Uri, service_endpoint: Url, extra: E) -> Self {
Self {
id,
service_endpoint,
extra: E::default(),
extra,
}
}

Expand Down Expand Up @@ -111,10 +102,7 @@ where
}
}

impl<E> ServiceBuilderWithServiceType<E>
where
E: Default,
{
impl<E> ServiceBuilderWithServiceType<E> {
pub fn add_service_type(
mut self,
service_type: String,
Expand All @@ -126,11 +114,6 @@ where
Ok(self)
}

pub fn add_extra(mut self, extra: E) -> Self {
self.extra = extra;
self
}

pub fn build(self) -> Service<E> {
Service {
id: self.id,
Expand Down Expand Up @@ -163,11 +146,14 @@ mod tests {
let service_endpoint = "http://example.com/endpoint";
let service_type = "DIDCommMessaging".to_string();

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

assert_eq!(service.id(), &id);
assert_eq!(service.service_endpoint().as_ref(), service_endpoint);
Expand All @@ -187,11 +173,11 @@ mod tests {
routing_keys: routing_keys.clone(),
};

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

assert_eq!(service.extra().recipient_keys, recipient_keys);
assert_eq!(service.extra().routing_keys, routing_keys);
Expand All @@ -203,12 +189,16 @@ mod tests {
let service_endpoint = "http://example.com/endpoint";
let service_type = "DIDCommMessaging".to_string();

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

assert_eq!(service.service_type(), &OneOrList::List(vec![service_type]));
}
Expand All @@ -218,8 +208,12 @@ mod tests {
let id = create_valid_uri();
let service_endpoint = "http://example.com/endpoint";

let res = ServiceBuilder::<ExtraSov>::new(id, service_endpoint.try_into().unwrap())
.add_service_type("".to_string());
let res = ServiceBuilder::<ExtraSov>::new(
id,
service_endpoint.try_into().unwrap(),
Default::default(),
)
.add_service_type("".to_string());
assert!(res.is_err());
}

Expand Down
3 changes: 3 additions & 0 deletions did_resolver_sov/src/dereferencing/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ mod tests {
let agent_service = Service::builder(
"did:example:123456789abcdefghi#agent".parse().unwrap(),
"https://agent.example.com/8377464".try_into().unwrap(),
Default::default(),
)
.add_service_type("AgentService".to_string())
.unwrap()
Expand All @@ -124,6 +125,7 @@ mod tests {
let messaging_service = Service::builder(
"did:example:123456789abcdefghi#messages".parse().unwrap(),
"https://example.com/messages/8377464".try_into().unwrap(),
Default::default(),
)
.add_service_type("MessagingService".to_string())
.unwrap()
Expand Down Expand Up @@ -202,6 +204,7 @@ mod tests {
let additional_service = Service::builder(
"did:example:123456789abcdefghi#keys-1".parse().unwrap(),
"https://example.com/duplicated/8377464".try_into().unwrap(),
Default::default(),
)
.add_service_type("DuplicatedService".to_string())
.unwrap()
Expand Down
10 changes: 7 additions & 3 deletions did_resolver_sov/src/resolution/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,13 @@ pub(super) async fn ledger_response_to_ddo<E: Default>(
.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()
Service::builder(
service_id,
endpoint.endpoint.as_str().try_into()?,
Default::default(),
)
.add_service_types(service_types)?
.build()
};

// TODO: Use multibase instead of base58
Expand Down

0 comments on commit f17c05a

Please sign in to comment.