use std::io::Read;
use std;
use serde_json;

#[derive(Serialize, Deserialize)]
pub struct ConfigData{
    pub username:String,
    pub password:String,
    pub channels:Vec<String>,
    pub admins:Vec<String>,
    pub nyaa:Nyaa,
}

#[derive(Serialize, Deserialize,Clone)]
pub struct 
{
    pub delay:u64,
}

#[derive(Debug)]
pub enum ConfigErr{
    Parse,
    Open,
    Read
}


impl ConfigData{
    pub fn new(file: &str)->Result<ConfigData,ConfigErr>{
        let s = try!(file_to_string(file));

        serde_json::from_str(&s).map_err(|_|ConfigErr::Parse)
    }
}

fn file_to_string(file: &str)->Result<String,ConfigErr>{
    let mut f = try!(std::fs::File::open(file).map_err(|_|ConfigErr::Open));
    let mut s = String::new();
    match f.read_to_string(&mut s){
        Ok(_)=>Ok(s),
        Err(_)=>Err(ConfigErr::Read),
    }

}

#[cfg(test)]
mod test {
    #[test]
    fn new_config_data(){
        let mut cd = ConfigData::new("tests/config_test.json").unwrap();
        assert_eq!("name",cd.username());
        assert_eq!("oauth:1234",cd.password());
        assert_eq!("___4Header",cd.channels()[0]);
        assert_eq!("PagChomp",cd.channels()[1]);
        assert_eq!("Keepo",cd.channels()[2]);
        assert_eq!(3,cd.channels().len());
        assert_eq!("443297327",cd.admins()[0]);
        assert_eq!("443417327",cd.admins()[1]);
        assert_eq!(2,cd.admins().len());
        assert_eq!(100,cd.nyaa().delay().to_owned());
    }
}

Nyaa