rust-irc/src/data/config.rs

119 lines
3.5 KiB
Rust
Raw Normal View History

//! JSON configuration files using libserialize.
2014-11-03 00:52:15 -05:00
#![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;
/// 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 {
/// A list of the owners of the bot by nickname.
2014-11-02 18:16:49 -05:00
pub owners: Vec<String>,
/// The bot's nickname.
2014-11-02 18:16:49 -05:00
pub nickname: String,
/// The bot's username.
2014-11-02 18:16:49 -05:00
pub username: String,
/// The bot's real name.
2014-11-02 18:16:49 -05:00
pub realname: String,
/// The bot's password.
2014-11-02 18:16:49 -05:00
pub password: String,
/// The server to connect to.
2014-11-02 18:16:49 -05:00
pub server: String,
/// The port to connect on.
2014-11-02 18:16:49 -05:00
pub port: u16,
/// Whether or not to use SSL.
2014-11-08 23:21:55 -05:00
/// Bots will automatically panic if this is enabled without SSL support.
pub use_ssl: bool,
/// A list of channels to join on connection.
2014-11-02 18:16:49 -05:00
pub channels: Vec<String>,
/// 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,
use_ssl: false,
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,
use_ssl: false,
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,
use_ssl: false,
channels: Vec::new(),
options: HashMap::new(),
};
assert!(cfg.is_owner("test"));
assert!(cfg.is_owner("test2"));
assert!(!cfg.is_owner("test3"));
}
}