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