1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use anyhow::Result;
use clap::Parser;
use std::str::FromStr;

#[derive(Default, Debug, Parser)]
pub struct ConfigOverride {
    /// Wallet override.
    #[clap(global = true, long = "provider.wallet")]
    pub wallet: Option<WalletPath>,
}

// pub struct WithPath<T> {
//     inner: T,
//     path: PathBuf,
// }

// impl<T> WithPath<T> {
//     pub fn new(inner: T, path: PathBuf) -> Self {
//         Self { inner, path }
//     }

//     pub fn path(&self) -> &PathBuf {
//         &self.path
//     }

//     pub fn into_inner(self) -> T {
//         self.inner
//     }
// }

// impl<T> std::ops::Deref for WithPath<T> {
//     type Target = T;
//     fn deref(&self) -> &Self::Target {
//         &self.inner
//     }
// }

// impl<T> std::ops::DerefMut for WithPath<T> {
//     fn deref_mut(&mut self) -> &mut Self::Target {
//         &mut self.inner
//     }
// }

// impl<T> std::convert::AsRef<T> for WithPath<T> {
//     fn as_ref(&self) -> &T {
//         &self.inner
//     }
// }

#[derive(Debug, Default)]
pub struct Config {
    pub provider: ProviderConfig,
}

#[derive(Debug, Default)]
pub struct ProviderConfig {
    pub wallet: WalletPath,
}

impl Config {
    pub fn override_config(cfg_override: &ConfigOverride) -> Result<Config> {
        let mut cfg = Config::default();

        if let Some(wallet) = cfg_override.wallet.clone() {
            cfg.provider.wallet = wallet;
        }

        return Ok(cfg);
    }
}

pub fn with_config<R>(cfg_override: &ConfigOverride, f: impl FnOnce(&Config) -> R) -> R {
    let cfg = Config::override_config(cfg_override).expect("failed to override config");

    let r = f(&cfg);

    r
}

crate::home_path!(WalletPath, ".config/solana/id.json");