Skip to content

Commit

Permalink
Migrate more to use try_from
Browse files Browse the repository at this point in the history
  • Loading branch information
Hinton committed Oct 20, 2023
1 parent ffa7ea6 commit 68c5747
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 25 deletions.
20 changes: 12 additions & 8 deletions crates/bitwarden/src/admin_console/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -30,15 +32,17 @@ pub enum PolicyType {
ActivateAutofill = 11, // Activates autofill with page load on the browser extension
}

impl From<PolicyResponseModel> 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<PolicyResponseModel> for Policy {
type Error = Error;

fn try_from(policy: PolicyResponseModel) -> Result<Self> {
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)?,
})
}
}

Expand Down
18 changes: 11 additions & 7 deletions crates/bitwarden/src/platform/domain.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::error::{Error, Result};

#[derive(Serialize, Deserialize, Debug, JsonSchema)]
pub struct GlobalDomains {
pub r#type: i32,
pub domains: Vec<String>,
pub excluded: bool,
}

impl From<bitwarden_api_api::models::GlobalDomains> 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<bitwarden_api_api::models::GlobalDomains> for GlobalDomains {
type Error = Error;

fn try_from(global_domains: bitwarden_api_api::models::GlobalDomains) -> Result<Self> {
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)?,
})
}
}
19 changes: 10 additions & 9 deletions crates/bitwarden/src/platform/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,13 @@ impl SyncResponse {
.into_iter()
.map(|c| c.try_into())
.collect::<Result<Vec<Cipher>>>()?,
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::<Result<Vec<Policy>>>()?,
sends: response
.sends
.ok_or(Error::MissingFields)?
Expand Down Expand Up @@ -155,16 +155,17 @@ impl ProfileResponse {
}
}

impl From<DomainsResponseModel> for DomainResponse {
fn from(value: DomainsResponseModel) -> Self {
DomainResponse {
impl TryFrom<DomainsResponseModel> for DomainResponse {
type Error = Error;
fn try_from(value: DomainsResponseModel) -> Result<Self> {
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::<Result<Vec<GlobalDomains>>>()?,
})
}
}
2 changes: 1 addition & 1 deletion crates/bitwarden/src/vault/cipher/cipher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ impl TryFrom<CipherDetailsResponseModel> 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()?,
})
}
Expand Down

0 comments on commit 68c5747

Please sign in to comment.