@@ -11,13 +11,14 @@ use std::{
1111 env, fmt,
1212} ;
1313use toml:: Value ;
14+ use url:: Url ;
1415
1516const GITHUB : & ' static str = "https://github.com" ;
1617const GITLAB : & ' static str = "https://gitlab.com" ;
1718
1819#[ derive( Debug , Clone , PartialEq ) ]
1920pub struct ToolSpec {
20- host : String ,
21+ host : Url ,
2122 path : String ,
2223 version : VersionReq ,
2324 protocol : Protocol ,
@@ -70,7 +71,7 @@ impl ToolSpec {
7071 } ) ;
7172 }
7273
73- let host = host_source. source . to_string ( ) ;
74+ let host = host_source. source . to_owned ( ) ;
7475 let path = path_val
7576 . as_str ( )
7677 . ok_or_else ( || ConfigFileParseError :: Tool {
@@ -126,6 +127,10 @@ impl ToolSpec {
126127 Protocol :: Artifactory => Provider :: Artifactory ,
127128 }
128129 }
130+
131+ pub fn host ( & self ) -> & Url {
132+ & self . host
133+ }
129134}
130135
131136impl fmt:: Display for ToolSpec {
@@ -142,21 +147,18 @@ pub struct ConfigFile {
142147
143148#[ derive( Debug , PartialEq ) ]
144149pub struct Host {
145- source : String ,
150+ source : Url ,
146151 protocol : Protocol ,
147152}
148153
149154impl Host {
150- pub fn new < S : Into < String > > ( source : S , protocol : Protocol ) -> Self {
151- Self {
152- source : source. into ( ) ,
153- protocol,
154- }
155+ pub fn new ( source : Url , protocol : Protocol ) -> Self {
156+ Self { source, protocol }
155157 }
156158
157159 pub fn from_value ( value : & Value ) -> ConfigFileParseResult < Self > {
158160 if let Value :: Table ( mut map) = value. clone ( ) {
159- let source = map
161+ let source_string = map
160162 . remove ( "source" )
161163 . ok_or_else ( || ConfigFileParseError :: Host {
162164 host : value. to_string ( ) ,
@@ -167,6 +169,9 @@ impl Host {
167169 } ) ?
168170 . to_string ( ) ;
169171
172+ let source = Url :: parse ( & source_string) . map_err ( |_| ConfigFileParseError :: Host {
173+ host : value. to_string ( ) ,
174+ } ) ?;
170175 let protocol_value =
171176 map. remove ( "protocol" )
172177 . ok_or_else ( || ConfigFileParseError :: Host {
@@ -211,9 +216,18 @@ impl ConfigFile {
211216 Self {
212217 tools : BTreeMap :: new ( ) ,
213218 hosts : HashMap :: from ( [
214- ( "source" . to_string ( ) , Host :: new ( GITHUB , Protocol :: Github ) ) ,
215- ( "github" . to_string ( ) , Host :: new ( GITHUB , Protocol :: Github ) ) ,
216- ( "gitlab" . to_string ( ) , Host :: new ( GITLAB , Protocol :: Gitlab ) ) ,
219+ (
220+ "source" . to_string ( ) ,
221+ Host :: new ( Url :: parse ( GITHUB ) . unwrap ( ) , Protocol :: Github ) ,
222+ ) ,
223+ (
224+ "github" . to_string ( ) ,
225+ Host :: new ( Url :: parse ( GITHUB ) . unwrap ( ) , Protocol :: Github ) ,
226+ ) ,
227+ (
228+ "gitlab" . to_string ( ) ,
229+ Host :: new ( Url :: parse ( GITLAB ) . unwrap ( ) , Protocol :: Gitlab ) ,
230+ ) ,
217231 ] ) ,
218232 }
219233 }
@@ -332,7 +346,7 @@ mod test {
332346
333347 fn new_github < S : Into < String > > ( github : S , version : VersionReq ) -> ToolSpec {
334348 ToolSpec {
335- host : GITHUB . to_string ( ) ,
349+ host : Url :: parse ( GITHUB ) . unwrap ( ) ,
336350 path : github. into ( ) ,
337351 version : version,
338352 protocol : Protocol :: Github ,
@@ -341,7 +355,7 @@ mod test {
341355
342356 fn new_gitlab < S : Into < String > > ( gitlab : S , version : VersionReq ) -> ToolSpec {
343357 ToolSpec {
344- host : GITLAB . to_string ( ) ,
358+ host : Url :: parse ( GITLAB ) . unwrap ( ) ,
345359 path : gitlab. into ( ) ,
346360 version : version,
347361 protocol : Protocol :: Gitlab ,
@@ -350,7 +364,7 @@ mod test {
350364
351365 fn new_artifactory < S : Into < String > > ( host : S , path : S , version : VersionReq ) -> ToolSpec {
352366 ToolSpec {
353- host : host. into ( ) ,
367+ host : Url :: parse ( host. into ( ) . as_str ( ) ) . unwrap ( ) ,
354368 path : path. into ( ) ,
355369 version : version,
356370 protocol : Protocol :: Artifactory ,
@@ -367,26 +381,23 @@ mod test {
367381 VersionReq :: parse ( string) . unwrap ( )
368382 }
369383
370- fn new_host < S : Into < String > > ( source : S , protocol : Protocol ) -> Host {
371- Host {
372- source : source. into ( ) ,
373- protocol,
374- }
384+ fn new_host ( source : Url , protocol : Protocol ) -> Host {
385+ Host { source, protocol }
375386 }
376387
377388 fn default_hosts ( ) -> HashMap < String , Host > {
378389 HashMap :: from ( [
379390 (
380391 "source" . to_string ( ) ,
381- Host :: new ( GITHUB . to_string ( ) , Protocol :: Github ) ,
392+ Host :: new ( Url :: parse ( GITHUB ) . unwrap ( ) , Protocol :: Github ) ,
382393 ) ,
383394 (
384395 "github" . to_string ( ) ,
385- Host :: new ( GITHUB . to_string ( ) , Protocol :: Github ) ,
396+ Host :: new ( Url :: parse ( GITHUB ) . unwrap ( ) , Protocol :: Github ) ,
386397 ) ,
387398 (
388399 "gitlab" . to_string ( ) ,
389- Host :: new ( GITLAB . to_string ( ) , Protocol :: Gitlab ) ,
400+ Host :: new ( Url :: parse ( GITLAB ) . unwrap ( ) , Protocol :: Gitlab ) ,
390401 ) ,
391402 ] )
392403 }
@@ -395,7 +406,7 @@ mod test {
395406 let mut hosts = default_hosts ( ) ;
396407 hosts. insert (
397408 "artifactory" . to_string ( ) ,
398- Host :: new ( ARTIFACTORY . to_string ( ) , Protocol :: Artifactory ) ,
409+ Host :: new ( Url :: parse ( ARTIFACTORY ) . unwrap ( ) , Protocol :: Artifactory ) ,
399410 ) ;
400411 hosts
401412 }
@@ -469,7 +480,10 @@ mod test {
469480 let host = Host :: from_value ( & value) . unwrap ( ) ;
470481 assert_eq ! (
471482 host,
472- new_host( "https://artifactory.com" , Protocol :: Artifactory )
483+ new_host(
484+ Url :: parse( "https://artifactory.com" ) . unwrap( ) ,
485+ Protocol :: Artifactory
486+ )
473487 )
474488 }
475489
@@ -546,7 +560,7 @@ mod test {
546560 BTreeMap :: from( [ (
547561 "tool" . to_string( ) ,
548562 ToolSpec {
549- host: "https://artifactory.com" . to_string ( ) ,
563+ host: Url :: parse ( "https://artifactory.com" ) . unwrap ( ) ,
550564 path: "path/to/tool" . to_string( ) ,
551565 version: VersionReq :: parse( "1.0.0" ) . unwrap( ) ,
552566 protocol: Protocol :: Artifactory
@@ -555,7 +569,7 @@ mod test {
555569 HashMap :: from( [ (
556570 "artifactory" . to_string( ) ,
557571 Host {
558- source: "https://artifactory.com" . to_string ( ) ,
572+ source: Url :: parse ( "https://artifactory.com" ) . unwrap ( ) ,
559573 protocol: Protocol :: Artifactory
560574 }
561575 ) ] )
0 commit comments