Simplified config loading API with AsRef<Path>.
This commit is contained in:
parent
81b3e58d52
commit
f3a2417f6a
2 changed files with 6 additions and 10 deletions
|
@ -46,7 +46,7 @@ pub struct Config {
|
|||
|
||||
impl Config {
|
||||
/// 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 data = String::new();
|
||||
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.
|
||||
pub fn is_owner(&self, nickname: &str) -> bool {
|
||||
self.owners.as_ref().map(|o| o.contains(&nickname.to_string())).unwrap()
|
||||
|
@ -186,7 +181,7 @@ mod test {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn load_utf8() {
|
||||
fn load_from_str() {
|
||||
let cfg = Config {
|
||||
owners: Some(vec![format!("test")]),
|
||||
nickname: Some(format!("test")),
|
||||
|
@ -204,7 +199,7 @@ mod test {
|
|||
user_info: None,
|
||||
options: Some(HashMap::new()),
|
||||
};
|
||||
assert_eq!(Config::load_utf8("client_config.json").unwrap(), cfg);
|
||||
assert_eq!(Config::load("client_config.json").unwrap(), cfg);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@ use std::borrow::ToOwned;
|
|||
use std::collections::HashMap;
|
||||
use std::error::Error as StdError;
|
||||
use std::io::{BufReader, BufWriter, Error, ErrorKind, Result};
|
||||
use std::path::Path;
|
||||
use std::sync::{Mutex, RwLock};
|
||||
use std::iter::Map;
|
||||
use client::conn::{Connection, NetStream};
|
||||
|
@ -49,8 +50,8 @@ pub type NetIrcServer = IrcServer<BufReader<NetStream>, BufWriter<NetStream>>;
|
|||
impl IrcServer<BufReader<NetStream>, BufWriter<NetStream>> {
|
||||
/// Creates a new IRC Server connection from the configuration at the specified path,
|
||||
/// connecting immediately.
|
||||
pub fn new(config: &str) -> Result<NetIrcServer> {
|
||||
IrcServer::from_config(try!(Config::load_utf8(config)))
|
||||
pub fn new<P: AsRef<Path>>(config: P) -> Result<NetIrcServer> {
|
||||
IrcServer::from_config(try!(Config::load(config)))
|
||||
}
|
||||
|
||||
/// Creates a new IRC server connection from the specified configuration, connecting
|
||||
|
|
Loading…
Reference in a new issue