serum_dev_tools/
config.rs

1use anyhow::Result;
2use clap::Parser;
3use std::str::FromStr;
4
5#[derive(Default, Debug, Parser)]
6pub struct ConfigOverride {
7    /// Wallet override.
8    #[clap(global = true, long = "provider.wallet")]
9    pub wallet: Option<WalletPath>,
10}
11
12// pub struct WithPath<T> {
13//     inner: T,
14//     path: PathBuf,
15// }
16
17// impl<T> WithPath<T> {
18//     pub fn new(inner: T, path: PathBuf) -> Self {
19//         Self { inner, path }
20//     }
21
22//     pub fn path(&self) -> &PathBuf {
23//         &self.path
24//     }
25
26//     pub fn into_inner(self) -> T {
27//         self.inner
28//     }
29// }
30
31// impl<T> std::ops::Deref for WithPath<T> {
32//     type Target = T;
33//     fn deref(&self) -> &Self::Target {
34//         &self.inner
35//     }
36// }
37
38// impl<T> std::ops::DerefMut for WithPath<T> {
39//     fn deref_mut(&mut self) -> &mut Self::Target {
40//         &mut self.inner
41//     }
42// }
43
44// impl<T> std::convert::AsRef<T> for WithPath<T> {
45//     fn as_ref(&self) -> &T {
46//         &self.inner
47//     }
48// }
49
50#[derive(Debug, Default)]
51pub struct Config {
52    pub provider: ProviderConfig,
53}
54
55#[derive(Debug, Default)]
56pub struct ProviderConfig {
57    pub wallet: WalletPath,
58}
59
60impl Config {
61    pub fn override_config(cfg_override: &ConfigOverride) -> Result<Config> {
62        let mut cfg = Config::default();
63
64        if let Some(wallet) = cfg_override.wallet.clone() {
65            cfg.provider.wallet = wallet;
66        }
67
68        return Ok(cfg);
69    }
70}
71
72pub fn with_config<R>(cfg_override: &ConfigOverride, f: impl FnOnce(&Config) -> R) -> R {
73    let cfg = Config::override_config(cfg_override).expect("failed to override config");
74
75    let r = f(&cfg);
76
77    r
78}
79
80crate::home_path!(WalletPath, ".config/solana/id.json");