From b866785c8972b16b3d2d3483fd3fb8b4b1ba822e Mon Sep 17 00:00:00 2001 From: Aaron Weiss Date: Wed, 16 Sep 2015 12:21:58 -0400 Subject: [PATCH] Added a function to save a Config. --- src/client/data/config.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/client/data/config.rs b/src/client/data/config.rs index c1b6709..835a4e9 100644 --- a/src/client/data/config.rs +++ b/src/client/data/config.rs @@ -5,7 +5,7 @@ use std::fs::File; use std::io::prelude::*; use std::io::{Error, ErrorKind, Result}; use std::path::Path; -use rustc_serialize::json::decode; +use rustc_serialize::json::{decode, encode}; /// Configuration data. #[derive(Clone, RustcDecodable, RustcEncodable, Default, PartialEq, Debug)] @@ -55,6 +55,14 @@ impl Config { ) } + /// Saves a JSON configuration to the desired path. + pub fn save>(&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. pub fn is_owner(&self, nickname: &str) -> bool { self.owners.as_ref().map(|o| o.contains(&nickname.to_string())).unwrap()