Skip to content

Commit

Permalink
feat: Improve ConversionError
Browse files Browse the repository at this point in the history
  • Loading branch information
FabioPinheiro committed Dec 18, 2023
1 parent 467e4d0 commit 0aeefdd
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 57 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ local.properties
anoncred-kmm/anoncred-wrapper-rust/src/*
**/.DS_Store
anoncred-kmm/anoncred-kmm/anoncred-wrapper-rust/src/*
uniffi/output-frameworks/anoncreds-jvm/bin/main/anoncreds.kt
2 changes: 1 addition & 1 deletion uniffi/src/prover/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl Prover {
let mut mutable_credential = (*credential)
.core
.try_clone()
.map_err(|_| AnoncredsError::ConversionError)?;
.map_err(|err| AnoncredsError::ConversionError(err.to_string()))?;
prover::process_credential(
&mut mutable_credential,
&cred_request_metadata.into(),
Expand Down
38 changes: 19 additions & 19 deletions uniffi/src/types/cred_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ impl TryFrom<AnoncredsCredentialDefinitionData> for CredentialDefinitionData {
type Error = AnoncredsError;

fn try_from(acr: AnoncredsCredentialDefinitionData) -> Result<Self, Self::Error> {
let primary = serde_json::to_string(&acr.primary).map_err(|_| AnoncredsError::ConversionError)?;
let primary = serde_json::to_string(&acr.primary).map_err(|err| AnoncredsError::ConversionError(err.to_string()))?;

let revocation = if let Some(rev_key) = &acr.revocation {
Some(serde_json::to_string(rev_key).map_err(|_| AnoncredsError::ConversionError)?)
Some(serde_json::to_string(rev_key).map_err(|err| AnoncredsError::ConversionError(err.to_string()))?)
} else {
None
};
Expand All @@ -41,11 +41,11 @@ impl TryInto<AnoncredsCredentialDefinitionData> for CredentialDefinitionData {

fn try_into(self) -> Result<AnoncredsCredentialDefinitionData, Self::Error> {
let primary: ursa::cl::CredentialPrimaryPublicKey = serde_json::from_str(&self.primary)
.map_err(|_| AnoncredsError::ConversionError)?;
.map_err(|err| AnoncredsError::ConversionError(err.to_string()))?;

let revocation = match &self.revocation {
Some(rev_key_str) => Some(serde_json::from_str(rev_key_str)
.map_err(|_| AnoncredsError::ConversionError)?),
.map_err(|err| AnoncredsError::ConversionError(err.to_string()))?),
None => None,
};

Expand All @@ -62,7 +62,7 @@ pub struct CredentialDefinition {

impl CredentialDefinition {
pub fn new(json_string: String) -> Result<Self, AnoncredsError> {
let core_def: AnoncredsCredentialDefinition = serde_json::from_str(&json_string).map_err(|_| AnoncredsError::ConversionError)?;
let core_def: AnoncredsCredentialDefinition = serde_json::from_str(&json_string).map_err(|err| AnoncredsError::ConversionError(err.to_string()))?;
return Ok(CredentialDefinition { core: core_def })
}

Expand All @@ -83,7 +83,7 @@ impl CredentialDefinition {
}

pub fn get_json(&self) -> Result<String, AnoncredsError> {
serde_json::to_string(&self.core).map_err(|_| AnoncredsError::ConversionError)
serde_json::to_string(&self.core).map_err(|err| AnoncredsError::ConversionError(err.to_string()))
}
}

Expand All @@ -95,8 +95,8 @@ impl TryInto<AnoncredsCredentialDefinition> for CredentialDefinition {
let cloned_signature_type = self.core.signature_type.clone();
let cloned_get_tag = self.core.tag.clone();
let cloned_issuer_id = self.core.issuer_id.clone();
let json_value = serde_json::to_string(&self.core.value).map_err(|_| AnoncredsError::ConversionError)?;
let cloned_value = serde_json::from_str(&json_value).map_err(|_| AnoncredsError::ConversionError)?;
let json_value = serde_json::to_string(&self.core.value).map_err(|err| AnoncredsError::ConversionError(err.to_string()))?;
let cloned_value = serde_json::from_str(&json_value).map_err(|err| AnoncredsError::ConversionError(err.to_string()))?;

let cloned_def = AnoncredsCredentialDefinition {
schema_id: cloned_schema_id,
Expand All @@ -117,8 +117,8 @@ impl TryFrom<AnoncredsCredentialDefinition> for CredentialDefinition {
let cloned_signature_type = acr.signature_type.clone();
let cloned_get_tag = acr.tag.clone();
let cloned_issuer_id = acr.issuer_id.clone();
let json_value = serde_json::to_string(&acr.value).map_err(|_| AnoncredsError::ConversionError)?;
let cloned_value = serde_json::from_str(&json_value).map_err(|_| AnoncredsError::ConversionError)?;
let json_value = serde_json::to_string(&acr.value).map_err(|err| AnoncredsError::ConversionError(err.to_string()))?;
let cloned_value = serde_json::from_str(&json_value).map_err(|err| AnoncredsError::ConversionError(err.to_string()))?;

let cloned_def = AnoncredsCredentialDefinition {
schema_id: cloned_schema_id,
Expand All @@ -139,8 +139,8 @@ impl TryFrom<&CredentialDefinition> for AnoncredsCredentialDefinition {
let cloned_signature_type = def.core.signature_type.clone();
let cloned_get_tag = def.core.tag.clone();
let cloned_issuer_id = def.core.issuer_id.clone();
let json_value = serde_json::to_string(&def.core.value).map_err(|_| AnoncredsError::ConversionError)?;
let cloned_value = serde_json::from_str(&json_value).map_err(|_| AnoncredsError::ConversionError)?;
let json_value = serde_json::to_string(&def.core.value).map_err(|err| AnoncredsError::ConversionError(err.to_string()))?;
let cloned_value = serde_json::from_str(&json_value).map_err(|err| AnoncredsError::ConversionError(err.to_string()))?;

return Ok(AnoncredsCredentialDefinition {
schema_id: cloned_schema_id,
Expand All @@ -158,12 +158,12 @@ pub struct CredentialDefinitionPrivate {

impl CredentialDefinitionPrivate {
pub fn new(json_string: String) -> Result<Self, AnoncredsError> {
let core_def: AnoncredsCredentialDefinitionPrivate = serde_json::from_str(&json_string).map_err(|_| AnoncredsError::ConversionError)?;
let core_def: AnoncredsCredentialDefinitionPrivate = serde_json::from_str(&json_string).map_err(|err| AnoncredsError::ConversionError(err.to_string()))?;
return Ok(CredentialDefinitionPrivate { core: core_def })
}

pub fn get_json(&self) -> Result<String, AnoncredsError> {
serde_json::to_string(&self.core).map_err(|_| AnoncredsError::ConversionError)
serde_json::to_string(&self.core).map_err(|err| AnoncredsError::ConversionError(err.to_string()))
}
}

Expand All @@ -177,8 +177,8 @@ impl TryFrom<&CredentialDefinitionPrivate> for AnoncredsCredentialDefinitionPriv
type Error = AnoncredsError;

fn try_from(def: &CredentialDefinitionPrivate) -> Result<AnoncredsCredentialDefinitionPrivate, Self::Error> {
let json_value = serde_json::to_string(&def.core.value).map_err(|_| AnoncredsError::ConversionError)?;
return serde_json::from_str(&json_value).map_err(|_| AnoncredsError::ConversionError)
let json_value = serde_json::to_string(&def.core.value).map_err(|err| AnoncredsError::ConversionError(err.to_string()))?;
return serde_json::from_str(&json_value).map_err(|err| AnoncredsError::ConversionError(err.to_string()))
}
}

Expand All @@ -188,12 +188,12 @@ pub struct CredentialKeyCorrectnessProof {

impl CredentialKeyCorrectnessProof {
pub fn new(json_string: String) -> Result<Self, AnoncredsError> {
let core_def: AnoncredsCredentialKeyCorrectnessProof = serde_json::from_str(&json_string).map_err(|_| AnoncredsError::ConversionError)?;
let core_def: AnoncredsCredentialKeyCorrectnessProof = serde_json::from_str(&json_string).map_err(|err| AnoncredsError::ConversionError(err.to_string()))?;
return Ok(CredentialKeyCorrectnessProof { core: core_def })
}

pub fn get_json(&self) -> Result<String, AnoncredsError> {
serde_json::to_string(&self.core).map_err(|_| AnoncredsError::ConversionError)
serde_json::to_string(&self.core).map_err(|err| AnoncredsError::ConversionError(err.to_string()))
}
}

Expand All @@ -207,6 +207,6 @@ impl TryFrom<&CredentialKeyCorrectnessProof> for AnoncredsCredentialKeyCorrectne
type Error = AnoncredsError;

fn try_from(def: &CredentialKeyCorrectnessProof) -> Result<AnoncredsCredentialKeyCorrectnessProof, Self::Error> {
return def.core.try_clone().map_err(|_| AnoncredsError::ConversionError)
return def.core.try_clone().map_err(|err| AnoncredsError::ConversionError(err.to_string()))
}
}
4 changes: 2 additions & 2 deletions uniffi/src/types/cred_offer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct CredentialOffer {

impl CredentialOffer {
pub fn new(json_string: String) -> Result<Self, AnoncredsError> {
let core_def: AnoncredsCredentialOffer = serde_json::from_str(&json_string).map_err(|_| AnoncredsError::ConversionError)?;
let core_def: AnoncredsCredentialOffer = serde_json::from_str(&json_string).map_err(|err| AnoncredsError::ConversionError(err.to_string()))?;
return Ok(CredentialOffer { core: core_def })
}

Expand All @@ -38,7 +38,7 @@ impl CredentialOffer {
}

pub fn get_json(&self) -> Result<String, AnoncredsError> {
serde_json::to_string(&self.core).map_err(|_| AnoncredsError::ConversionError)
serde_json::to_string(&self.core).map_err(|err| AnoncredsError::ConversionError(err.to_string()))
}
}

Expand Down
4 changes: 2 additions & 2 deletions uniffi/src/types/cred_req.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct CredentialRequest {
impl CredentialRequest {
pub fn new(json_string: String) -> Result<Self, AnoncredsError> {
let core_def: AnoncredsCredentialRequest =
serde_json::from_str(&json_string).map_err(|_| AnoncredsError::ConversionError)?;
serde_json::from_str(&json_string).map_err(|err| AnoncredsError::ConversionError(err.to_string()))?;
return Ok(CredentialRequest { core: core_def });
}

Expand All @@ -27,7 +27,7 @@ impl CredentialRequest {
}

pub fn get_json(&self) -> Result<String, AnoncredsError> {
serde_json::to_string(&self.core).map_err(|_| AnoncredsError::ConversionError)
serde_json::to_string(&self.core).map_err(|err| AnoncredsError::ConversionError(err.to_string()))
}
}

Expand Down
4 changes: 2 additions & 2 deletions uniffi/src/types/credential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct Credential {

impl Credential {
pub fn new(json_string: String) -> Result<Self, AnoncredsError> {
let core_def: AnoncredsCredential = serde_json::from_str(&json_string).map_err(|_| AnoncredsError::ConversionError)?;
let core_def: AnoncredsCredential = serde_json::from_str(&json_string).map_err(|err| AnoncredsError::ConversionError(err.to_string()))?;
return Ok(Credential { core: core_def })
}

Expand Down Expand Up @@ -48,6 +48,6 @@ impl Credential {
}

pub fn get_json(&self) -> Result<String, AnoncredsError> {
serde_json::to_string(&self.core).map_err(|_| AnoncredsError::ConversionError)
serde_json::to_string(&self.core).map_err(|err| AnoncredsError::ConversionError(err.to_string()))
}
}
4 changes: 2 additions & 2 deletions uniffi/src/types/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use serde::{Deserialize, Serialize};

#[derive(Debug, thiserror::Error, Serialize, Deserialize)]
pub enum AnoncredsError {
#[error("Conversion Error")]
ConversionError,
#[error("Conversion Error: {0}")]
ConversionError(String),
#[error("Something went wrong")]
SomethingWentWrong,
#[error("Create Credential Error: {0}")]
Expand Down
6 changes: 3 additions & 3 deletions uniffi/src/types/link_secret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl LinkSecret {

pub fn new_from_value(value_string: String) -> Result<Self, AnoncredsError> {
let core_def = AnoncredsLinkSecret::try_from(value_string.as_str())
.map_err(|_| AnoncredsError::ConversionError)?;
.map_err(|err| AnoncredsError::ConversionError(err.to_string()))?;
return Ok(LinkSecret { secret: core_def });
}

Expand All @@ -40,7 +40,7 @@ impl TryFrom<&str> for LinkSecret {

fn try_from(string: &str) -> Result<Self, Self::Error> {
let acr =
AnoncredsLinkSecret::try_from(string).map_err(|_| AnoncredsError::ConversionError)?;
AnoncredsLinkSecret::try_from(string).map_err(|err| AnoncredsError::ConversionError(err.to_string()))?;
return Ok(LinkSecret { secret: acr });
}
}
Expand All @@ -51,7 +51,7 @@ impl TryFrom<&LinkSecret> for AnoncredsLinkSecret {
fn try_from(acr: &LinkSecret) -> Result<Self, Self::Error> {
acr.secret
.try_clone()
.map_err(|_| AnoncredsError::ConversionError)
.map_err(|err| AnoncredsError::ConversionError(err.to_string()))
}
}

Expand Down
6 changes: 3 additions & 3 deletions uniffi/src/types/nonce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl Nonce {

pub fn new_from_value(value_string: String) -> Result<Self, AnoncredsError> {
let nonce = AnoncredsNonce::try_from(value_string.as_str())
.map_err(|_| AnoncredsError::ConversionError)?;
.map_err(|err| AnoncredsError::ConversionError(err.to_string()))?;
return Ok(Nonce {
anoncreds_nonce: nonce,
});
Expand All @@ -42,7 +42,7 @@ impl TryFrom<&Nonce> for AnoncredsNonce {
fn try_from(acr: &Nonce) -> Result<Self, Self::Error> {
acr.anoncreds_nonce
.try_clone()
.map_err(|_| AnoncredsError::ConversionError)
.map_err(|err| AnoncredsError::ConversionError(err.to_string()))
}
}

Expand All @@ -56,7 +56,7 @@ impl TryFrom<&str> for Nonce {
type Error = AnoncredsError;

fn try_from(value: &str) -> Result<Self, Self::Error> {
let nonce = AnoncredsNonce::try_from(value).map_err(|_| AnoncredsError::ConversionError)?;
let nonce = AnoncredsNonce::try_from(value).map_err(|err| AnoncredsError::ConversionError(err.to_string()))?;
return Ok(Nonce {
anoncreds_nonce: nonce,
});
Expand Down
10 changes: 5 additions & 5 deletions uniffi/src/types/presentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ pub struct Presentation {

impl Presentation {
pub fn new(json_string: String) -> Result<Self, AnoncredsError> {
let core_def: AnoncredsPresentation =
serde_json::from_str(&json_string).map_err(|_| AnoncredsError::ConversionError)?;
let core_def: AnoncredsPresentation = serde_json::from_str(&json_string)
.map_err(|err| AnoncredsError::ConversionError(err.to_string()))?;
return Ok(Presentation { core: core_def });
}

pub fn get_json(&self) -> Result<String, AnoncredsError> {
serde_json::to_string(&self.core).map_err(|_| AnoncredsError::ConversionError)
serde_json::to_string(&self.core).map_err(|err| AnoncredsError::ConversionError(err.to_string()))
}
}

Expand All @@ -26,11 +26,11 @@ pub struct PresentationRequest {
impl PresentationRequest {
pub fn new(json_string: String) -> Result<Self, AnoncredsError> {
let core_def: AnoncredsPresentationRequest =
serde_json::from_str(&json_string).map_err(|_| AnoncredsError::ConversionError)?;
serde_json::from_str(&json_string).map_err(|err| AnoncredsError::ConversionError(err.to_string()))?;
return Ok(PresentationRequest { core: core_def });
}

pub fn get_json(&self) -> Result<String, AnoncredsError> {
serde_json::to_string(&self.core).map_err(|_| AnoncredsError::ConversionError)
serde_json::to_string(&self.core).map_err(|err| AnoncredsError::ConversionError(err.to_string()))
}
}
Loading

0 comments on commit 0aeefdd

Please sign in to comment.