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 let specified_path = env::var("LOCKBOOK_PATH");
72
73 let default_path =
74 env::var("HOME") // unix
75 .or(env::var("HOMEPATH")) // windows
76 .map(|home| format!("{home}/.lockbook/{writeable_path_subfolder}"));
77
78 let Ok(writeable_path) = specified_path.or(default_path) else {
79 panic!("no location for lockbook to initialize");
80 };
81
82 writeable_path
83 }
84}
85
86// todo: we added background work as a flag to speed up test execution in debug mode
87// turn background work back to true in test_utils to see the slow test
88// the slow test primarily does a large amount of allocations due to ownership model
89// of treelike. In a universe where these operations could be expressed as iterators
90// we would be able to vastly cut down on allocations and eliminate this complexity
91//
92// another nice aspect of background work is that it is a workaround for CLI's lack
93// of graceful shutdown. Ideally, both of these situations will be handled differently.