Skip to content

Commit

Permalink
Use try_* Durations bc of chrono 0.4.35
Browse files Browse the repository at this point in the history
  • Loading branch information
fsktom committed Mar 27, 2024
1 parent 9e391b2 commit c9bac8e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
7 changes: 4 additions & 3 deletions endsong_ui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fn main() {
let entries = SongEntries::new(&paths)
.unwrap_or_else(|e| panic!("{e}"))
.sum_different_capitalization()
.filter(30, Duration::seconds(10));
.filter(30, Duration::try_seconds(10).unwrap());

// test(&entries);
// test_two(&entries);
Expand Down Expand Up @@ -119,7 +119,8 @@ fn test(entries: &SongEntries) {
gather::listening_time(entries.between(&entries.first_date(), &entries.last_date()))
);

let (time, start, end) = entries.max_listening_time(chrono::Duration::weeks(26 * 9));
let (time, start, end) =
entries.max_listening_time(chrono::Duration::try_weeks(26 * 9).unwrap());
dbg!(time.num_minutes(), start.date_naive(), end.date_naive());

dbg!(gather::all_plays(entries.between(&start, &end)));
Expand All @@ -145,7 +146,7 @@ fn test_two(entries: &SongEntries) {
dbg!(a.display());

let ct = Album::new("Waking The Fallen", "Avenged Sevenfold");
let mut alb_dur = Duration::seconds(0);
let mut alb_dur = Duration::try_seconds(0).unwrap();
let ct_songs = entries.find().songs_from_album(&ct);
for song in &ct_songs {
println!(
Expand Down
11 changes: 9 additions & 2 deletions endsong_ui/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ enum UiError {
/// Used when the end date is before the start date
#[error("Date range is in wrong order - end date is before start date!")]
DateWrongOrder,
/// Used when absurdly high time period would lead to panic (shouldn't happen)
#[error("Use a sane time period")]
TimeDeltaOverflow,
}

/// Helper for [`Editor`]
Expand Down Expand Up @@ -357,8 +360,12 @@ fn match_print_max_time(
let duration_num = usr_input_duration.parse::<i64>()?;

let (_, start, end) = match duration_type.as_str() {
"days" => entries.max_listening_time(Duration::days(duration_num)),
"weeks" => entries.max_listening_time(Duration::weeks(duration_num)),
"days" => entries.max_listening_time(
Duration::try_days(duration_num).ok_or(UiError::TimeDeltaOverflow)?,
),
"weeks" => entries.max_listening_time(
Duration::try_weeks(duration_num).ok_or(UiError::TimeDeltaOverflow)?,
),
// is unreachable because of the check above
_ => unreachable!(),
};
Expand Down
13 changes: 10 additions & 3 deletions src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,11 @@ impl SongEntries {
///
/// Minimum duration is 1 day and maximum duration is the whole dataset, so
/// a check is performed and the timespan is adjusted accordingly
///
/// # Panics
///
/// Unwraps used on [`Duration::try_days`], but won't panic since
/// only duration of 1 day created
#[must_use]
pub fn max_listening_time(
&self,
Expand All @@ -360,13 +365,15 @@ impl SongEntries {
let first = self.first_date();
let last = self.last_date();

let one_day: Duration = Duration::try_days(1).unwrap();

let actual_time_span = match time_span {
// maximum duration is whole dataset?
x if x >= last - first => {
return (gather::listening_time(self), first, last);
}
// minimum duration is 1 day
x if x < Duration::days(1) => Duration::days(1),
x if x < one_day => one_day,
// duration is within bounds
_ => time_span,
};
Expand All @@ -385,8 +392,8 @@ impl SongEntries {
start_max = start;
end_max = end;
}
start += Duration::days(1);
end += Duration::days(1);
start += one_day;
end += one_day;
}
(highest, start_max, end_max)
}
Expand Down
3 changes: 2 additions & 1 deletion src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ fn entry_to_songentry(

Some(SongEntry {
timestamp,
time_played: Duration::milliseconds(entry.ms_played),
// unwrap fine since ms_played will never be big enough...
time_played: Duration::try_milliseconds(entry.ms_played).unwrap(),
track,
album,
artist,
Expand Down

0 comments on commit c9bac8e

Please sign in to comment.