use config_file_handler::{self, FileHandler};
use error::InternalError;
use routing::XorName;
use std::ffi::OsString;
#[derive(Clone, Debug, Default, RustcDecodable, RustcEncodable)]
pub struct Config {
pub wallet_address: Option<XorName>,
pub max_capacity: Option<u64>, pub chunk_store_root: Option<String>,
}
pub fn read_config_file() -> Result<Config, InternalError> {
let file_handler = try!(FileHandler::new(&try!(get_file_name()), false));
let cfg = try!(file_handler.read_file());
Ok(cfg)
}
#[cfg(test)]
#[allow(dead_code)]
pub fn write_config_file(config: Config) -> Result<::std::path::PathBuf, InternalError> {
use rustc_serialize::json;
use std::fs::File;
use std::io::Write;
let mut config_path = try!(config_file_handler::current_bin_dir());
config_path.push(try!(get_file_name()));
let mut file = try!(File::create(&config_path));
try!(write!(&mut file, "{}", json::as_pretty_json(&config)));
try!(file.sync_all());
Ok(config_path)
}
fn get_file_name() -> Result<OsString, InternalError> {
let mut name = try!(config_file_handler::exe_file_stem());
name.push(".vault.config");
Ok(name)
}
#[cfg(test)]
mod test {
#[test]
fn parse_sample_config_file() {
use std::path::Path;
use std::fs::File;
use std::io::Read;
use super::Config;
use rustc_serialize::json;
let path = Path::new("installer/common/sample.vault.config").to_path_buf();
let mut file = match File::open(path) {
Ok(file) => file,
Err(what) => {
panic!(format!("Error opening safe_vault.vault.config: {:?}", what));
}
};
let mut encoded_contents = String::new();
if let Err(what) = file.read_to_string(&mut encoded_contents) {
panic!(format!("Error reading safe_vault.vault.config: {:?}", what));
}
if let Err(what) = json::decode::<Config>(&encoded_contents) {
panic!(format!("Error parsing safe_vault.vault.config: {:?}", what));
}
}
}