Skip to content

Commit

Permalink
Merge pull request #81 from JakeStanger/fix/missing-duration-crash
Browse files Browse the repository at this point in the history
Fix/missing duration crash
  • Loading branch information
JakeStanger authored Oct 10, 2023
2 parents 1466991 + 3b46dcc commit 0319a3b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dirs = "5.0.1"
toml = "0.8.2"
regex = "1.9.6"
serde = { version = "1.0.188", features = ["derive"] }
serde_json = "1.0.107"
reqwest = { version = "0.11.22", features = ["json"] }
tokio = { version = "1.33.0", features = ["rt-multi-thread"] }
universal-config = { version = "0.4.3", default-features = false, features = ["toml"] }
Expand Down
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,14 @@ impl<'a> Service<'a> {
drpc.on_ready(move |_| {
event_tx
.try_send(ServiceEvent::Ready)
.expect("channel to be open")
.expect("channel to be open");
});

drpc.on_error(move |err| {
if err
.event
.get("error_message")
.and_then(|v| v.as_str())
.and_then(serde_json::value::Value::as_str)
.map(|str| str == "Io Error")
.unwrap_or_default()
{
Expand All @@ -133,7 +133,7 @@ impl<'a> Service<'a> {
}

fn start(&mut self) {
self.drpc.start()
self.drpc.start();
}

async fn update_state(&mut self, mpd: &MultiHostClient<'a>, status: &Status) {
Expand Down
28 changes: 13 additions & 15 deletions src/mpd_conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ pub fn get_token_value(song: &Song, status: &Status, token: &str) -> String {
"disc" => try_get_first_tag(song.tags.get(&Tag::Disc)),
"genre" => try_get_first_tag(song.tags.get(&Tag::Genre)),
"track" => try_get_first_tag(song.tags.get(&Tag::Track)),
"duration" => return format_time(get_duration(status)),
"elapsed" => return format_time(get_elapsed(status)),
"duration" => return get_duration(status).map_or_else(|| String::from("N/A"), format_time),
"elapsed" => return get_elapsed(status).map_or_else(|| String::from("N/A"), format_time),
_ => Some(token),
}
.unwrap_or("unknown")
Expand All @@ -40,13 +40,17 @@ pub fn get_timestamp(status: &Status, mode: TimestampMode) -> ActivityTimestamps
.expect("Failed to get system time")
.as_secs();

let elapsed = get_elapsed(status);

let timestamps = ActivityTimestamps::new();

let Some(elapsed) = get_elapsed(status) else {
return timestamps;
};

match mode {
TimestampMode::Left => {
let duration = get_duration(status);
let Some(duration) = get_duration(status) else {
return timestamps;
};

let remaining = duration - elapsed;
timestamps.end(current_time + remaining)
Expand All @@ -63,17 +67,11 @@ pub fn try_get_first_tag(vec: Option<&Vec<String>>) -> Option<&str> {
}

/// Gets the duration of the current song
fn get_duration(status: &Status) -> u64 {
status
.duration
.expect("Failed to get duration from MPD status")
.as_secs()
fn get_duration(status: &Status) -> Option<u64> {
status.duration.map(|d| d.as_secs())
}

/// Gets the elapsed time of the current song
fn get_elapsed(status: &Status) -> u64 {
status
.elapsed
.expect("Failed to get elapsed time from MPD status")
.as_secs()
fn get_elapsed(status: &Status) -> Option<u64> {
status.elapsed.map(|e| e.as_secs())
}

0 comments on commit 0319a3b

Please sign in to comment.