summaryrefslogtreecommitdiff
path: root/src/datatype/system_info.rs
blob: de776aa0b5fcb381204b482ce4e8fd0274a0efb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use rustc_serialize::{Decoder, Decodable};
use rustc_serialize::json::Json;
use std::process::Command;
use std::str::FromStr;

use datatype::Error;


#[derive(PartialEq, Eq, Debug, Clone)]
pub struct SystemInfo {
    command: String
}

impl SystemInfo {
    pub fn new(command: String) -> SystemInfo {
        SystemInfo { command: command }
    }

    pub fn report(&self) -> Result<Json, Error> {
        Command::new(&self.command)
            .output().map_err(|err| Error::SystemInfo(err.to_string()))
            .and_then(|info| String::from_utf8(info.stdout).map_err(Error::FromUtf8))
            .and_then(|text| Json::from_str(&text).map_err(Error::JsonParser))
    }
}

impl Default for SystemInfo {
    fn default() -> SystemInfo {
        SystemInfo::new("system_info.sh".to_string())
    }
}

impl FromStr for SystemInfo {
    type Err = Error;

    fn from_str(s: &str) -> Result<SystemInfo, Error> {
        Ok(SystemInfo::new(s.to_string()))
    }
}

impl Decodable for SystemInfo {
    fn decode<D: Decoder>(d: &mut D) -> Result<SystemInfo, D::Error> {
        d.read_str().and_then(|s| Ok(s.parse::<SystemInfo>().unwrap()))
    }
}