All ProjectsHome
jobrunner
jobrunner/src/config.rs
config.rs Raw
extern crate toml;

use std::fs::File;
use std::io;
use std::io::prelude::*;
use std::path::{Path, PathBuf};

use self::toml::Value;

#[derive(Debug)]
pub struct Config {
    pub base_dir: PathBuf,
    pub work_dir: PathBuf,
}

impl Config {
    pub fn from_file(conf: &Path) -> io::Result<Config> {
        let mut f = try!(File::open(conf));
        let mut buf = String::new();
        try!(f.read_to_string(&mut buf));
        let t = buf.parse::<Value>().map_err(|e| {
            io::Error::new(io::ErrorKind::InvalidData, "No valid configuration file")
        })?;
        
        let work_dir = match t.get("workspace") {
            Some(&toml::Value::String(ref s)) => PathBuf::from(s),
            _ => return Err(io::Error::new(io::ErrorKind::InvalidData, "Missing option \"workspace\" in configuration file")),
        };
        Ok(Config { base_dir: PathBuf::from(conf.parent().unwrap()), work_dir: PathBuf::from(work_dir), })
    }
}