Skip to content

Commit

Permalink
Resolve some review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Hinton committed Sep 2, 2024
1 parent 0c0c4a7 commit d9e470e
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 17 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions crates/bitwarden-db-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,31 @@ extern "C" {
}
*/

// Ensure resources are cleaned up after tests run
#[cfg(not(target_arch = "wasm32"))]
struct TestResources;
#[cfg(not(target_arch = "wasm32"))]
impl Drop for TestResources {
fn drop(&mut self) {
let _ = std::fs::remove_file("test.sqlite");
}

Check warning on line 19 in crates/bitwarden-db-tests/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-db-tests/src/lib.rs#L17-L19

Added lines #L17 - L19 were not covered by tests
}

#[wasm_bindgen(js_name = runTests)]
pub async fn run_tests() {
#[cfg(not(target_arch = "wasm32"))]
let cleanup = TestResources;

std::panic::set_hook(Box::new(console_error_panic_hook::hook));
let db = Database::default().await.unwrap();

test_version(&db).await;
test_create_select(&db).await;

Check warning on line 31 in crates/bitwarden-db-tests/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-db-tests/src/lib.rs#L22-L31

Added lines #L22 - L31 were not covered by tests

print!("Ran tests");

#[cfg(not(target_arch = "wasm32"))]
core::hint::black_box(cleanup);
}

Check warning on line 37 in crates/bitwarden-db-tests/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-db-tests/src/lib.rs#L33-L37

Added lines #L33 - L37 were not covered by tests

pub async fn test_version(db: &Database) {
Expand Down
2 changes: 1 addition & 1 deletion crates/bitwarden-db/src/wasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ extern "C" {
type SqliteDatabase;

#[wasm_bindgen(static_method_of = SqliteDatabase)]
async fn factory(name: &str) -> JsValue;
async fn create(name: &str) -> JsValue;

#[wasm_bindgen(method)]
async fn get_version(this: &SqliteDatabase) -> JsValue;
Expand Down
1 change: 0 additions & 1 deletion crates/bitwarden-json/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ log = ">=0.4.18, <0.5"
schemars = ">=0.8.12, <0.9"
serde = { version = ">=1.0, <2.0", features = ["derive"] }
serde_json = ">=1.0.96, <2.0"
uuid = { version = ">=1.3.3, <2", features = ["serde", "v4"] }


[lints]
Expand Down
5 changes: 4 additions & 1 deletion crates/bitwarden-napi/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ export const enum LogLevel {
Error = 4,
}
export declare class BitwardenClient {
constructor(settingsInput?: string | undefined | null, logLevel?: LogLevel | undefined | null);
static create(
settingsInput?: string | undefined | null,
logLevel?: LogLevel | undefined | null,
): Promise<BitwardenClient>;
runCommand(commandInput: string): Promise<string>;
}
12 changes: 9 additions & 3 deletions crates/bitwarden-napi/src-ts/bitwarden_client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,17 @@ function handleResponse<T>(response: {
}

export class BitwardenClient {
client: rust.BitwardenClient;
private client: rust.BitwardenClient;

constructor(settings?: ClientSettings, loggingLevel?: LogLevel) {
static async create(settings?: ClientSettings, loggingLevel?: LogLevel) {
const settingsJson = settings == null ? null : Convert.clientSettingsToJson(settings);
this.client = new rust.BitwardenClient(settingsJson, loggingLevel ?? LogLevel.Info);
new BitwardenClient(
await rust.BitwardenClient.create(settingsJson, loggingLevel ?? LogLevel.Info),
);
}

private constructor(client: rust.BitwardenClient) {
this.client = client;
}

async accessTokenLogin(accessToken: string, stateFile?: string): Promise<void> {
Expand Down
11 changes: 3 additions & 8 deletions crates/bitwarden-napi/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,18 @@ pub struct BitwardenClient(JsonClient);

#[napi]
impl BitwardenClient {
#[napi(constructor)]
pub fn new(settings_input: Option<String>, log_level: Option<LogLevel>) -> Self {
#[napi(factory)]
pub async fn create(settings_input: Option<String>, log_level: Option<LogLevel>) -> Self {

Check warning on line 31 in crates/bitwarden-napi/src/client.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-napi/src/client.rs#L31

Added line #L31 was not covered by tests
// This will only fail if another logger was already initialized, so we can ignore the
// result
let _ = env_logger::Builder::from_default_env()
.filter_level(convert_level(log_level.unwrap_or(LogLevel::Info)))
.try_init();
Self(new(settings_input))
Self(bitwarden_json::client::Client::new(settings_input).await)

Check warning on line 37 in crates/bitwarden-napi/src/client.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-napi/src/client.rs#L37

Added line #L37 was not covered by tests
}

#[napi]
pub async fn run_command(&self, command_input: String) -> String {
self.0.run_command(&command_input).await
}
}

#[tokio::main]
async fn new(settings_string: Option<String>) -> JsonClient {
JsonClient::new(settings_string).await
}
2 changes: 1 addition & 1 deletion crates/bitwarden-uniffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub struct Client(bitwarden::Client);
impl Client {
/// Initialize a new instance of the SDK client
#[uniffi::constructor]
pub async fn factory(settings: Option<ClientSettings>) -> Arc<Self> {
pub async fn create(settings: Option<ClientSettings>) -> Arc<Self> {

Check warning on line 35 in crates/bitwarden-uniffi/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-uniffi/src/lib.rs#L35

Added line #L35 was not covered by tests
init_logger();

#[cfg(target_os = "android")]
Expand Down
2 changes: 1 addition & 1 deletion crates/bitwarden-wasm/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub struct BitwardenClient(Rc<JsonClient>);
#[wasm_bindgen]
impl BitwardenClient {
#[wasm_bindgen]
pub async fn factory(settings_input: Option<String>, log_level: Option<LogLevel>) -> Self {
pub async fn create(settings_input: Option<String>, log_level: Option<LogLevel>) -> Self {

Check warning on line 38 in crates/bitwarden-wasm/src/client.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm/src/client.rs#L38

Added line #L38 was not covered by tests
console_error_panic_hook::set_once();
if let Err(e) =
console_log::init_with_level(convert_level(log_level.unwrap_or(LogLevel::Info)))
Expand Down

0 comments on commit d9e470e

Please sign in to comment.