Added load_utf8 to Config and load takes a Path.

This commit is contained in:
Aaron Weiss 2014-11-02 13:47:22 -05:00
parent f8a6987fcf
commit 0ad276fdae
2 changed files with 10 additions and 6 deletions

View file

@ -14,7 +14,7 @@ pub struct IrcBot<'a, T, U> where T: IrcWriter, U: IrcReader {
impl<'a> IrcBot<'a, BufferedWriter<TcpStream>, BufferedReader<TcpStream>> { impl<'a> IrcBot<'a, BufferedWriter<TcpStream>, BufferedReader<TcpStream>> {
pub fn new(process: |&IrcBot<BufferedWriter<TcpStream>, BufferedReader<TcpStream>>, &str, &str, &[&str]|:'a -> IoResult<()>) -> IoResult<IrcBot<'a, BufferedWriter<TcpStream>, BufferedReader<TcpStream>>> { pub fn new(process: |&IrcBot<BufferedWriter<TcpStream>, BufferedReader<TcpStream>>, &str, &str, &[&str]|:'a -> IoResult<()>) -> IoResult<IrcBot<'a, BufferedWriter<TcpStream>, BufferedReader<TcpStream>>> {
let config = try!(Config::load("config.json")); let config = try!(Config::load_utf8("config.json"));
let conn = try!(Connection::connect(config.server[], config.port)); let conn = try!(Connection::connect(config.server[], config.port));
Ok(IrcBot { Ok(IrcBot {
conn: conn, conn: conn,
@ -123,7 +123,7 @@ impl<'a, T, U> IrcBot<'a, T, U> where T: IrcWriter, U: IrcReader {
pub fn from_connection(conn: Connection<T, U>, process: |&IrcBot<T, U>, &str, &str, &[&str]|:'a -> IoResult<()>) -> IoResult<IrcBot<'a, T, U>> { pub fn from_connection(conn: Connection<T, U>, process: |&IrcBot<T, U>, &str, &str, &[&str]|:'a -> IoResult<()>) -> IoResult<IrcBot<'a, T, U>> {
Ok(IrcBot { Ok(IrcBot {
conn: conn, conn: conn,
config: try!(Config::load("config.json")), config: try!(Config::load_utf8("config.json")),
process: RefCell::new(process), process: RefCell::new(process),
chanlists: RefCell::new(HashMap::new()), chanlists: RefCell::new(HashMap::new()),
}) })

View file

@ -86,8 +86,8 @@ pub struct Config {
} }
impl Config { impl Config {
pub fn load(path: &str) -> IoResult<Config> { pub fn load(path: Path) -> IoResult<Config> {
let mut file = try!(File::open(&Path::new(path))); let mut file = try!(File::open(&path));
let data = try!(file.read_to_string()); let data = try!(file.read_to_string());
decode(data[]).map_err(|e| IoError { decode(data[]).map_err(|e| IoError {
kind: InvalidInput, kind: InvalidInput,
@ -96,6 +96,10 @@ impl Config {
}) })
} }
pub fn load_utf8(path: &str) -> IoResult<Config> {
Config::load(Path::new(path))
}
pub fn is_owner(&self, nickname: &str) -> bool { pub fn is_owner(&self, nickname: &str) -> bool {
self.owners[].contains(&String::from_str(nickname)) self.owners[].contains(&String::from_str(nickname))
} }
@ -119,12 +123,12 @@ mod test {
#[test] #[test]
fn load_config() { fn load_config() {
assert!(Config::load("config.json").is_ok()); assert!(Config::load_utf8("config.json").is_ok());
} }
#[test] #[test]
fn is_owner() { fn is_owner() {
let cfg = Config::load("config.json").unwrap(); let cfg = Config::load_utf8("config.json").unwrap();
assert!(cfg.is_owner("test")); assert!(cfg.is_owner("test"));
assert!(!cfg.is_owner("test2")); assert!(!cfg.is_owner("test2"));
} }