Simplified config loading API with AsRef<Path>.

This commit is contained in:
Aaron Weiss 2015-06-05 21:37:21 -04:00
parent 81b3e58d52
commit f3a2417f6a
2 changed files with 6 additions and 10 deletions

View file

@ -46,7 +46,7 @@ pub struct Config {
impl Config { impl Config {
/// Loads a JSON configuration from the desired path. /// Loads a JSON configuration from the desired path.
pub fn load(path: &Path) -> Result<Config> { pub fn load<P: AsRef<Path>>(path: P) -> Result<Config> {
let mut file = try!(File::open(path)); let mut file = try!(File::open(path));
let mut data = String::new(); let mut data = String::new();
try!(file.read_to_string(&mut data)); try!(file.read_to_string(&mut data));
@ -55,11 +55,6 @@ impl Config {
) )
} }
/// Loads a JSON configuration using the string as a UTF-8 path.
pub fn load_utf8(path: &str) -> Result<Config> {
Config::load(Path::new(path))
}
/// Determines whether or not the nickname provided is the owner of the bot. /// Determines whether or not the nickname provided is the owner of the bot.
pub fn is_owner(&self, nickname: &str) -> bool { pub fn is_owner(&self, nickname: &str) -> bool {
self.owners.as_ref().map(|o| o.contains(&nickname.to_string())).unwrap() self.owners.as_ref().map(|o| o.contains(&nickname.to_string())).unwrap()
@ -186,7 +181,7 @@ mod test {
} }
#[test] #[test]
fn load_utf8() { fn load_from_str() {
let cfg = Config { let cfg = Config {
owners: Some(vec![format!("test")]), owners: Some(vec![format!("test")]),
nickname: Some(format!("test")), nickname: Some(format!("test")),
@ -204,7 +199,7 @@ mod test {
user_info: None, user_info: None,
options: Some(HashMap::new()), options: Some(HashMap::new()),
}; };
assert_eq!(Config::load_utf8("client_config.json").unwrap(), cfg); assert_eq!(Config::load("client_config.json").unwrap(), cfg);
} }

View file

@ -5,6 +5,7 @@ use std::borrow::ToOwned;
use std::collections::HashMap; use std::collections::HashMap;
use std::error::Error as StdError; use std::error::Error as StdError;
use std::io::{BufReader, BufWriter, Error, ErrorKind, Result}; use std::io::{BufReader, BufWriter, Error, ErrorKind, Result};
use std::path::Path;
use std::sync::{Mutex, RwLock}; use std::sync::{Mutex, RwLock};
use std::iter::Map; use std::iter::Map;
use client::conn::{Connection, NetStream}; use client::conn::{Connection, NetStream};
@ -49,8 +50,8 @@ pub type NetIrcServer = IrcServer<BufReader<NetStream>, BufWriter<NetStream>>;
impl IrcServer<BufReader<NetStream>, BufWriter<NetStream>> { impl IrcServer<BufReader<NetStream>, BufWriter<NetStream>> {
/// Creates a new IRC Server connection from the configuration at the specified path, /// Creates a new IRC Server connection from the configuration at the specified path,
/// connecting immediately. /// connecting immediately.
pub fn new(config: &str) -> Result<NetIrcServer> { pub fn new<P: AsRef<Path>>(config: P) -> Result<NetIrcServer> {
IrcServer::from_config(try!(Config::load_utf8(config))) IrcServer::from_config(try!(Config::load(config)))
} }
/// Creates a new IRC server connection from the specified configuration, connecting /// Creates a new IRC server connection from the specified configuration, connecting