utility_cli_rs/network/
mod.rs1#[derive(Debug, Clone, interactive_clap::InteractiveClap)]
2#[interactive_clap(input_context = NetworkContext)]
3#[interactive_clap(output_context = NetworkOutputContext)]
4pub struct Network {
5 #[interactive_clap(long)]
6 #[interactive_clap(skip_interactive_input)]
7 wallet_url: Option<crate::types::url::Url>,
8 #[interactive_clap(skip_default_input_arg)]
10 network_name: String,
11}
12
13pub type OnAfterGettingNetworkCallback =
14 std::sync::Arc<dyn Fn(&crate::config::NetworkConfig) -> crate::CliResult>;
15
16#[derive(Clone)]
17pub struct NetworkContext {
18 pub config: crate::config::Config,
19 pub interacting_with_account_ids: Vec<unc_primitives::types::AccountId>,
20 pub on_after_getting_network_callback: OnAfterGettingNetworkCallback,
21}
22
23#[derive(Clone)]
24pub struct NetworkOutputContext;
25
26impl NetworkOutputContext {
27 pub fn from_previous_context(
28 previous_context: NetworkContext,
29 scope: &<Network as interactive_clap::ToInteractiveClapContextScope>::InteractiveClapContextScope,
30 ) -> color_eyre::eyre::Result<Self> {
31 let network_connection = previous_context.config.network_connection;
32 let mut network_config = network_connection
33 .get(&scope.network_name)
34 .expect("Failed to get network config!")
35 .clone();
36 if let Some(url) = scope.wallet_url.clone() {
37 network_config.wallet_url = url.into();
38 }
39
40 (previous_context.on_after_getting_network_callback)(&network_config)?;
41 Ok(Self)
42 }
43}
44
45impl Network {
46 fn input_network_name(context: &NetworkContext) -> color_eyre::eyre::Result<Option<String>> {
47 crate::common::input_network_name(&context.config, &context.interacting_with_account_ids)
48 }
49}