From 4df7be1662eec4d9618f5d58dfacaed781eb255e Mon Sep 17 00:00:00 2001 From: Aaron Weiss Date: Sun, 2 Nov 2014 18:16:49 -0500 Subject: [PATCH] Added Config implementation. --- src/data/config.rs | 37 +++++++++++++++++++++++++++++++++++++ src/data/mod.rs | 1 + 2 files changed, 38 insertions(+) create mode 100644 src/data/config.rs diff --git a/src/data/config.rs b/src/data/config.rs new file mode 100644 index 0000000..5fd3d0d --- /dev/null +++ b/src/data/config.rs @@ -0,0 +1,37 @@ +use std::collections::HashMap; +use std::io::fs::File; +use std::io::{InvalidInput, IoError, IoResult}; +use serialize::json::decode; + +#[deriving(Clone, Decodable)] +pub struct Config { + pub owners: Vec, + pub nickname: String, + pub username: String, + pub realname: String, + pub password: String, + pub server: String, + pub port: u16, + pub channels: Vec, + pub options: HashMap, +} + +impl Config { + 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, + desc: "Failed to decode configuration file.", + detail: Some(e.to_string()), + }) + } + + 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)) + } +} diff --git a/src/data/mod.rs b/src/data/mod.rs index 3178a28..471a5ed 100644 --- a/src/data/mod.rs +++ b/src/data/mod.rs @@ -5,4 +5,5 @@ pub mod kinds { impl IrcReader for T where T: Buffer + Sized + Send + 'static {} } +pub mod config; pub mod message;