Skip to content

Commit

Permalink
Make tokio optional
Browse files Browse the repository at this point in the history
  • Loading branch information
Hinton committed Aug 12, 2024
1 parent 31d5886 commit 3d24452
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 26 deletions.
6 changes: 3 additions & 3 deletions crates/bitwarden-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ uniffi = [
"dep:p256",
] # Uniffi bindings
secrets = [] # Secrets manager API
state = ["dep:bitwarden-db"] # Persistent state
state = ["dep:bitwarden-db", "dep:tokio"] # Persistent state

[dependencies]
async-trait = ">=0.1.80, <0.2"
Expand Down Expand Up @@ -60,7 +60,7 @@ serde_repr = ">=0.1.12, <0.2"
sha1 = ">=0.10.5, <0.11"
sha2 = ">=0.10.6, <0.11"
thiserror = ">=1.0.40, <2.0"
tokio = { workspace = true }
tokio = { workspace = true, optional = true }
uniffi = { version = "=0.28.0", optional = true, features = ["tokio"] }
uuid = { version = ">=1.3.3, <2.0", features = ["serde"] }
validator = { version = "0.18.1", features = ["derive"] }
Expand Down Expand Up @@ -90,8 +90,8 @@ reqwest = { version = ">=0.12.5, <0.13", features = [
security-framework = { version = "=2.10" }

[dev-dependencies]
bitwarden-crypto = { workspace = true }
rand_chacha = "0.3.1"
tokio = { workspace = true }
wiremock = "0.6.0"
zeroize = { version = ">=1.7.0, <2.0", features = ["derive", "aarch64"] }

Expand Down
2 changes: 1 addition & 1 deletion crates/bitwarden-core/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::sync::{Arc, RwLock};
#[cfg(feature = "state")]
use bitwarden_db::{Database, Migration, Migrator};
use reqwest::header::{self, HeaderValue};
#[cfg(feature = "internal")]
#[cfg(feature = "state")]
use tokio::sync::Mutex;

use super::internal::InternalClient;
Expand Down
2 changes: 1 addition & 1 deletion crates/bitwarden-core/src/client/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use bitwarden_crypto::{AsymmetricEncString, EncString, Kdf, MasterKey, PinKey};
#[cfg(feature = "state")]
use bitwarden_db::Database;
use chrono::Utc;
#[cfg(feature = "internal")]
#[cfg(feature = "state")]
use tokio::sync::Mutex;
use uuid::Uuid;

Expand Down
7 changes: 5 additions & 2 deletions crates/bitwarden-vault/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ uniffi = [
"bitwarden-crypto/uniffi",
"dep:uniffi",
] # Uniffi bindings
state = ["bitwarden-core/state"] # Persistent state
state = ["bitwarden-core/state", "dep:tokio"] # Persistent state

[dependencies]
base64 = ">=0.22.1, <0.23"
Expand All @@ -41,9 +41,12 @@ serde_repr = ">=0.1.12, <0.2"
sha1 = ">=0.10.5, <0.11"
sha2 = ">=0.10.6, <0.11"
thiserror = ">=1.0.40, <2.0"
tokio = { workspace = true }
tokio = { workspace = true, optional = true }
uniffi = { version = "=0.28.0", optional = true }
uuid = { version = ">=1.3.3, <2.0", features = ["serde"] }

[dev-dependencies]
tokio = { workspace = true }

[lints]
workspace = true
1 change: 1 addition & 0 deletions crates/bitwarden-vault/src/cipher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub(crate) mod identity;
pub(crate) mod linked_id;
pub(crate) mod local_data;
pub(crate) mod login;
#[cfg(feature = "state")]
pub(crate) mod repository;
pub(crate) mod secure_note;

Expand Down
15 changes: 9 additions & 6 deletions crates/bitwarden-vault/src/cipher/repository.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::sync::Arc;

use bitwarden_core::{require, MissingFieldError};
use bitwarden_db::{named_params, params, Database, DatabaseError, DatabaseTrait};
use thiserror::Error;
use tokio::sync::Mutex;
Expand All @@ -16,6 +17,8 @@ struct CipherRow {

#[derive(Debug, Error)]
pub enum CipherRepositoryError {
#[error(transparent)]
MissingFieldError(#[from] MissingFieldError),
#[error(transparent)]
DatabaseError(#[from] DatabaseError),
#[error(transparent)]
Expand All @@ -32,8 +35,8 @@ impl CipherRepository {
}

pub async fn save(&self, cipher: &Cipher) -> Result<(), CipherRepositoryError> {
let id = cipher.id.unwrap();
let serialized = serde_json::to_string(cipher).unwrap();
let id = require!(cipher.id);
let serialized = serde_json::to_string(cipher)?;

let guard = self.db.lock().await;

Expand All @@ -58,7 +61,7 @@ impl CipherRepository {

for cipher in ciphers {
let id = cipher.id.unwrap();
let serialized = serde_json::to_string(&cipher).unwrap();
let serialized = serde_json::to_string(&cipher)?;

guard
.execute(
Expand All @@ -84,7 +87,7 @@ impl CipherRepository {
pub async fn get_all(&self) -> Result<Vec<Cipher>, CipherRepositoryError> {
let guard = self.db.lock().await;

let rows = guard
let rows: Result<Vec<Cipher>, _> = guard
.query_map(
"SELECT id, value FROM ciphers",
[],
Expand All @@ -97,10 +100,10 @@ impl CipherRepository {
)
.await?
.iter()
.map(|row| serde_json::from_str(&row.value).unwrap())
.map(|row| serde_json::from_str(&row.value))
.collect();

Ok(rows)
Ok(rows?)
}
}

Expand Down
8 changes: 5 additions & 3 deletions crates/bitwarden-vault/src/client_vault.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
use bitwarden_core::Client;

#[cfg(feature = "state")]
use crate::repository::CipherRepository;
use crate::{
repository::CipherRepository,
sync::{sync, SyncError},
SyncRequest, SyncResponse,
};

pub struct ClientVault<'a> {
pub(crate) client: &'a Client,
#[cfg(feature = "state")]
pub cipher_repository: CipherRepository,
}

impl<'a> ClientVault<'a> {
pub fn new(client: &'a Client) -> Self {
let t = client.internal.db.clone();
Self {
client,
cipher_repository: CipherRepository::new(t),
#[cfg(feature = "state")]
cipher_repository: CipherRepository::new(client.internal.db.clone()),
}
}

Expand Down
11 changes: 1 addition & 10 deletions crates/bitwarden-vault/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use serde::{Deserialize, Serialize};
use thiserror::Error;
use uuid::Uuid;

use crate::{Cipher, ClientVaultExt, Collection, Folder, GlobalDomains, VaultParseError};
use crate::{Cipher, Collection, Folder, GlobalDomains, VaultParseError};

#[derive(Debug, Error)]
pub enum SyncError {
Expand Down Expand Up @@ -48,15 +48,6 @@ pub(crate) async fn sync(client: &Client, input: &SyncRequest) -> Result<SyncRes

let res = SyncResponse::process_response(sync, &enc)?;

let ciphers = res.ciphers.as_slice();

client
.vault()
.cipher_repository
.replace_all(ciphers)
.await
.unwrap();

Ok(res)
}

Expand Down

0 comments on commit 3d24452

Please sign in to comment.