1
1
use crate :: CmdRun ;
2
2
use crate :: cmd_traits:: { GetScriptsData , InitGovernance } ;
3
+ use crate :: config:: SelectOptions ;
3
4
use crate :: config:: config_fields:: { BOOTNODES , SUBSTRATE_NODE_DATA_BASE_PATH } ;
4
5
use crate :: generate_keys:: network_key_path;
5
6
use crate :: io:: IOContext ;
@@ -16,6 +17,7 @@ use partner_chains_cardano_offchain::csl::NetworkTypeExt;
16
17
use partner_chains_cardano_offchain:: governance:: MultiSigParameters ;
17
18
use partner_chains_cardano_offchain:: scripts_data:: { ScriptsData , get_scripts_data} ;
18
19
use sidechain_domain:: { McTxHash , UtxoId } ;
20
+ use std:: fmt:: Display ;
19
21
use std:: net:: Ipv4Addr ;
20
22
use std:: str:: FromStr ;
21
23
use std:: vec;
@@ -44,7 +46,7 @@ impl CmdRun for PrepareConfigurationCmd {
44
46
context,
45
47
) ? {
46
48
prepare_main_chain_config ( context, & ogmios_config, genesis_utxo) ?;
47
- context. eprint ( "🚀 Chain configuration wizards completed successufully !" ) ;
49
+ context. eprint ( "🚀 Chain configuration wizards completed successfully !" ) ;
48
50
Ok ( ( ) )
49
51
} else {
50
52
context
@@ -126,13 +128,11 @@ fn read_bootnode_defaults(context: &impl IOContext) -> (Protocol, String, u16) {
126
128
}
127
129
128
130
fn choose_protocol ( context : & impl IOContext , default_protocol : Protocol ) -> Protocol {
129
- let mut protocols: Vec < String > = vec ! [ Protocol :: Dns . into( ) , Protocol :: Ipv4 . into( ) ] ;
130
- // default protocol should be the first in the list
131
- protocols
132
- . sort_by_key ( |protocol| if * protocol != String :: from ( default_protocol) { 1 } else { 0 } ) ;
131
+ let protocols = Protocol :: select_options_with_default ( & default_protocol) ;
133
132
134
- Protocol :: from_str ( & context. prompt_multi_option ( CHOOSE_PROTOCOL_PROMPT , protocols) )
135
- . expect ( "Invalid protocol cannot be chosen from valid options only" )
133
+ let protocol_selection_ix =
134
+ context. prompt_multi_option ( CHOOSE_PROTOCOL_PROMPT , protocols. clone ( ) ) ;
135
+ protocols[ protocol_selection_ix]
136
136
}
137
137
138
138
fn deconstruct_bootnode ( bootnode_opt : Option < String > ) -> Option < ( Protocol , String , u16 ) > {
@@ -160,7 +160,7 @@ fn peer_id_from_config(context: &impl IOContext) -> anyhow::Result<Option<String
160
160
}
161
161
162
162
#[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash ) ]
163
- enum Protocol {
163
+ pub ( crate ) enum Protocol {
164
164
Dns ,
165
165
Ipv4 ,
166
166
}
@@ -174,11 +174,11 @@ impl Protocol {
174
174
}
175
175
}
176
176
177
- impl From < Protocol > for String {
178
- fn from ( value : Protocol ) -> Self {
179
- match value {
180
- Protocol :: Dns => "hostname" . to_string ( ) ,
181
- Protocol :: Ipv4 => "IP address" . to_string ( ) ,
177
+ impl Display for Protocol {
178
+ fn fmt ( & self , f : & mut std :: fmt :: Formatter < ' _ > ) -> std :: fmt :: Result {
179
+ match self {
180
+ Protocol :: Dns => write ! ( f , "hostname" ) ,
181
+ Protocol :: Ipv4 => write ! ( f , "IP address" ) ,
182
182
}
183
183
}
184
184
}
@@ -195,6 +195,14 @@ impl FromStr for Protocol {
195
195
}
196
196
}
197
197
198
+ impl SelectOptions for Protocol {
199
+ type Opt = Protocol ;
200
+
201
+ fn select_options ( ) -> Vec < Self :: Opt > {
202
+ vec ! [ Protocol :: Dns , Protocol :: Ipv4 ]
203
+ }
204
+ }
205
+
198
206
const INTRO : & str =
199
207
"This 🧙 wizard will:
200
208
* establish single bootnode configuration (to be later included in chain-spec file)
@@ -315,29 +323,25 @@ pub mod tests {
315
323
}
316
324
317
325
pub fn pick_ip_protocol_with_defaults ( ) -> MockIO {
318
- pick_ip_protocol (
319
- vec ! [ Dns . into( ) , Ipv4 . into( ) ] ,
320
- DEFAULT_PORT ,
321
- Ipv4 . default_address ( ) . to_string ( ) ,
322
- )
326
+ pick_ip_protocol ( vec ! [ Dns , Ipv4 ] , DEFAULT_PORT , Ipv4 . default_address ( ) . to_string ( ) )
323
327
}
324
328
325
329
pub fn pick_ip_protocol (
326
- options : Vec < String > ,
330
+ options : Vec < Protocol > ,
327
331
default_port : u16 ,
328
332
default_ip_address : String ,
329
333
) -> MockIO {
330
334
pick_chosen_ip_protocol ( options, default_port, & default_ip_address, "10.2.2.4" )
331
335
}
332
336
333
337
pub fn pick_chosen_ip_protocol (
334
- options : Vec < String > ,
338
+ options : Vec < Protocol > ,
335
339
default_port : u16 ,
336
340
default_ip_address : & str ,
337
341
input : & str ,
338
342
) -> MockIO {
339
343
MockIO :: Group ( vec ! [
340
- MockIO :: prompt_multi_option( CHOOSE_PROTOCOL_PROMPT , options, & String :: from ( Ipv4 ) ) ,
344
+ MockIO :: prompt_multi_option( CHOOSE_PROTOCOL_PROMPT , options, & Ipv4 ) ,
341
345
MockIO :: prompt(
342
346
CHOOSE_PORT_PROMPT ,
343
347
Some ( & default_port. to_string( ) ) ,
@@ -355,20 +359,16 @@ pub mod tests {
355
359
}
356
360
357
361
pub fn pick_dns_protocol_with_defaults ( ) -> MockIO {
358
- pick_dns_protocol (
359
- vec ! [ Dns . into( ) , Ipv4 . into( ) ] ,
360
- DEFAULT_PORT ,
361
- Dns . default_address ( ) . to_string ( ) ,
362
- )
362
+ pick_dns_protocol ( vec ! [ Dns , Ipv4 ] , DEFAULT_PORT , Dns . default_address ( ) . to_string ( ) )
363
363
}
364
364
365
365
pub fn pick_dns_protocol (
366
- options : Vec < String > ,
366
+ options : Vec < Protocol > ,
367
367
default_port : u16 ,
368
368
default_hostname : String ,
369
369
) -> MockIO {
370
370
MockIO :: Group ( vec ! [
371
- MockIO :: prompt_multi_option( CHOOSE_PROTOCOL_PROMPT , options, & String :: from ( Dns ) ) ,
371
+ MockIO :: prompt_multi_option( CHOOSE_PROTOCOL_PROMPT , options, & Dns ) ,
372
372
MockIO :: prompt(
373
373
CHOOSE_PORT_PROMPT ,
374
374
Some ( & default_port. to_string( ) ) ,
@@ -460,7 +460,7 @@ pub mod tests {
460
460
scenarios:: read_config( ) ,
461
461
scenarios:: choose_to_configure_bootnode( true ) ,
462
462
scenarios:: pick_dns_protocol(
463
- vec![ Ipv4 . into ( ) , Dns . into ( ) ] ,
463
+ vec![ Ipv4 , Dns ] ,
464
464
3034 ,
465
465
Dns . default_address( ) . to_string( ) ,
466
466
) ,
@@ -486,7 +486,7 @@ pub mod tests {
486
486
scenarios:: read_config( ) ,
487
487
scenarios:: choose_to_configure_bootnode( true ) ,
488
488
scenarios:: pick_ip_protocol(
489
- vec![ Ipv4 . into ( ) , Dns . into ( ) ] ,
489
+ vec![ Ipv4 , Dns ] ,
490
490
3034 ,
491
491
"ip_address" . to_string( ) ,
492
492
) ,
@@ -550,7 +550,7 @@ pub mod tests {
550
550
scenarios:: read_config( ) ,
551
551
scenarios:: choose_to_configure_bootnode( true ) ,
552
552
scenarios:: pick_chosen_ip_protocol(
553
- vec![ Dns . into ( ) , Ipv4 . into ( ) ] ,
553
+ vec![ Dns , Ipv4 ] ,
554
554
DEFAULT_PORT ,
555
555
Ipv4 . default_address( ) ,
556
556
"100" ,
@@ -581,14 +581,16 @@ pub mod tests {
581
581
MockIO :: prompt ( & format ! ( "Enter the {}" , field_definition. name) , default, value)
582
582
}
583
583
584
- pub fn prompt_multi_option_with_default < T : SelectOptions > (
584
+ pub fn prompt_multi_option_with_default <
585
+ T : Clone + PartialEq + ToString + SelectOptions < Opt = T > ,
586
+ > (
585
587
field_definition : ConfigFieldDefinition < ' _ , T > ,
586
- default : Option < & str > ,
587
- value : & str ,
588
+ default : & T ,
589
+ value : & T ,
588
590
) -> MockIO {
589
591
MockIO :: prompt_multi_option (
590
592
& format ! ( "Select {}" , field_definition. name) ,
591
- T :: select_options_with_default ( default) ,
593
+ T :: select_options_with_default ( & default) ,
592
594
value,
593
595
)
594
596
}
0 commit comments