@@ -56,6 +56,7 @@ use std::{env, fmt};
5656
5757use anyhow:: { anyhow, Context , Result } ;
5858use cfg_if:: cfg_if;
59+ use clap:: builder:: PossibleValue ;
5960use clap:: ValueEnum ;
6061use itertools:: Itertools ;
6162use same_file:: Handle ;
@@ -243,21 +244,36 @@ pub(crate) const NEVER_SELF_UPDATE: bool = true;
243244#[ cfg( not( feature = "no-self-update" ) ) ]
244245pub ( crate ) const NEVER_SELF_UPDATE : bool = false ;
245246
246- #[ derive( Clone , Debug , Deserialize , Eq , PartialEq , Serialize ) ]
247+ #[ derive( Clone , Copy , Debug , Default , Deserialize , Eq , PartialEq , Serialize ) ]
247248#[ serde( rename_all = "kebab-case" ) ]
248249pub enum SelfUpdateMode {
250+ #[ default]
249251 Enable ,
250252 Disable ,
251253 CheckOnly ,
252254}
253255
254256impl SelfUpdateMode {
255- pub ( crate ) fn modes ( ) -> & ' static [ & ' static str ] {
256- & [ "enable" , "disable" , "check-only" ]
257+ pub ( crate ) fn as_str ( & self ) -> & ' static str {
258+ match self {
259+ Self :: Enable => "enable" ,
260+ Self :: Disable => "disable" ,
261+ Self :: CheckOnly => "check-only" ,
262+ }
263+ }
264+ }
265+
266+ impl ValueEnum for SelfUpdateMode {
267+ fn value_variants < ' a > ( ) -> & ' a [ Self ] {
268+ & [ Self :: Enable , Self :: Disable , Self :: CheckOnly ]
257269 }
258270
259- pub ( crate ) fn default_mode ( ) -> & ' static str {
260- "enable"
271+ fn to_possible_value ( & self ) -> Option < PossibleValue > {
272+ Some ( PossibleValue :: new ( self . as_str ( ) ) )
273+ }
274+
275+ fn from_str ( input : & str , _: bool ) -> Result < Self , String > {
276+ <Self as FromStr >:: from_str ( input) . map_err ( |e| e. to_string ( ) )
261277 }
262278}
263279
@@ -272,19 +288,15 @@ impl FromStr for SelfUpdateMode {
272288 _ => Err ( anyhow ! ( format!(
273289 "unknown self update mode: '{}'; valid modes are {}" ,
274290 mode,
275- Self :: modes ( ) . join( ", " ) ,
291+ Self :: value_variants ( ) . iter ( ) . join( ", " )
276292 ) ) ) ,
277293 }
278294 }
279295}
280296
281297impl std:: fmt:: Display for SelfUpdateMode {
282298 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
283- f. write_str ( match self {
284- SelfUpdateMode :: Enable => "enable" ,
285- SelfUpdateMode :: Disable => "disable" ,
286- SelfUpdateMode :: CheckOnly => "check-only" ,
287- } )
299+ f. write_str ( self . as_str ( ) )
288300 }
289301}
290302
0 commit comments