Skip to content

Commit

Permalink
Change key type
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 9, 2023
1 parent 4d2b3f9 commit 51392ce
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
18 changes: 9 additions & 9 deletions did_doc_sov/src/extra_fields/didcommv1.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use serde::{Deserialize, Serialize};

use super::AcceptType;
use super::{AcceptType, KeyKind};

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
#[serde(rename_all = "camelCase")]
pub struct ExtraFieldsDidCommV1 {
priority: u32,
recipient_keys: Vec<String>,
routing_keys: Vec<String>,
recipient_keys: Vec<KeyKind>,
routing_keys: Vec<KeyKind>,
accept: Vec<AcceptType>,
}

Expand All @@ -20,11 +20,11 @@ impl ExtraFieldsDidCommV1 {
self.priority
}

pub fn recipient_keys(&self) -> &[String] {
pub fn recipient_keys(&self) -> &[KeyKind] {
self.recipient_keys.as_ref()
}

pub fn routing_keys(&self) -> &[String] {
pub fn routing_keys(&self) -> &[KeyKind] {
self.routing_keys.as_ref()
}

Expand All @@ -36,8 +36,8 @@ impl ExtraFieldsDidCommV1 {
#[derive(Default)]
pub struct ExtraFieldsDidCommV1Builder {
priority: u32,
recipient_keys: Vec<String>,
routing_keys: Vec<String>,
recipient_keys: Vec<KeyKind>,
routing_keys: Vec<KeyKind>,
}

impl ExtraFieldsDidCommV1Builder {
Expand All @@ -46,12 +46,12 @@ impl ExtraFieldsDidCommV1Builder {
self
}

pub fn set_recipient_keys(mut self, recipient_keys: Vec<String>) -> Self {
pub fn set_recipient_keys(mut self, recipient_keys: Vec<KeyKind>) -> Self {
self.recipient_keys = recipient_keys;
self
}

pub fn set_routing_keys(mut self, routing_keys: Vec<String>) -> Self {
pub fn set_routing_keys(mut self, routing_keys: Vec<KeyKind>) -> Self {
self.routing_keys = routing_keys;
self
}
Expand Down
10 changes: 5 additions & 5 deletions did_doc_sov/src/extra_fields/didcommv2.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use serde::{Deserialize, Serialize};

use super::AcceptType;
use super::{AcceptType, KeyKind};

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
#[serde(rename_all = "camelCase")]
pub struct ExtraFieldsDidCommV2 {
accept: Vec<AcceptType>,
routing_keys: Vec<String>,
routing_keys: Vec<KeyKind>,
}

impl ExtraFieldsDidCommV2 {
Expand All @@ -18,18 +18,18 @@ impl ExtraFieldsDidCommV2 {
self.accept.as_ref()
}

pub fn routing_keys(&self) -> &[String] {
pub fn routing_keys(&self) -> &[KeyKind] {
self.routing_keys.as_ref()
}
}

#[derive(Default)]
pub struct ExtraFieldsDidCommV2Builder {
routing_keys: Vec<String>,
routing_keys: Vec<KeyKind>,
}

impl ExtraFieldsDidCommV2Builder {
pub fn set_routing_keys(mut self, routing_keys: Vec<String>) -> Self {
pub fn set_routing_keys(mut self, routing_keys: Vec<KeyKind>) -> Self {
self.routing_keys = routing_keys;
self
}
Expand Down
23 changes: 19 additions & 4 deletions did_doc_sov/src/extra_fields/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::fmt::Display;

use did_doc::did_parser::DidUrl;
use serde::{Deserialize, Deserializer, Serialize};

use crate::error::DidDocumentSovError;
Expand All @@ -24,6 +25,7 @@ impl Display for AcceptType {
}
}
}

impl<'de> Deserialize<'de> for AcceptType {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
Expand All @@ -38,6 +40,19 @@ impl<'de> Deserialize<'de> for AcceptType {
}
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(untagged)]
pub enum KeyKind {
Reference(DidUrl),
Value(String),
}

impl Default for KeyKind {
fn default() -> Self {
KeyKind::Value(String::default())
}
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(untagged)]
pub enum ExtraFields {
Expand All @@ -53,7 +68,7 @@ impl Default for ExtraFields {
}

impl ExtraFields {
pub fn recipient_keys(&self) -> Result<&[String], DidDocumentSovError> {
pub fn recipient_keys(&self) -> Result<&[KeyKind], DidDocumentSovError> {
match self {
ExtraFields::DIDCommV1(extra) => Ok(extra.recipient_keys()),
ExtraFields::AIP1(_) | ExtraFields::DIDCommV2(_) => {
Expand All @@ -62,21 +77,21 @@ impl ExtraFields {
}
}

pub fn routing_keys(&self) -> Result<&[String], DidDocumentSovError> {
pub fn routing_keys(&self) -> Result<&[KeyKind], DidDocumentSovError> {
match self {
ExtraFields::DIDCommV1(extra) => Ok(extra.routing_keys()),
ExtraFields::DIDCommV2(extra) => Ok(extra.routing_keys()),
ExtraFields::AIP1(_) => Err(DidDocumentSovError::EmptyCollection("routing_keys")),
}
}

pub fn first_recipient_key(&self) -> Result<&String, DidDocumentSovError> {
pub fn first_recipient_key(&self) -> Result<&KeyKind, DidDocumentSovError> {
self.recipient_keys()?
.first()
.ok_or(DidDocumentSovError::EmptyCollection("recipient_keys"))
}

pub fn first_routing_key(&self) -> Result<&String, DidDocumentSovError> {
pub fn first_routing_key(&self) -> Result<&KeyKind, DidDocumentSovError> {
self.routing_keys()?
.first()
.ok_or(DidDocumentSovError::EmptyCollection("routing_keys"))
Expand Down

0 comments on commit 51392ce

Please sign in to comment.