bitwarden_napi/
client.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
extern crate log;

use bitwarden_json::client::Client as JsonClient;
use napi_derive::napi;

#[napi]
pub enum LogLevel {
    Trace,
    Debug,
    Info,
    Warn,
    Error,
}

fn convert_level(level: LogLevel) -> log::LevelFilter {
    match level {
        LogLevel::Trace => log::LevelFilter::Trace,
        LogLevel::Debug => log::LevelFilter::Debug,
        LogLevel::Info => log::LevelFilter::Info,
        LogLevel::Warn => log::LevelFilter::Warn,
        LogLevel::Error => log::LevelFilter::Error,
    }
}

#[napi]
pub struct BitwardenClient(JsonClient);

#[napi]
impl BitwardenClient {
    #[napi(constructor)]
    pub fn new(settings_input: Option<String>, log_level: Option<LogLevel>) -> Self {
        // 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(bitwarden_json::client::Client::new(settings_input))
    }

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