11//! Tools to manage and use Rust toolchains.
22
33use crate :: cmd:: { Binary , Command , Runnable } ;
4- use crate :: tools:: { RUSTUP , RUSTUP_TOOLCHAIN_INSTALL_MASTER } ;
4+ use crate :: tools:: RUSTUP ;
5+ #[ cfg( feature = "unstable-toolchain-ci" ) ]
6+ use crate :: tools:: RUSTUP_TOOLCHAIN_INSTALL_MASTER ;
57use crate :: Workspace ;
6- use failure:: { bail , Error , ResultExt } ;
8+ use failure:: { Error , ResultExt } ;
79use log:: info;
810use std:: borrow:: Cow ;
911use std:: path:: Path ;
@@ -95,6 +97,8 @@ impl std::fmt::Display for RustupThing {
9597
9698/// Metadata of a CI toolchain. See [`Toolchain`](struct.Toolchain.html) to create and get it.
9799#[ derive( serde:: Serialize , serde:: Deserialize , PartialEq , Eq , Hash , Debug , Clone ) ]
100+ #[ cfg( any( feature = "unstable-toolchain-ci" , doc) ) ]
101+ #[ cfg_attr( docs_rs, doc( cfg( feature = "unstable-toolchain-ci" ) ) ) ]
98102pub struct CiToolchain {
99103 /// Hash of the merge commit of the PR you want to download.
100104 sha : String ,
@@ -103,6 +107,7 @@ pub struct CiToolchain {
103107 alt : bool ,
104108}
105109
110+ #[ cfg( any( feature = "unstable-toolchain-ci" , doc) ) ]
106111impl CiToolchain {
107112 /// Get the SHA of the git commit that produced this toolchain.
108113 pub fn sha ( & self ) -> & str {
@@ -149,6 +154,7 @@ impl CiToolchain {
149154enum ToolchainInner {
150155 Dist ( DistToolchain ) ,
151156 #[ serde( rename = "ci" ) ]
157+ #[ cfg( feature = "unstable-toolchain-ci" ) ]
152158 CI ( CiToolchain ) ,
153159}
154160
@@ -197,6 +203,8 @@ impl Toolchain {
197203 /// **There is no availability or stability guarantee for these builds!**
198204 ///
199205 /// [repo]: https://github.com/rust-lang/rust
206+ #[ cfg( any( feature = "unstable-toolchain-ci" , doc) ) ]
207+ #[ cfg_attr( docs_rs, doc( cfg( feature = "unstable-toolchain-ci" ) ) ) ]
200208 pub fn ci ( sha : & str , alt : bool ) -> Self {
201209 Toolchain {
202210 inner : ToolchainInner :: CI ( CiToolchain {
@@ -207,6 +215,7 @@ impl Toolchain {
207215 }
208216
209217 /// If this toolchain is a dist toolchain, return its metadata.
218+ #[ allow( irrefutable_let_patterns) ]
210219 pub fn as_dist ( & self ) -> Option < & DistToolchain > {
211220 if let ToolchainInner :: Dist ( dist) = & self . inner {
212221 Some ( dist)
@@ -216,6 +225,8 @@ impl Toolchain {
216225 }
217226
218227 /// If this toolchain is a CI toolchain, return its metadata.
228+ #[ cfg( any( feature = "unstable-toolchain-ci" , doc) ) ]
229+ #[ cfg_attr( docs_rs, doc( cfg( feature = "unstable-toolchain-ci" ) ) ) ]
219230 pub fn as_ci ( & self ) -> Option < & CiToolchain > {
220231 if let ToolchainInner :: CI ( ci) = & self . inner {
221232 Some ( ci)
@@ -228,6 +239,7 @@ impl Toolchain {
228239 pub fn install ( & self , workspace : & Workspace ) -> Result < ( ) , Error > {
229240 match & self . inner {
230241 ToolchainInner :: Dist ( dist) => dist. init ( workspace) ?,
242+ #[ cfg( feature = "unstable-toolchain-ci" ) ]
231243 ToolchainInner :: CI ( ci) => ci. init ( workspace) ?,
232244 }
233245
@@ -287,13 +299,15 @@ impl Toolchain {
287299 let thing = thing. to_string ( ) ;
288300 let action = action. to_string ( ) ;
289301
302+ #[ cfg( feature = "unstable-toolchain-ci" ) ]
290303 if let ToolchainInner :: CI { .. } = self . inner {
291- bail ! (
304+ failure :: bail!(
292305 "{} {} on CI toolchains is not supported yet" ,
293306 log_action_ing,
294307 thing
295308 ) ;
296309 }
310+
297311 let toolchain_name = self . rustup_name ( ) ;
298312 info ! (
299313 "{} {} {} for toolchain {}" ,
@@ -419,7 +433,9 @@ impl Toolchain {
419433 fn rustup_name ( & self ) -> String {
420434 match & self . inner {
421435 ToolchainInner :: Dist ( dist) => dist. name . to_string ( ) ,
436+ #[ cfg( feature = "unstable-toolchain-ci" ) ]
422437 ToolchainInner :: CI ( ci) if ci. alt => format ! ( "{}-alt" , ci. sha) ,
438+ #[ cfg( feature = "unstable-toolchain-ci" ) ]
423439 ToolchainInner :: CI ( ci) => ci. sha . to_string ( ) ,
424440 }
425441 }
@@ -462,12 +478,15 @@ pub(crate) fn list_installed_toolchains(rustup_home: &Path) -> Result<Vec<Toolch
462478 if entry. file_type ( ) ?. is_symlink ( ) || update_hashes. join ( & name) . exists ( ) {
463479 result. push ( Toolchain :: dist ( & name) ) ;
464480 } else {
465- let ( sha, alt) = if name. ends_with ( "-alt" ) {
466- ( ( & name[ ..name. len ( ) - 4 ] ) . to_string ( ) , true )
467- } else {
468- ( name, false )
469- } ;
470- result. push ( Toolchain :: ci ( & sha, alt) ) ;
481+ #[ cfg( feature = "unstable-toolchain-ci" ) ]
482+ {
483+ let ( sha, alt) = if name. ends_with ( "-alt" ) {
484+ ( ( & name[ ..name. len ( ) - 4 ] ) . to_string ( ) , true )
485+ } else {
486+ ( name, false )
487+ } ;
488+ result. push ( Toolchain :: ci ( & sha, alt) ) ;
489+ }
471490 }
472491 }
473492 Ok ( result)
@@ -479,12 +498,20 @@ mod tests {
479498 use failure:: Error ;
480499
481500 #[ test]
482- fn test_serde_repr ( ) -> Result < ( ) , Error > {
501+ fn test_dist_serde_repr ( ) -> Result < ( ) , Error > {
483502 const DIST : & str = r#"{"type": "dist", "name": "stable"}"# ;
503+
504+ assert_eq ! ( Toolchain :: dist( "stable" ) , serde_json:: from_str( DIST ) ?) ;
505+
506+ Ok ( ( ) )
507+ }
508+
509+ #[ test]
510+ #[ cfg( feature = "unstable-toolchain-ci" ) ]
511+ fn test_ci_serde_repr ( ) -> Result < ( ) , Error > {
484512 const CI_NORMAL : & str = r#"{"type": "ci", "sha": "0000000", "alt": false}"# ;
485513 const CI_ALT : & str = r#"{"type": "ci", "sha": "0000000", "alt": true}"# ;
486514
487- assert_eq ! ( Toolchain :: dist( "stable" ) , serde_json:: from_str( DIST ) ?) ;
488515 assert_eq ! (
489516 Toolchain :: ci( "0000000" , false ) ,
490517 serde_json:: from_str( CI_NORMAL ) ?
@@ -537,11 +564,21 @@ mod tests {
537564 ) ?;
538565
539566 let res = super :: list_installed_toolchains ( rustup_home. path ( ) ) ?;
540- assert_eq ! ( 4 , res. len( ) ) ;
567+
568+ let mut expected_count = 0 ;
569+
541570 assert ! ( res. contains( & Toolchain :: dist( DIST_NAME ) ) ) ;
542571 assert ! ( res. contains( & Toolchain :: dist( LINK_NAME ) ) ) ;
543- assert ! ( res. contains( & Toolchain :: ci( CI_SHA , false ) ) ) ;
544- assert ! ( res. contains( & Toolchain :: ci( CI_SHA , true ) ) ) ;
572+ expected_count += 2 ;
573+
574+ #[ cfg( feature = "unstable-toolchain-ci" ) ]
575+ {
576+ assert ! ( res. contains( & Toolchain :: ci( CI_SHA , false ) ) ) ;
577+ assert ! ( res. contains( & Toolchain :: ci( CI_SHA , true ) ) ) ;
578+ expected_count += 2 ;
579+ }
580+
581+ assert_eq ! ( res. len( ) , expected_count) ;
545582
546583 Ok ( ( ) )
547584 }
0 commit comments