diff options
Diffstat (limited to 'src/systemd')
| -rw-r--r-- | src/systemd/job.rs | 30 |
1 files changed, 25 insertions, 5 deletions
diff --git a/src/systemd/job.rs b/src/systemd/job.rs index 68fb18d..967bb42 100644 --- a/src/systemd/job.rs +++ b/src/systemd/job.rs @@ -5,8 +5,23 @@ use std::io; #[derive(Debug)] pub struct Unit { - name: String, - type_: String + pub name: String, + pub type_: String +} + +pub fn parse_key_value(line: &str) -> Option<(String, String)> { + let pos = line.find('='); + pos.map(|pos| { + let (k, v) = line.split_at(pos); + (k.to_owned(), v[1..].to_owned()) + }) +} + +#[test] +fn test_parse_key_value() { + assert_eq!(None, parse_key_value("ab")); + assert_eq!(Some(("a".to_owned(), "b".to_owned())), parse_key_value("a=b")); + assert_eq!(Some(("a".to_owned(), "b=c".to_owned())), parse_key_value("a=b=c")); } pub fn get_jobs() -> io::Result<Vec<Unit>> { @@ -14,10 +29,15 @@ pub fn get_jobs() -> io::Result<Vec<Unit>> { let status = try!(Command::new("systemctl") .args(&["show", "*"]).output()); + let mut unit_info = HashMap::new(); for line in String::from_utf8_lossy(&status.stdout).split('\n') { - let words = line.split('=').collect::<Vec<&str>>(); - if words.len() == 2 && words[0] == "Id" { - units.push(Unit { name: words[1].to_owned(), type_: "Unknown".to_owned() }); + if let Some((k, v)) = parse_key_value(line) { + unit_info.insert(k, v); + } else { + if let Some(id) = unit_info.remove("Id") { + units.push(Unit { name: id, type_: String::new() }); + } + unit_info = HashMap::new(); } } return Ok(units); |
