lifetime issue

This commit is contained in:
Ultra Desu
2019-11-30 03:14:00 +03:00
parent a9216ff630
commit b95183fd94

View File

@@ -9,6 +9,11 @@ const MID: &str = "#[fg=colour208]";
const HIGH: &str = "#[fg=colour160]"; const HIGH: &str = "#[fg=colour160]";
const END: &str = "#[fg=colour7]"; const END: &str = "#[fg=colour7]";
struct Track_info<'a> {
title: &'a str,
artist: &'a str,
}
fn read_file(file_path: &str) -> String { fn read_file(file_path: &str) -> String {
fs::read_to_string(file_path).expect("Cant read file.") fs::read_to_string(file_path).expect("Cant read file.")
} }
@@ -59,43 +64,56 @@ fn cpu_load_bar(bar_len: i32) {
} }
fn print_refarg(value: &dyn arg::RefArg) { fn print_refarg(value: &dyn arg::RefArg) {
// We don't know what type the value is. We'll try a few and fall back to
// debug printing if the value is more complex than that.
if let Some(s) = value.as_str() { if let Some(s) = value.as_str() {
println!("{}", s); println!("{}", s);
} else if let Some(i) = value.as_i64() { } else if let Some(i) = value.as_i64() {
println!("{}", i); println!("{}", i);
// } else if let Some(mut c) = value.as_iter() { } else if let Some(mut c) = value.as_iter() {
// while let Some(key) = c.next() { while let Some(val) = c.next() {
// // Printing the key is easy, since we know it's a String. if let Some(mut line) = val.as_iter() {
// print!(" {}: ", key.as_str().unwrap()); print!("{:?}", line.next().unwrap());
// let val = c.next().unwrap(); }
// print_refarg(&val); }
// } println!("");
} else { } else {
println!("{:?}", value); println!("{:?}", value);
} }
} }
fn player_info() -> Result<(), Box<dyn std::error::Error>> { //fn player_info(player: &str) -> Result<(), Box<dyn std::error::Error>> {
fn player_info<'a>(player: &'a str) -> Result<Track_info, Box<dyn std::error::Error>> {
let conn = Connection::new_session()?; let conn = Connection::new_session()?;
let mut service: String = "org.mpris.MediaPlayer2.".to_owned();
service.push_str(player);
let proxy = conn.with_proxy( let proxy = conn.with_proxy(
"org.mpris.MediaPlayer2.cmus", service,
"/org/mpris/MediaPlayer2", "/org/mpris/MediaPlayer2",
Duration::from_millis(5000), Duration::from_millis(5000),
); );
let metadata: Box<dyn arg::RefArg> = let metadata: Box<dyn arg::RefArg> = proxy.get("org.mpris.MediaPlayer2.Player", "Metadata")?;
proxy.get("org.mpris.MediaPlayer2.Player", "Metadata")?;
let mut iter = metadata.as_iter().unwrap(); let mut iter = metadata.as_iter().unwrap();
let mut track_info = Track_info {
println!("Option 2:"); artist: "",
title: "",
};
while let Some(key) = iter.next() { while let Some(key) = iter.next() {
// Printing the key is easy, since we know it's a String. if key.as_str() == Some("xesam:title") {
print!(" {}: ", key.as_str().unwrap()); if let Some(title) = iter.next().unwrap().as_str() {
let value = iter.next().unwrap(); track_info.title = title;
print_refarg(&value); }
}
if key.as_str() == Some("xesam:artist") {
if let Some(mut artists) = iter.next().unwrap().as_iter() {
while let Some(artist) = artists.next() {
if let Some(mut line) = artist.as_iter() {
track_info.artist = line.next().unwrap().as_str().unwrap();
}
}
}
}
} }
Ok(()) //Ok(Box::new(track_info))
Ok(track_info)
} }
fn main() { fn main() {
@@ -109,8 +127,10 @@ fn main() {
"-cb" => cpu_load_bar(15), "-cb" => cpu_load_bar(15),
"-mb" => mem_load_bar(15), "-mb" => mem_load_bar(15),
"-p" => { "-p" => {
let x = player_info(); match player_info("cmus") {
println!("{:?}", x); Ok(track_info) => println!("{:?}, {:?}", track_info.title, track_info.artist),
Err(e) => panic!("Error"),
}
} }
_ => panic!(help_text), _ => panic!(help_text),
}, },