rust-irc/src/data/config.rs

113 lines
3.3 KiB
Rust
Raw Normal View History

2014-11-03 00:52:15 -05:00
//! JSON configuration files using libserialize
#![stable]
2014-11-02 18:16:49 -05:00
use std::collections::HashMap;
use std::io::fs::File;
use std::io::{InvalidInput, IoError, IoResult};
use serialize::json::decode;
2014-11-03 00:52:15 -05:00
/// Configuration data
#[deriving(Clone, Decodable, PartialEq, Show)]
2014-11-03 00:52:15 -05:00
#[unstable]
2014-11-02 18:16:49 -05:00
pub struct Config {
2014-11-03 00:52:15 -05:00
/// A list of the owners of the bot by nickname
2014-11-02 18:16:49 -05:00
pub owners: Vec<String>,
2014-11-03 00:52:15 -05:00
/// The bot's nickname
2014-11-02 18:16:49 -05:00
pub nickname: String,
2014-11-03 00:52:15 -05:00
/// The bot's username
2014-11-02 18:16:49 -05:00
pub username: String,
2014-11-03 00:52:15 -05:00
/// The bot's real name
2014-11-02 18:16:49 -05:00
pub realname: String,
2014-11-03 00:52:15 -05:00
/// The bot's password
2014-11-02 18:16:49 -05:00
pub password: String,
2014-11-03 00:52:15 -05:00
/// The server to connect to
2014-11-02 18:16:49 -05:00
pub server: String,
2014-11-03 00:52:15 -05:00
/// The port to connect on
2014-11-02 18:16:49 -05:00
pub port: u16,
2014-11-03 00:52:15 -05:00
/// A list of channels to join on connection
2014-11-02 18:16:49 -05:00
pub channels: Vec<String>,
2014-11-03 00:52:15 -05:00
/// A map of additional options to be stored in config
2014-11-02 18:16:49 -05:00
pub options: HashMap<String, String>,
}
impl Config {
/// Loads a JSON configuration from the desired path
2014-11-03 00:52:15 -05:00
#[stable]
2014-11-02 18:16:49 -05:00
pub fn load(path: Path) -> IoResult<Config> {
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()),
})
}
/// Loads a JSON configuration using the string as a UTF-8 path
2014-11-03 00:52:15 -05:00
#[stable]
2014-11-02 18:16:49 -05:00
pub fn load_utf8(path: &str) -> IoResult<Config> {
Config::load(Path::new(path))
}
/// Determines whether or not the nickname provided is the owner of the bot
2014-11-03 00:52:15 -05:00
#[stable]
2014-11-02 18:16:49 -05:00
pub fn is_owner(&self, nickname: &str) -> bool {
self.owners[].contains(&String::from_str(nickname))
}
}
#[cfg(test)]
mod test {
use super::Config;
use std::collections::HashMap;
#[test]
fn load() {
let cfg = Config {
owners: vec![format!("test")],
nickname: format!("test"),
username: format!("test"),
realname: format!("test"),
password: String::new(),
server: format!("irc.test.net"),
port: 6667,
channels: vec![format!("#test"), format!("#test2")],
options: HashMap::new(),
};
assert_eq!(Config::load(Path::new("config.json")), Ok(cfg));
}
#[test]
fn load_utf8() {
let cfg = Config {
owners: vec![format!("test")],
nickname: format!("test"),
username: format!("test"),
realname: format!("test"),
password: String::new(),
server: format!("irc.test.net"),
port: 6667,
channels: vec![format!("#test"), format!("#test2")],
options: HashMap::new(),
};
assert_eq!(Config::load_utf8("config.json"), Ok(cfg));
}
#[test]
fn is_owner() {
let cfg = Config {
owners: vec![format!("test"), format!("test2")],
nickname: format!("test"),
username: format!("test"),
realname: format!("test"),
password: String::new(),
server: format!("irc.test.net"),
port: 6667,
channels: Vec::new(),
options: HashMap::new(),
};
assert!(cfg.is_owner("test"));
assert!(cfg.is_owner("test2"));
assert!(!cfg.is_owner("test3"));
}
}