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
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
use std::fmt::Error;
use std::fmt::{Display, Formatter};

pub struct Process {
    pub user: String,
    pub pid: String,
    pub cpu: Option<String>,
    pub memory: Option<String>,
    pub vsz: Option<String>,
    pub rss: Option<String>,
    pub tty: Option<String>,
    pub stat: Option<String>,
    pub start: Option<String>,
    pub time: Option<String>,
    pub command: String,
}

#[derive(RustcEncodable, RustcDecodable)]
#[allow(non_snake_case)]
pub struct Top {
    pub Titles: Vec<String>,
    pub Processes: Vec<Vec<String>>
}

impl Display for Process {
    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
        let mut s = String::new();

        s.push_str(&*self.user.clone());

        s.push_str(",");
        s.push_str(&*self.pid.clone());

        match self.cpu.clone() {
            Some(v) => {
                s.push_str(",");
                s.push_str(&*v);
            },
            None => {}
        }

        match self.memory.clone() {
            Some(v) => {
                s.push_str(",");
                s.push_str(&*v);
            },
            None => {}
        }
        
        match self.vsz.clone() {
            Some(v) => {
                s.push_str(",");
                s.push_str(&*v);
            },
            None => {}
        }

        match self.rss.clone() {
            Some(v) => {
                s.push_str(",");
                s.push_str(&*v);
            },
            None => {}
        }

        match self.tty.clone() {
            Some(v) => {
                s.push_str(",");
                s.push_str(&*v);
            },
            None => {}
        }

        match self.stat.clone() {
            Some(v) => {
                s.push_str(",");
                s.push_str(&*v);
            },
            None => {}
        }

        match self.start.clone() {
            Some(v) => {
                s.push_str(",");
                s.push_str(&*v);
            },
            None => {}
        }

        match self.time.clone() {
            Some(v) => {
                s.push_str(",");
                s.push_str(&*v);
            },
            None => {}
        }

        s.push_str(",");
        s.push_str(&*self.command.clone());
        
        write!(f, "{}", s)
    }
}