lb_rs/model/
core_config.rs

1use std::env;
2
3use serde::Deserialize;
4
5#[derive(Debug, Deserialize, Clone)]
6pub struct Config {
7    /// Where should lockbook store data, including logs?
8    pub writeable_path: String,
9    /// Should lb do background work like keep search indexes up to date?
10    pub background_work: bool,
11
12    /// Should we log at all?
13    pub logs: bool,
14    /// Should logs be printed to stdout?
15    pub stdout_logs: bool,
16    /// Should logs be colored?
17    pub colored_logs: bool,
18}
19
20impl Config {
21    /// Configures lockbook for CLI use with no stdout logs or background work. `writeable_path_subfolder` is generally
22    /// a hardcoded client name like `"cli"`.
23    pub fn cli_config(writeable_path_subfolder: &str) -> Config {
24        Config {
25            writeable_path: Self::writeable_path(writeable_path_subfolder),
26            background_work: false,
27            logs: true,
28            stdout_logs: false,
29            colored_logs: true,
30        }
31    }
32
33    /// Configures lockbook for UI use with stdout logs and background work. `writeable_path_subfolder` is generally
34    /// a hardcoded client name like `"macos"`.
35    pub fn ui_config(writeable_path_subfolder: &str) -> Config {
36        Config {
37            writeable_path: Self::writeable_path(writeable_path_subfolder),
38            background_work: true,
39            logs: true,
40            stdout_logs: true,
41            colored_logs: true,
42        }
43    }
44
45    /// Produces a full writable path for lockbook to use based on environment variables and platform. Useful for
46    /// initializing the Config struct.
47    pub fn writeable_path(writeable_path_subfolder: &str) -> String {
48        let specified_path = env::var("LOCKBOOK_PATH");
49
50        let default_path =
51            env::var("HOME") // unix
52                .or(env::var("HOMEPATH")) // windows
53                .map(|home| format!("{home}/.lockbook/{writeable_path_subfolder}"));
54
55        let Ok(writeable_path) = specified_path.or(default_path) else {
56            panic!("no location for lockbook to initialize");
57        };
58
59        writeable_path
60    }
61}
62
63// todo: we added background work as a flag to speed up test execution in debug mode
64// turn background work back to true in test_utils to see the slow test
65// the slow test primarily does a large amount of allocations due to ownership model
66// of treelike. In a universe where these operations could be expressed as iterators
67// we would be able to vastly cut down on allocations and eliminate this complexity
68//
69// another nice aspect of background work is that it is a workaround for CLI's lack
70// of graceful shutdown. Ideally, both of these situations will be handled differently.