Skip to content

Commit

Permalink
add credential methods for type conversions
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Tate <[email protected]>
  • Loading branch information
Ryanmtate committed Sep 27, 2024
1 parent e7ce10a commit 1540216
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 25 deletions.
60 changes: 38 additions & 22 deletions src/credential/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,27 +159,15 @@ impl Credential {
}

/// Convert the parsed credential into a specialized JSON credential.
pub fn into_parsed(&self) -> Result<Arc<ParsedCredential>, CredentialDecodingError> {
match self.format {
CredentialFormat::MsoMdoc => {
Ok(ParsedCredential::new_mso_mdoc(self.to_owned().try_into()?))
}
CredentialFormat::JwtVcJson => Ok(ParsedCredential::new_jwt_vc_json(
self.to_owned().try_into()?,
)),
CredentialFormat::JwtVcJsonLd => Ok(ParsedCredential::new_jwt_vc_json_ld(
self.to_owned().try_into()?,
)),
CredentialFormat::SdJwtVcJsonLd => {
ParsedCredential::new_sd_jwt_vc_json(self.to_owned().into())
}
CredentialFormat::LdpVc => {
Ok(ParsedCredential::new_ldp_vc(self.to_owned().try_into()?))
}
_ => Err(CredentialDecodingError::UnsupportedCredentialFormat(
self.format.to_string(),
)),
}
pub fn try_into_parsed(&self) -> Result<Arc<ParsedCredential>, CredentialDecodingError> {
self.to_owned().try_into()
}

/// Returns the credential as a `Mdoc` credential.
pub fn try_into_mdoc(&self) -> Result<Arc<Mdoc>, CredentialDecodingError> {
self.to_owned()
.try_into()
.map_err(CredentialDecodingError::from)
}
}

Expand Down Expand Up @@ -301,7 +289,12 @@ impl ParsedCredential {
pub fn parse_from_credential(
credential: Arc<Credential>,
) -> Result<Arc<Self>, CredentialDecodingError> {
credential.into_parsed()
// NOTE: due to the Arc<Credential> type needed in the constructor,
// given the uniffi::Object trait, we need to have an inner reference
// to the credential that provided the type conversion, which avoids the
// TryFrom<Arc<Credential>> that cannot be implemented given the compiler
// constraints on foreign types.
credential.try_into_parsed()
}

/// Convert a parsed credential into the generic form for storage.
Expand Down Expand Up @@ -342,6 +335,29 @@ impl ParsedCredential {
}
}

impl TryFrom<Credential> for Arc<ParsedCredential> {
type Error = CredentialDecodingError;

fn try_from(credential: Credential) -> Result<Self, Self::Error> {
match credential.format {
CredentialFormat::MsoMdoc => Ok(ParsedCredential::new_mso_mdoc(credential.try_into()?)),
CredentialFormat::JwtVcJson => {
Ok(ParsedCredential::new_jwt_vc_json(credential.try_into()?))
}
CredentialFormat::JwtVcJsonLd => {
Ok(ParsedCredential::new_jwt_vc_json_ld(credential.try_into()?))
}
CredentialFormat::SdJwtVcJsonLd => {
ParsedCredential::new_sd_jwt_vc_json(credential.into())
}
CredentialFormat::LdpVc => Ok(ParsedCredential::new_ldp_vc(credential.try_into()?)),
_ => Err(CredentialDecodingError::UnsupportedCredentialFormat(
credential.format.to_string(),
)),
}
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, uniffi::Enum)]
pub enum VcdmVersion {
V1,
Expand Down
8 changes: 5 additions & 3 deletions src/mdl/holder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ pub async fn initialize_mdl_presentation(
value: "No credential with that ID in the VDC collection.".to_string(),
})?;

let mdoc: Arc<Mdoc> = document.try_into().map_err(|e| SessionError::Generic {
value: format!("Error retrieving MDoc from storage: {e:}"),
})?;
let mdoc: Arc<Mdoc> = document
.try_into_mdoc()
.map_err(|e| SessionError::Generic {
value: format!("Error retrieving MDoc from storage: {e:}"),
})?;
let drms = DeviceRetrievalMethods::new(DeviceRetrievalMethod::BLE(BleOptions {
peripheral_server_mode: None,
central_client_mode: Some(CentralClientMode { uuid }),
Expand Down

0 comments on commit 1540216

Please sign in to comment.