solana_cli_config/
config_input.rs

1use {
2    crate::Config, solana_clap_utils::input_validators::normalize_to_url_if_moniker,
3    solana_commitment_config::CommitmentConfig, std::str::FromStr,
4};
5
6pub enum SettingType {
7    Explicit,
8    Computed,
9    SystemDefault,
10}
11
12pub struct ConfigInput {
13    pub json_rpc_url: String,
14    pub websocket_url: String,
15    pub keypair_path: String,
16    pub commitment: CommitmentConfig,
17}
18
19impl ConfigInput {
20    fn default_keypair_path() -> String {
21        Config::default().keypair_path
22    }
23
24    fn default_json_rpc_url() -> String {
25        Config::default().json_rpc_url
26    }
27
28    fn default_websocket_url() -> String {
29        Config::default().websocket_url
30    }
31
32    fn default_commitment() -> CommitmentConfig {
33        CommitmentConfig::confirmed()
34    }
35
36    fn first_nonempty_setting(
37        settings: std::vec::Vec<(SettingType, String)>,
38    ) -> (SettingType, String) {
39        settings
40            .into_iter()
41            .find(|(_, value)| !value.is_empty())
42            .expect("no nonempty setting")
43    }
44
45    fn first_setting_is_some<T>(
46        settings: std::vec::Vec<(SettingType, Option<T>)>,
47    ) -> (SettingType, T) {
48        let (setting_type, setting_option) = settings
49            .into_iter()
50            .find(|(_, value)| value.is_some())
51            .expect("all settings none");
52        (setting_type, setting_option.unwrap())
53    }
54
55    pub fn compute_websocket_url_setting(
56        websocket_cmd_url: &str,
57        websocket_cfg_url: &str,
58        json_rpc_cmd_url: &str,
59        json_rpc_cfg_url: &str,
60    ) -> (SettingType, String) {
61        Self::first_nonempty_setting(vec![
62            (SettingType::Explicit, websocket_cmd_url.to_string()),
63            (SettingType::Explicit, websocket_cfg_url.to_string()),
64            (
65                SettingType::Computed,
66                Config::compute_websocket_url(&normalize_to_url_if_moniker(json_rpc_cmd_url)),
67            ),
68            (
69                SettingType::Computed,
70                Config::compute_websocket_url(&normalize_to_url_if_moniker(json_rpc_cfg_url)),
71            ),
72            (SettingType::SystemDefault, Self::default_websocket_url()),
73        ])
74    }
75
76    pub fn compute_json_rpc_url_setting(
77        json_rpc_cmd_url: &str,
78        json_rpc_cfg_url: &str,
79    ) -> (SettingType, String) {
80        let (setting_type, url_or_moniker) = Self::first_nonempty_setting(vec![
81            (SettingType::Explicit, json_rpc_cmd_url.to_string()),
82            (SettingType::Explicit, json_rpc_cfg_url.to_string()),
83            (SettingType::SystemDefault, Self::default_json_rpc_url()),
84        ]);
85        (setting_type, normalize_to_url_if_moniker(url_or_moniker))
86    }
87
88    pub fn compute_keypair_path_setting(
89        keypair_cmd_path: &str,
90        keypair_cfg_path: &str,
91    ) -> (SettingType, String) {
92        Self::first_nonempty_setting(vec![
93            (SettingType::Explicit, keypair_cmd_path.to_string()),
94            (SettingType::Explicit, keypair_cfg_path.to_string()),
95            (SettingType::SystemDefault, Self::default_keypair_path()),
96        ])
97    }
98
99    pub fn compute_commitment_config(
100        commitment_cmd: &str,
101        commitment_cfg: &str,
102    ) -> (SettingType, CommitmentConfig) {
103        Self::first_setting_is_some(vec![
104            (
105                SettingType::Explicit,
106                CommitmentConfig::from_str(commitment_cmd).ok(),
107            ),
108            (
109                SettingType::Explicit,
110                CommitmentConfig::from_str(commitment_cfg).ok(),
111            ),
112            (SettingType::SystemDefault, Some(Self::default_commitment())),
113        ])
114    }
115}
116
117impl Default for ConfigInput {
118    fn default() -> ConfigInput {
119        ConfigInput {
120            json_rpc_url: Self::default_json_rpc_url(),
121            websocket_url: Self::default_websocket_url(),
122            keypair_path: Self::default_keypair_path(),
123            commitment: CommitmentConfig::confirmed(),
124        }
125    }
126}