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