From 68c5747f0f2ca5f50f231b78ee46313cdc8b26b7 Mon Sep 17 00:00:00 2001 From: Hinton Date: Fri, 20 Oct 2023 11:49:02 +0200 Subject: [PATCH] Migrate more to use try_from --- crates/bitwarden/src/admin_console/policy.rs | 20 ++++++++++++-------- crates/bitwarden/src/platform/domain.rs | 18 +++++++++++------- crates/bitwarden/src/platform/sync.rs | 19 ++++++++++--------- crates/bitwarden/src/vault/cipher/cipher.rs | 2 +- 4 files changed, 34 insertions(+), 25 deletions(-) diff --git a/crates/bitwarden/src/admin_console/policy.rs b/crates/bitwarden/src/admin_console/policy.rs index 0bc12a9f5..df8e57dd1 100644 --- a/crates/bitwarden/src/admin_console/policy.rs +++ b/crates/bitwarden/src/admin_console/policy.rs @@ -5,6 +5,8 @@ use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use uuid::Uuid; +use crate::error::{Error, Result}; + #[derive(Serialize, Deserialize, Debug, JsonSchema)] pub struct Policy { id: Uuid, @@ -30,15 +32,17 @@ pub enum PolicyType { ActivateAutofill = 11, // Activates autofill with page load on the browser extension } -impl From for Policy { - fn from(policy: PolicyResponseModel) -> Self { - Policy { - id: policy.id.unwrap(), - organization_id: policy.organization_id.unwrap(), - r#type: policy.r#type.unwrap().into(), +impl TryFrom for Policy { + type Error = Error; + + fn try_from(policy: PolicyResponseModel) -> Result { + Ok(Self { + id: policy.id.ok_or(Error::MissingFields)?, + organization_id: policy.organization_id.ok_or(Error::MissingFields)?, + r#type: policy.r#type.ok_or(Error::MissingFields)?.into(), data: policy.data, - enabled: policy.enabled.unwrap(), - } + enabled: policy.enabled.ok_or(Error::MissingFields)?, + }) } } diff --git a/crates/bitwarden/src/platform/domain.rs b/crates/bitwarden/src/platform/domain.rs index 9ed28017b..c903dd5a5 100644 --- a/crates/bitwarden/src/platform/domain.rs +++ b/crates/bitwarden/src/platform/domain.rs @@ -1,6 +1,8 @@ use schemars::JsonSchema; use serde::{Deserialize, Serialize}; +use crate::error::{Error, Result}; + #[derive(Serialize, Deserialize, Debug, JsonSchema)] pub struct GlobalDomains { pub r#type: i32, @@ -8,12 +10,14 @@ pub struct GlobalDomains { pub excluded: bool, } -impl From for GlobalDomains { - fn from(global_domains: bitwarden_api_api::models::GlobalDomains) -> Self { - GlobalDomains { - r#type: global_domains.r#type.unwrap(), - domains: global_domains.domains.unwrap(), - excluded: global_domains.excluded.unwrap(), - } +impl TryFrom for GlobalDomains { + type Error = Error; + + fn try_from(global_domains: bitwarden_api_api::models::GlobalDomains) -> Result { + Ok(Self { + r#type: global_domains.r#type.ok_or(Error::MissingFields)?, + domains: global_domains.domains.ok_or(Error::MissingFields)?, + excluded: global_domains.excluded.ok_or(Error::MissingFields)?, + }) } } diff --git a/crates/bitwarden/src/platform/sync.rs b/crates/bitwarden/src/platform/sync.rs index 6d5c73c4c..c7aded18a 100644 --- a/crates/bitwarden/src/platform/sync.rs +++ b/crates/bitwarden/src/platform/sync.rs @@ -107,13 +107,13 @@ impl SyncResponse { .into_iter() .map(|c| c.try_into()) .collect::>>()?, - domains: response.domains.map(|d| (*d).into()), + domains: response.domains.map(|d| (*d).try_into()).transpose()?, policies: response .policies .ok_or(Error::MissingFields)? .into_iter() - .map(|p| p.into()) - .collect(), + .map(|p| p.try_into()) + .collect::>>()?, sends: response .sends .ok_or(Error::MissingFields)? @@ -155,16 +155,17 @@ impl ProfileResponse { } } -impl From for DomainResponse { - fn from(value: DomainsResponseModel) -> Self { - DomainResponse { +impl TryFrom for DomainResponse { + type Error = Error; + fn try_from(value: DomainsResponseModel) -> Result { + Ok(Self { equivalent_domains: value.equivalent_domains.unwrap_or_default(), global_equivalent_domains: value .global_equivalent_domains .unwrap_or_default() .into_iter() - .map(|s| s.into()) - .collect(), - } + .map(|s| s.try_into()) + .collect::>>()?, + }) } } diff --git a/crates/bitwarden/src/vault/cipher/cipher.rs b/crates/bitwarden/src/vault/cipher/cipher.rs index d869024b9..5a569b1f7 100644 --- a/crates/bitwarden/src/vault/cipher/cipher.rs +++ b/crates/bitwarden/src/vault/cipher/cipher.rs @@ -328,7 +328,7 @@ impl TryFrom for Cipher { .password_history .map(|p| p.into_iter().map(|p| p.into()).collect()), creation_date: cipher.creation_date.ok_or(Error::MissingFields)?.parse()?, - deleted_date: cipher.deleted_date.map(|d| d.parse().unwrap()), + deleted_date: cipher.deleted_date.map(|d| d.parse()).transpose()?, revision_date: cipher.revision_date.ok_or(Error::MissingFields)?.parse()?, }) }