1pub mod cli;
5pub mod logger;
6
7use crate::cli_shared::cli::{Config, ConfigPath, find_config_path};
8use crate::networks::NetworkChain;
9use crate::utils::io::read_toml;
10use std::path::PathBuf;
11
12cfg_if::cfg_if! {
13 if #[cfg(feature = "rustalloc")] {
14 } else if #[cfg(feature = "jemalloc")] {
15 pub use tikv_jemallocator;
16 }
17}
18
19pub fn chain_path(config: &Config) -> PathBuf {
21 PathBuf::from(&config.client.data_dir).join(config.chain().to_string())
22}
23
24pub fn read_config(
25 config_path_opt: Option<&PathBuf>,
26 chain_opt: Option<NetworkChain>,
27) -> anyhow::Result<(Option<ConfigPath>, Config)> {
28 let (path, mut config) = match find_config_path(config_path_opt) {
29 Some(path) => {
30 let toml = std::fs::read_to_string(path.to_path_buf())?;
32 (Some(path), read_toml(&toml)?)
34 }
35 None => (None, Config::default()),
36 };
37 if let Some(chain) = chain_opt {
38 config.chain = chain;
39 }
40 Ok((path, config))
41}
42
43#[cfg(test)]
44mod tests {
45 use super::*;
46
47 #[test]
48 fn read_config_default() {
49 let (config_path, config) = read_config(None, None).unwrap();
50
51 assert!(config_path.is_none());
52 assert_eq!(config.chain(), &NetworkChain::Mainnet);
53 }
54
55 #[test]
56 fn read_config_calibnet_override() {
57 let (config_path, config) = read_config(None, Some(NetworkChain::Calibnet)).unwrap();
58
59 assert!(config_path.is_none());
60 assert_eq!(config.chain(), &NetworkChain::Calibnet);
61 }
62
63 #[test]
64 fn read_config_butterflynet_override() {
65 let (config_path, config) = read_config(None, Some(NetworkChain::Butterflynet)).unwrap();
66
67 assert!(config_path.is_none());
68 assert_eq!(config.chain(), &NetworkChain::Butterflynet);
69 }
70
71 #[test]
72 fn read_config_with_path() {
73 let default_config = Config::default();
74 let path: PathBuf = "config.toml".into();
75 let serialized_config = toml::to_string(&default_config).unwrap();
76 std::fs::write(path.clone(), serialized_config).unwrap();
77
78 let (config_path, config) = read_config(Some(&path), None).unwrap();
79
80 assert_eq!(config_path.unwrap(), ConfigPath::Cli(path));
81 assert_eq!(config.chain(), &NetworkChain::Mainnet);
82 assert_eq!(config, default_config);
83 }
84}
85
86pub mod snapshot;