From 0ad276fdaeb502b497fb44a0b062c7cf549b7e4a Mon Sep 17 00:00:00 2001 From: Aaron Weiss Date: Sun, 2 Nov 2014 13:47:22 -0500 Subject: [PATCH] Added load_utf8 to Config and load takes a Path. --- src/bot.rs | 4 ++-- src/data.rs | 12 ++++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/bot.rs b/src/bot.rs index 775a280..4672cb8 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -14,7 +14,7 @@ pub struct IrcBot<'a, T, U> where T: IrcWriter, U: IrcReader { impl<'a> IrcBot<'a, BufferedWriter, BufferedReader> { pub fn new(process: |&IrcBot, BufferedReader>, &str, &str, &[&str]|:'a -> IoResult<()>) -> IoResult, BufferedReader>> { - let config = try!(Config::load("config.json")); + let config = try!(Config::load_utf8("config.json")); let conn = try!(Connection::connect(config.server[], config.port)); Ok(IrcBot { 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, process: |&IrcBot, &str, &str, &[&str]|:'a -> IoResult<()>) -> IoResult> { Ok(IrcBot { conn: conn, - config: try!(Config::load("config.json")), + config: try!(Config::load_utf8("config.json")), process: RefCell::new(process), chanlists: RefCell::new(HashMap::new()), }) diff --git a/src/data.rs b/src/data.rs index 89ce853..da8f792 100644 --- a/src/data.rs +++ b/src/data.rs @@ -86,8 +86,8 @@ pub struct Config { } impl Config { - pub fn load(path: &str) -> IoResult { - let mut file = try!(File::open(&Path::new(path))); + pub fn load(path: Path) -> IoResult { + let mut file = try!(File::open(&path)); let data = try!(file.read_to_string()); decode(data[]).map_err(|e| IoError { kind: InvalidInput, @@ -96,6 +96,10 @@ impl Config { }) } + pub fn load_utf8(path: &str) -> IoResult { + Config::load(Path::new(path)) + } + pub fn is_owner(&self, nickname: &str) -> bool { self.owners[].contains(&String::from_str(nickname)) } @@ -119,12 +123,12 @@ mod test { #[test] fn load_config() { - assert!(Config::load("config.json").is_ok()); + assert!(Config::load_utf8("config.json").is_ok()); } #[test] 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("test2")); }