@@ -4,6 +4,7 @@ use anyhow::Result;
44use camino:: Utf8PathBuf ;
55use configuration:: Config ;
66use serde:: { Deserialize , Serialize } ;
7+ use url:: Url ;
78
89#[ must_use]
910pub const fn show_explorer_links_default ( ) -> bool {
@@ -13,14 +14,14 @@ pub const fn show_explorer_links_default() -> bool {
1314#[ derive( Deserialize , Serialize , Clone , Debug , PartialEq , Default ) ]
1415#[ serde( deny_unknown_fields) ]
1516pub struct NetworksConfig {
16- pub mainnet : Option < String > ,
17- pub sepolia : Option < String > ,
18- pub devnet : Option < String > ,
17+ pub mainnet : Option < Url > ,
18+ pub sepolia : Option < Url > ,
19+ pub devnet : Option < Url > ,
1920}
2021
2122impl NetworksConfig {
2223 #[ must_use]
23- pub fn get_url ( & self , network : Network ) -> Option < & String > {
24+ pub fn get_url ( & self , network : Network ) -> Option < & Url > {
2425 match network {
2526 Network :: Mainnet => self . mainnet . as_ref ( ) ,
2627 Network :: Sepolia => self . sepolia . as_ref ( ) ,
@@ -45,7 +46,7 @@ impl NetworksConfig {
4546pub struct CastConfig {
4647 #[ serde( default ) ]
4748 /// RPC url
48- pub url : String ,
49+ pub url : Option < Url > ,
4950
5051 #[ serde( default ) ]
5152 pub account : String ,
@@ -86,7 +87,7 @@ pub struct CastConfig {
8687impl Default for CastConfig {
8788 fn default ( ) -> Self {
8889 Self {
89- url : String :: default ( ) ,
90+ url : None ,
9091 account : String :: default ( ) ,
9192 accounts_file : Utf8PathBuf :: default ( ) ,
9293 keystore : None ,
@@ -104,7 +105,7 @@ impl Config for CastConfig {
104105 }
105106
106107 fn from_raw ( config : serde_json:: Value ) -> Result < Self > {
107- Ok ( serde_json:: from_value :: < CastConfig > ( config) ?)
108+ Ok ( serde_json:: from_value :: < Self > ( config) ?)
108109 }
109110}
110111
@@ -115,34 +116,34 @@ mod tests {
115116 #[ test]
116117 fn test_networks_config_get ( ) {
117118 let networks = NetworksConfig {
118- mainnet : Some ( "https://mainnet.example.com" . to_string ( ) ) ,
119- sepolia : Some ( "https://sepolia.example.com" . to_string ( ) ) ,
120- devnet : Some ( "https://devnet.example.com" . to_string ( ) ) ,
119+ mainnet : Some ( Url :: parse ( "https://mainnet.example.com" ) . unwrap ( ) ) ,
120+ sepolia : Some ( Url :: parse ( "https://sepolia.example.com" ) . unwrap ( ) ) ,
121+ devnet : Some ( Url :: parse ( "https://devnet.example.com" ) . unwrap ( ) ) ,
121122 } ;
122123
123124 assert_eq ! (
124125 networks. get_url( Network :: Mainnet ) ,
125- Some ( & "https://mainnet.example.com" . to_string ( ) )
126+ Some ( & Url :: parse ( "https://mainnet.example.com" ) . unwrap ( ) )
126127 ) ;
127128 assert_eq ! (
128129 networks. get_url( Network :: Sepolia ) ,
129- Some ( & "https://sepolia.example.com" . to_string ( ) )
130+ Some ( & Url :: parse ( "https://sepolia.example.com" ) . unwrap ( ) )
130131 ) ;
131132 assert_eq ! (
132133 networks. get_url( Network :: Devnet ) ,
133- Some ( & "https://devnet.example.com" . to_string ( ) )
134+ Some ( & Url :: parse ( "https://devnet.example.com" ) . unwrap ( ) )
134135 ) ;
135136 }
136137
137138 #[ test]
138139 fn test_networks_config_override ( ) {
139140 let mut global = NetworksConfig {
140- mainnet : Some ( "https://global-mainnet.example.com" . to_string ( ) ) ,
141- sepolia : Some ( "https://global-sepolia.example.com" . to_string ( ) ) ,
141+ mainnet : Some ( Url :: parse ( "https://global-mainnet.example.com" ) . unwrap ( ) ) ,
142+ sepolia : Some ( Url :: parse ( "https://global-sepolia.example.com" ) . unwrap ( ) ) ,
142143 devnet : None ,
143144 } ;
144145 let local = NetworksConfig {
145- mainnet : Some ( "https://local-mainnet.example.com" . to_string ( ) ) ,
146+ mainnet : Some ( Url :: parse ( "https://local-mainnet.example.com" ) . unwrap ( ) ) ,
146147 sepolia : None ,
147148 devnet : None ,
148149 } ;
@@ -152,12 +153,12 @@ mod tests {
152153 // Local mainnet should override global
153154 assert_eq ! (
154155 global. mainnet,
155- Some ( "https://local-mainnet.example.com" . to_string ( ) )
156+ Some ( Url :: parse ( "https://local-mainnet.example.com" ) . unwrap ( ) )
156157 ) ;
157158 // Global sepolia should remain
158159 assert_eq ! (
159160 global. sepolia,
160- Some ( "https://global-sepolia.example.com" . to_string ( ) )
161+ Some ( Url :: parse ( "https://global-sepolia.example.com" ) . unwrap ( ) )
161162 ) ;
162163 }
163164
0 commit comments