From d9438c30d59bbe7c4442e4a824a41a2bd00372ac Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Thu, 22 Feb 2018 15:25:31 -0800 Subject: [PATCH] Add ToString and FromStr impls for Epoch --- src/librustc/session/config.rs | 38 +++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/src/librustc/session/config.rs b/src/librustc/session/config.rs index cfbf233297cf..ef85ef8e37e7 100644 --- a/src/librustc/session/config.rs +++ b/src/librustc/session/config.rs @@ -41,7 +41,7 @@ use std::collections::btree_map::Iter as BTreeMapIter; use std::collections::btree_map::Keys as BTreeMapKeysIter; use std::collections::btree_map::Values as BTreeMapValuesIter; -use std::fmt; +use std::{fmt, str}; use std::hash::Hasher; use std::collections::hash_map::DefaultHasher; use std::collections::HashSet; @@ -137,6 +137,28 @@ pub enum Epoch { // as well as changing the default Cargo template. } +pub const ALL_EPOCHS: &[Epoch] = &[Epoch::Epoch2015, Epoch::Epoch2018]; + +impl ToString for Epoch { + fn to_string(&self) -> String { + match *self { + Epoch::Epoch2015 => "2015".into(), + Epoch::Epoch2018 => "2018".into(), + } + } +} + +impl str::FromStr for Epoch { + type Err = (); + fn from_str(s: &str) -> Result { + match s { + "2015" => Ok(Epoch::Epoch2015), + "2018" => Ok(Epoch::Epoch2018), + _ => Err(()) + } + } +} + impl_stable_hash_for!(enum self::OutputType { Bitcode, Assembly, @@ -1021,11 +1043,17 @@ macro_rules! options { fn parse_epoch(slot: &mut Epoch, v: Option<&str>) -> bool { match v { - Some("2015") => *slot = Epoch::Epoch2015, - Some("2018") => *slot = Epoch::Epoch2018, - _ => return false, + Some(s) => { + let epoch = s.parse(); + if let Ok(parsed) = epoch { + *slot = parsed; + true + } else { + false + } + } + _ => false, } - true } } ) }