mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-22 14:55:26 +00:00
Implement FromStr for RelroLevel rather than duplicating the match
Signed-off-by: Johannes Löthberg <johannes@kyriasis.com>
This commit is contained in:
parent
6a8328cfa3
commit
2161fb25ca
@ -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
|
||||
|
@ -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 {
|
||||
|
@ -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))),
|
||||
|
Loading…
Reference in New Issue
Block a user