Added a function to save a Config.

This commit is contained in:
Aaron Weiss 2015-09-16 12:21:58 -04:00
parent 4deda28a00
commit b866785c89

View file

@ -5,7 +5,7 @@ use std::fs::File;
use std::io::prelude::*; use std::io::prelude::*;
use std::io::{Error, ErrorKind, Result}; use std::io::{Error, ErrorKind, Result};
use std::path::Path; use std::path::Path;
use rustc_serialize::json::decode; use rustc_serialize::json::{decode, encode};
/// Configuration data. /// Configuration data.
#[derive(Clone, RustcDecodable, RustcEncodable, Default, PartialEq, Debug)] #[derive(Clone, RustcDecodable, RustcEncodable, Default, PartialEq, Debug)]
@ -55,6 +55,14 @@ impl Config {
) )
} }
/// Saves a JSON configuration to the desired path.
pub fn save<P: AsRef<Path>>(&self, path: P) -> Result<()> {
let mut file = try!(File::create(path));
file.write_all(try!(encode(self).map_err(|_|
Error::new(ErrorKind::InvalidInput, "Failed to encode configuration file.")
)).as_bytes())
}
/// Determines whether or not the nickname provided is the owner of the bot. /// Determines whether or not the nickname provided is the owner of the bot.
pub fn is_owner(&self, nickname: &str) -> bool { pub fn is_owner(&self, nickname: &str) -> bool {
self.owners.as_ref().map(|o| o.contains(&nickname.to_string())).unwrap() self.owners.as_ref().map(|o| o.contains(&nickname.to_string())).unwrap()