Implement FromStr for RelroLevel rather than duplicating the match

Signed-off-by: Johannes Löthberg <johannes@kyriasis.com>
This commit is contained in:
Johannes Löthberg 2017-07-18 01:27:55 +02:00
parent 6a8328cfa3
commit 2161fb25ca
3 changed files with 23 additions and 7 deletions

View File

@ -790,9 +790,12 @@ macro_rules! options {
fn parse_relro_level(slot: &mut Option<RelroLevel>, v: Option<&str>) -> bool {
match v {
Some("full") => *slot = Some(RelroLevel::Full),
Some("partial") => *slot = Some(RelroLevel::Partial),
Some("off") => *slot = Some(RelroLevel::Off),
Some(s) => {
match s.parse::<RelroLevel>() {
Ok(level) => *slot = Some(level),
_ => return false
}
},
_ => return false
}
true

View File

@ -47,6 +47,8 @@ pub mod target;
pub mod slice;
pub mod dynamic_lib;
use std::str::FromStr;
use serialize::json::{Json, ToJson};
macro_rules! linker_flavor {
@ -132,6 +134,19 @@ impl RelroLevel {
}
}
impl FromStr for RelroLevel {
type Err = ();
fn from_str(s: &str) -> Result<RelroLevel, ()> {
match s {
"full" => Ok(RelroLevel::Full),
"partial" => Ok(RelroLevel::Partial),
"off" => Ok(RelroLevel::Off),
_ => Err(()),
}
}
}
impl ToJson for RelroLevel {
fn to_json(&self) -> Json {
match *self {

View File

@ -588,10 +588,8 @@ impl Target {
($key_name:ident, RelroLevel) => ( {
let name = (stringify!($key_name)).replace("_", "-");
obj.find(&name[..]).and_then(|o| o.as_string().and_then(|s| {
match s {
"full" => base.options.$key_name = RelroLevel::Full,
"partial" => base.options.$key_name = RelroLevel::Partial,
"off" => base.options.$key_name = RelroLevel::Off,
match s.parse::<RelroLevel>() {
Ok(level) => base.options.$key_name = level,
_ => return Some(Err(format!("'{}' is not a valid value for \
relro-level. Use 'full', 'partial, or 'off'.",
s))),