Skip to content

Commit

Permalink
Implement FromStr trait for Config
Browse files Browse the repository at this point in the history
  • Loading branch information
workanator committed Nov 10, 2015
1 parent f0dbb78 commit 72eb036
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! Internal types used to represent a configuration and corresponding primitives to browse it

use std::collections::HashMap;
use std::str::FromStr;
use std::string::ToString;
use ::error::ConfigError;

/// The top-level `Config` type that represents a configuration
#[derive(PartialEq)]
Expand Down Expand Up @@ -276,6 +278,18 @@ impl Config {
}
}

// Implement `FromStr` for `Config` so it can be constructed using `parse()` method
// on the string slice.
impl FromStr for Config {
type Err = ConfigError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
use ::reader;

reader::from_str(s)
}
}

impl Setting {
/// Creates a new setting with a given name and value
/// # Examples
Expand Down Expand Up @@ -718,4 +732,11 @@ mod test {
value = ScalarValue::Str("this is a string".to_string());
assert_eq!(value.to_string(), "this is a string");
}

#[test]
fn parse_config_from_str_slice() {
let config: Config = "answer=42;".parse().unwrap();
assert!(config.lookup_integer32("answer").is_some());
assert_eq!(config.lookup_integer32("answer").unwrap().to_string(), "42".to_string());
}
}

0 comments on commit 72eb036

Please sign in to comment.