rust-irc/src/data/config.rs

57 lines
1.6 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
2014-11-02 18:16:49 -05:00
#[deriving(Clone, Decodable)]
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 {
2014-11-03 00:52:15 -05:00
/// Loads a JSON configuration from the desired path.
#[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()),
})
}
2014-11-03 00:52:15 -05:00
/// Loads a JSON configuration using the string as a UTF-8 path.
#[stable]
2014-11-02 18:16:49 -05:00
pub fn load_utf8(path: &str) -> IoResult<Config> {
Config::load(Path::new(path))
}
2014-11-03 00:52:15 -05:00
/// Determines whether or not the nickname provided is the owner of the bot.
#[stable]
2014-11-02 18:16:49 -05:00
pub fn is_owner(&self, nickname: &str) -> bool {
self.owners[].contains(&String::from_str(nickname))
}
}