Skip to content

Commit

Permalink
(refactor) small refactor to the new DIDExchange AnyXYZ wrappers (#1249)
Browse files Browse the repository at this point in the history
* remove deserialize from any wrappers

Signed-off-by: George Mulhearn <gmulhearn@anonyome.com>

* simply any wrappers

Signed-off-by: George Mulhearn <gmulhearn@anonyome.com>

---------

Signed-off-by: George Mulhearn <gmulhearn@anonyome.com>
Co-authored-by: George Mulhearn <gmulhearn@anonyome.com>
  • Loading branch information
gmulhearn and gmulhearn-anonyome authored Jul 3, 2024
1 parent 1a52699 commit c9b6597
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 164 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@ use serde::{Deserialize, Serialize};
use shared::misc::serde_ignored::SerdeIgnored as NoContent;
use typed_builder::TypedBuilder;

use super::DidExchangeV1MessageVariant;
use crate::{
decorators::{thread::Thread, timing::Timing},
msg_fields::protocols::did_exchange::{
v1_0::DidExchangeV1_0, v1_1::DidExchangeV1_1, DidExchange,
},
msg_parts::MsgParts,
msg_types::protocols::did_exchange::DidExchangeTypeV1,
AriesMessage,
};

/// Alias type for the shared DIDExchange v1.X complete message type.
Expand All @@ -29,41 +25,4 @@ pub struct CompleteDecorators {
pub timing: Option<Timing>,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
#[serde(untagged)]
pub enum AnyComplete {
V1_0(Complete),
V1_1(Complete),
}

impl AnyComplete {
pub fn get_version(&self) -> DidExchangeTypeV1 {
match self {
AnyComplete::V1_0(_) => DidExchangeTypeV1::new_v1_0(),
AnyComplete::V1_1(_) => DidExchangeTypeV1::new_v1_1(),
}
}
}

impl AnyComplete {
pub fn into_inner(self) -> Complete {
match self {
AnyComplete::V1_0(r) | AnyComplete::V1_1(r) => r,
}
}

pub fn inner(&self) -> &Complete {
match self {
AnyComplete::V1_0(r) | AnyComplete::V1_1(r) => r,
}
}
}

impl From<AnyComplete> for AriesMessage {
fn from(value: AnyComplete) -> Self {
match value {
AnyComplete::V1_0(inner) => DidExchange::V1_0(DidExchangeV1_0::Complete(inner)).into(),
AnyComplete::V1_1(inner) => DidExchange::V1_1(DidExchangeV1_1::Complete(inner)).into(),
}
}
}
pub type AnyComplete = DidExchangeV1MessageVariant<Complete, Complete>;
52 changes: 52 additions & 0 deletions aries/messages/src/msg_fields/protocols/did_exchange/v1_x/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,59 @@
//! Common components for V1.X DIDExchange messages (v1.0 & v1.1).
//! Necessary to prevent duplicated code, since most types between v1.0 & v1.1 are identical

use serde::Serialize;

use super::{v1_0::DidExchangeV1_0, v1_1::DidExchangeV1_1, DidExchange};
use crate::{msg_types::protocols::did_exchange::DidExchangeTypeV1, AriesMessage};

pub mod complete;
pub mod problem_report;
pub mod request;
pub mod response;

#[derive(Debug, Clone, Serialize, PartialEq)]
#[serde(untagged)]
pub enum DidExchangeV1MessageVariant<V1_0, V1_1> {
V1_0(V1_0),
V1_1(V1_1),
}

impl<A, B> DidExchangeV1MessageVariant<A, B> {
pub fn get_version(&self) -> DidExchangeTypeV1 {
match self {
DidExchangeV1MessageVariant::V1_0(_) => DidExchangeTypeV1::new_v1_0(),
DidExchangeV1MessageVariant::V1_1(_) => DidExchangeTypeV1::new_v1_1(),
}
}
}

impl<T> DidExchangeV1MessageVariant<T, T> {
pub fn into_inner(self) -> T {
match self {
DidExchangeV1MessageVariant::V1_0(r) | DidExchangeV1MessageVariant::V1_1(r) => r,
}
}

pub fn inner(&self) -> &T {
match self {
DidExchangeV1MessageVariant::V1_0(r) | DidExchangeV1MessageVariant::V1_1(r) => r,
}
}
}

impl<A, B> From<DidExchangeV1MessageVariant<A, B>> for AriesMessage
where
A: Into<DidExchangeV1_0>,
B: Into<DidExchangeV1_1>,
{
fn from(value: DidExchangeV1MessageVariant<A, B>) -> Self {
match value {
DidExchangeV1MessageVariant::V1_0(a) => {
AriesMessage::DidExchange(DidExchange::V1_0(a.into()))
}
DidExchangeV1MessageVariant::V1_1(b) => {
AriesMessage::DidExchange(DidExchange::V1_1(b.into()))
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
use serde::{Deserialize, Serialize};
use typed_builder::TypedBuilder;

use super::DidExchangeV1MessageVariant;
use crate::{
decorators::{localization::MsgLocalization, thread::Thread, timing::Timing},
msg_fields::protocols::did_exchange::{
v1_0::DidExchangeV1_0, v1_1::DidExchangeV1_1, DidExchange,
},
msg_parts::MsgParts,
msg_types::protocols::did_exchange::DidExchangeTypeV1,
AriesMessage,
};

/// Alias type for the shared DIDExchange v1.X problem report message type.
Expand Down Expand Up @@ -59,45 +55,4 @@ impl ProblemReportDecorators {
}
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
#[serde(untagged)]
pub enum AnyProblemReport {
V1_0(ProblemReport),
V1_1(ProblemReport),
}

impl AnyProblemReport {
pub fn get_version(&self) -> DidExchangeTypeV1 {
match self {
AnyProblemReport::V1_0(_) => DidExchangeTypeV1::new_v1_0(),
AnyProblemReport::V1_1(_) => DidExchangeTypeV1::new_v1_1(),
}
}
}

impl AnyProblemReport {
pub fn into_inner(self) -> ProblemReport {
match self {
AnyProblemReport::V1_0(r) | AnyProblemReport::V1_1(r) => r,
}
}

pub fn inner(&self) -> &ProblemReport {
match self {
AnyProblemReport::V1_0(r) | AnyProblemReport::V1_1(r) => r,
}
}
}

impl From<AnyProblemReport> for AriesMessage {
fn from(value: AnyProblemReport) -> Self {
match value {
AnyProblemReport::V1_0(inner) => {
DidExchange::V1_0(DidExchangeV1_0::ProblemReport(inner)).into()
}
AnyProblemReport::V1_1(inner) => {
DidExchange::V1_1(DidExchangeV1_1::ProblemReport(inner)).into()
}
}
}
}
pub type AnyProblemReport = DidExchangeV1MessageVariant<ProblemReport, ProblemReport>;
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@ use serde::{Deserialize, Serialize};
use shared::maybe_known::MaybeKnown;
use typed_builder::TypedBuilder;

use super::DidExchangeV1MessageVariant;
use crate::{
decorators::{
attachment::Attachment,
thread::{Thread, ThreadGoalCode},
timing::Timing,
},
msg_fields::protocols::did_exchange::{
v1_0::DidExchangeV1_0, v1_1::DidExchangeV1_1, DidExchange,
},
msg_parts::MsgParts,
msg_types::protocols::did_exchange::DidExchangeTypeV1,
AriesMessage,
};

/// Alias type for the shared DIDExchange v1.X request message type.
Expand Down Expand Up @@ -45,41 +41,4 @@ pub struct RequestDecorators {
pub timing: Option<Timing>,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
#[serde(untagged)]
pub enum AnyRequest {
V1_0(Request),
V1_1(Request),
}

impl AnyRequest {
pub fn get_version(&self) -> DidExchangeTypeV1 {
match self {
AnyRequest::V1_0(_) => DidExchangeTypeV1::new_v1_0(),
AnyRequest::V1_1(_) => DidExchangeTypeV1::new_v1_1(),
}
}
}

impl AnyRequest {
pub fn into_inner(self) -> Request {
match self {
AnyRequest::V1_0(r) | AnyRequest::V1_1(r) => r,
}
}

pub fn inner(&self) -> &Request {
match self {
AnyRequest::V1_0(r) | AnyRequest::V1_1(r) => r,
}
}
}

impl From<AnyRequest> for AriesMessage {
fn from(value: AnyRequest) -> Self {
match value {
AnyRequest::V1_0(inner) => DidExchange::V1_0(DidExchangeV1_0::Request(inner)).into(),
AnyRequest::V1_1(inner) => DidExchange::V1_1(DidExchangeV1_1::Request(inner)).into(),
}
}
}
pub type AnyRequest = DidExchangeV1MessageVariant<Request, Request>;
Original file line number Diff line number Diff line change
@@ -1,35 +1,18 @@
use serde::{Deserialize, Serialize};
use typed_builder::TypedBuilder;

use super::DidExchangeV1MessageVariant;
use crate::{
decorators::{thread::Thread, timing::Timing},
msg_fields::protocols::did_exchange::{
v1_0::{response::Response as ResponseV1_0, DidExchangeV1_0},
v1_1::{
response::{Response as ResponseV1_1, ResponseContent as ResponseV1_1Content},
DidExchangeV1_1,
},
DidExchange,
v1_0::response::Response as ResponseV1_0,
v1_1::response::{Response as ResponseV1_1, ResponseContent as ResponseV1_1Content},
},
msg_types::protocols::did_exchange::DidExchangeTypeV1,
AriesMessage,
};

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, derive_more::From)]
#[serde(untagged)]
pub enum AnyResponse {
V1_0(ResponseV1_0),
V1_1(ResponseV1_1),
}
pub type AnyResponse = DidExchangeV1MessageVariant<ResponseV1_0, ResponseV1_1>;

impl AnyResponse {
pub fn get_version(&self) -> DidExchangeTypeV1 {
match self {
AnyResponse::V1_0(_) => DidExchangeTypeV1::new_v1_0(),
AnyResponse::V1_1(_) => DidExchangeTypeV1::new_v1_1(),
}
}

pub fn into_v1_1(self) -> ResponseV1_1 {
match self {
AnyResponse::V1_0(r) => r.into_v1_1(),
Expand All @@ -52,12 +35,15 @@ impl ResponseV1_0 {
}
}

impl From<AnyResponse> for AriesMessage {
fn from(value: AnyResponse) -> Self {
match value {
AnyResponse::V1_0(inner) => DidExchange::V1_0(DidExchangeV1_0::Response(inner)).into(),
AnyResponse::V1_1(inner) => DidExchange::V1_1(DidExchangeV1_1::Response(inner)).into(),
}
impl From<ResponseV1_0> for AnyResponse {
fn from(value: ResponseV1_0) -> Self {
Self::V1_0(value)
}
}

impl From<ResponseV1_1> for AnyResponse {
fn from(value: ResponseV1_1) -> Self {
Self::V1_1(value)
}
}

Expand Down

0 comments on commit c9b6597

Please sign in to comment.