Skip to main content

lb_rs/model/
core_config.rs

1use std::env;
2
3use serde::Deserialize;
4
5#[derive(Debug, Deserialize, Clone, Copy, PartialEq, Eq, Default)]
6pub enum ClientType {
7    Cli,
8    Ui,
9    #[default]
10    Unknown,
11}
12
13impl ClientType {
14    pub fn as_str(self) -> &'static str {
15        match self {
16            ClientType::Cli => "cli",
17            ClientType::Ui => "ui",
18            ClientType::Unknown => "unknown",
19        }
20    }
21}
22
23#[derive(Debug, Deserialize, Clone)]
24pub struct Config {
25    /// Where should lockbook store data, including logs?
26    pub writeable_path: String,
27    /// Should lb do background work like keep search indexes up to date?
28    pub background_work: bool,
29
30    /// Should we log at all?
31    pub logs: bool,
32    /// Should logs be printed to stdout?
33    pub stdout_logs: bool,
34    /// Should logs be colored?
35    pub colored_logs: bool,
36
37    #[serde(default)]
38    pub client_type: ClientType,
39}
40
41impl Config {
42    /// Configures lockbook for CLI use with no stdout logs or background work. `writeable_path_subfolder` is generally
43    /// a hardcoded client name like `"cli"`.
44    pub fn cli_config(writeable_path_subfolder: &str) -> Config {
45        Config {
46            writeable_path: Self::writeable_path(writeable_path_subfolder),
47            background_work: false,
48            logs: true,
49            stdout_logs: false,
50            colored_logs: true,
51            client_type: ClientType::Cli,
52        }
53    }
54
55    /// Configures lockbook for UI use with stdout logs and background work. `writeable_path_subfolder` is generally
56    /// a hardcoded client name like `"macos"`.
57    pub fn ui_config(writeable_path_subfolder: &str) -> Config {
58        Config {
59            writeable_path: Self::writeable_path(writeable_path_subfolder),
60            background_work: true,
61            logs: true,
62            stdout_logs: true,
63            colored_logs: true,
64            client_type: ClientType::Ui,
65        }
66    }
67
68    /// Produces a full writable path for lockbook to use based on environment variables and platform. Useful for
69    /// initializing the Config struct.
70    pub fn writeable_path(writeable_path_subfolder: &str) -> String {
71        if let Ok(specified_path) = env::var("LOCKBOOK_PATH") {
72            return specified_path;
73        }
74
75        // `env::home_dir` is `HOME` then `getpwuid_r` on unix, and `USERPROFILE` then `GetUserProfileDirectory` on
76        // windows. It notably never consults `HOME` on windows: we used to fall back to `HOMEPATH`, which omits the
77        // drive letter (`\Users\parth`), so the data directory silently followed whichever drive the process happened
78        // to be running from.
79        let Some(home) = env::home_dir() else {
80            panic!("no location for lockbook to initialize");
81        };
82
83        home.join(".lockbook")
84            .join(writeable_path_subfolder)
85            .to_string_lossy()
86            .into_owned()
87    }
88}
89
90// todo: we added background work as a flag to speed up test execution in debug mode
91// turn background work back to true in test_utils to see the slow test
92// the slow test primarily does a large amount of allocations due to ownership model
93// of treelike. In a universe where these operations could be expressed as iterators
94// we would be able to vastly cut down on allocations and eliminate this complexity
95//
96// another nice aspect of background work is that it is a workaround for CLI's lack
97// of graceful shutdown. Ideally, both of these situations will be handled differently.