summaryrefslogtreecommitdiff
path: root/src/datatype/system_info.rs
blob: 85d1de604a7bc421b94aa8c1a43ebb09a9ce87f0 (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
46
47
48
49
use rustc_serialize::{Decoder, Decodable};
use rustc_serialize::json::Json;

use std::process::Command;
use std::str::FromStr;

use datatype::Error;

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

impl SystemInfo {

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

    pub fn get_json(&self) -> Result<Json,Error> {
        Command::new(&self.script)
            .output().map_err(|e| Error::SystemInfo(format!("Error getting system info: {}", e)))
            .and_then(|c| String::from_utf8(c.stdout)
                     .map_err(|e| Error::FromUtf8(e)))
            .and_then(|s| Json::from_str(&s)
                     .map_err(|e| Error::JsonParser(e)))
    }
}

impl Default for SystemInfo {
    fn default() -> SystemInfo {
        SystemInfo::new("sota-system-info".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| s.parse::<SystemInfo>()
                    .map_err(|e|d.error(&format!("Error couldn't parse SystemInfo: {}",e))))
    }
}