diff --git a/src/main.rs b/src/main.rs index 5f363cf53544e82e35984a7091ec0dcaa672a22f..a59aca4d5cdcb3d93929a4077dd0a2a792a2fac2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,27 +26,16 @@ pub fn main() { // load Xsessions + let sessions = vec![]; for p in glob::glob("/usr/share/xsessions/*.desktop").unwrap() { + // glob gives you all the files, and those that don't match as Err(_)… if let Ok(path) = p { - let conf = Ini::load_from_file(path.to_str().unwrap()); - match conf { - Ok(c) => { - println!("ok for file {}", path.to_str().unwrap()); - for (sec, prop) in c.iter() { - println!("Section: {:?}", sec); - for (key, value) in prop.iter() { - println!("{:?}:{:?}", key, value); - } - } - println!("\n\n"); - } - Err(e) => { - println!("error in file {}: {}", path.to_str().unwrap(), e); - } - } + let info = load_xsession_file(path.to_str().unwrap()); + println!("{:?}", info); + sessions.push(info); } - } + } return; @@ -93,3 +82,62 @@ pub fn main() { // now do actual stuff } + + +#[derive(Debug,PartialEq)] +struct XSessionInfo { + name: String, + exec: String, + desktop_file: String, + + comment: Option<String>, +} + + +/* TODO: + * - do actual error handling instead of unwrap() everywhere… + * - multilanguage: have a map language -> comment in the XSessionInfo? + * **/ +fn load_xsession_file(fname: &str) -> Result<XSessionInfo, String> { + let conf = Ini::load_from_file(fname); + match conf { + Ok(c) => { + // spec says you need to have a 'Desktop Entry' section + let sec = c.section(Some("Desktop Entry")).unwrap(); + let ty = sec.get("Type").unwrap(); + if ty != "XSession" && ty != "Application" { + return Err(format!("{}: type {} is not known", fname, ty)); + } + + let name = sec.get("Name").unwrap(); + let exec = sec.get("Exec").unwrap(); + + let comment = sec.get("Comment"); + let desktopFile = fname; + + // geht das schöner? + // (in haskell wär's liftA to_string .. :/) + let opt_comment : Option<String> = match comment { + Some(c) => { + Some(c.to_string()) + } + None => { + None + } + }; + + Ok(XSessionInfo { + name: name.to_string(), + comment: opt_comment, + exec: exec.to_string(), + desktop_file: desktopFile.to_string(), + }) + } + Err(e) => { + println!("error in file {}: {}", fname, e); + + // return error as string, because types + Err(format!("{}", e).to_string()) + } + } +}