From cab06fe43fddb0408b0a8f9676a32755d379d8e0 Mon Sep 17 00:00:00 2001 From: Alex Clarke Date: Mon, 10 Mar 2025 16:13:04 -0600 Subject: [PATCH] fix: Marked the `Season.statistics` field as `Option` so that a panic does not happen for outdated Sonarr data. This resolves #35 --- .../sonarr_handler_test_utils.rs | 2 +- src/models/sonarr_models.rs | 2 +- src/network/sonarr_network_tests.rs | 2 +- src/ui/sonarr_ui/library/library_ui_tests.rs | 32 +++++++++---------- src/ui/sonarr_ui/library/mod.rs | 30 ++++++++++++++--- src/ui/sonarr_ui/library/series_details_ui.rs | 6 +++- 6 files changed, 50 insertions(+), 24 deletions(-) diff --git a/src/handlers/sonarr_handlers/sonarr_handler_test_utils.rs b/src/handlers/sonarr_handlers/sonarr_handler_test_utils.rs index 4a6d40c..2691132 100644 --- a/src/handlers/sonarr_handlers/sonarr_handler_test_utils.rs +++ b/src/handlers/sonarr_handlers/sonarr_handler_test_utils.rs @@ -327,7 +327,7 @@ pub(in crate::handlers::sonarr_handlers) mod utils { title: None, season_number: 1, monitored: true, - statistics: season_statistics(), + statistics: Some(season_statistics()), } } diff --git a/src/models/sonarr_models.rs b/src/models/sonarr_models.rs index 4e0dbc2..95f1cdd 100644 --- a/src/models/sonarr_models.rs +++ b/src/models/sonarr_models.rs @@ -288,7 +288,7 @@ pub struct Season { #[serde(deserialize_with = "super::from_i64")] pub season_number: i64, pub monitored: bool, - pub statistics: SeasonStatistics, + pub statistics: Option, } #[derive(Derivative, Serialize, Deserialize, Debug, Default, Clone, PartialEq)] diff --git a/src/network/sonarr_network_tests.rs b/src/network/sonarr_network_tests.rs index 0b8e680..7788d05 100644 --- a/src/network/sonarr_network_tests.rs +++ b/src/network/sonarr_network_tests.rs @@ -5700,7 +5700,7 @@ mod test { title: None, season_number: 1, monitored: true, - statistics: season_statistics(), + statistics: Some(season_statistics()), } } diff --git a/src/ui/sonarr_ui/library/library_ui_tests.rs b/src/ui/sonarr_ui/library/library_ui_tests.rs index a3c082b..f740d39 100644 --- a/src/ui/sonarr_ui/library/library_ui_tests.rs +++ b/src/ui/sonarr_ui/library/library_ui_tests.rs @@ -54,20 +54,20 @@ mod tests { let seasons = vec![ Season { monitored: false, - statistics: SeasonStatistics { + statistics: Some(SeasonStatistics { episode_file_count: 1, episode_count: 3, ..SeasonStatistics::default() - }, + }), ..Season::default() }, Season { monitored: true, - statistics: SeasonStatistics { + statistics: Some(SeasonStatistics { episode_file_count: 3, episode_count: 3, ..SeasonStatistics::default() - }, + }), ..Season::default() }, ]; @@ -89,20 +89,20 @@ mod tests { let seasons = vec![ Season { monitored: true, - statistics: SeasonStatistics { + statistics: Some(SeasonStatistics { episode_file_count: 1, episode_count: 3, ..SeasonStatistics::default() - }, + }), ..Season::default() }, Season { monitored: true, - statistics: SeasonStatistics { + statistics: Some(SeasonStatistics { episode_file_count: 3, episode_count: 3, ..SeasonStatistics::default() - }, + }), ..Season::default() }, ]; @@ -139,20 +139,20 @@ mod tests { let seasons = vec![ Season { monitored: false, - statistics: SeasonStatistics { + statistics: Some(SeasonStatistics { episode_file_count: 1, episode_count: 3, ..SeasonStatistics::default() - }, + }), ..Season::default() }, Season { monitored: true, - statistics: SeasonStatistics { + statistics: Some(SeasonStatistics { episode_file_count: 3, episode_count: 3, ..SeasonStatistics::default() - }, + }), ..Season::default() }, ]; @@ -174,20 +174,20 @@ mod tests { let seasons = vec![ Season { monitored: true, - statistics: SeasonStatistics { + statistics: Some(SeasonStatistics { episode_file_count: 1, episode_count: 3, ..SeasonStatistics::default() - }, + }), ..Season::default() }, Season { monitored: true, - statistics: SeasonStatistics { + statistics: Some(SeasonStatistics { episode_file_count: 3, episode_count: 3, ..SeasonStatistics::default() - }, + }), ..Season::default() }, ]; diff --git a/src/ui/sonarr_ui/library/mod.rs b/src/ui/sonarr_ui/library/mod.rs index a31482c..445c512 100644 --- a/src/ui/sonarr_ui/library/mod.rs +++ b/src/ui/sonarr_ui/library/mod.rs @@ -202,8 +202,19 @@ fn decorate_series_row_with_style<'a>(series: &Series, row: Row<'a>) -> Row<'a> return if seasons .iter() .filter(|season| season.monitored) - .all(|season| season.statistics.episode_file_count == season.statistics.episode_count) - { + .filter(|season| season.statistics.is_some()) + .all(|season| { + season + .statistics + .as_ref() + .expect("Season Statistics is undefined") + .episode_file_count + == season + .statistics + .as_ref() + .expect("Season statistics is undefined") + .episode_count + }) { row.downloaded() } else { row.missing() @@ -217,8 +228,19 @@ fn decorate_series_row_with_style<'a>(series: &Series, row: Row<'a>) -> Row<'a> return if seasons .iter() .filter(|season| season.monitored) - .all(|season| season.statistics.episode_file_count == season.statistics.episode_count) - { + .filter(|season| season.statistics.is_some()) + .all(|season| { + season + .statistics + .as_ref() + .expect("Season Statistics is undefined") + .episode_file_count + == season + .statistics + .as_ref() + .expect("Season statistics is undefined") + .episode_count + }) { row.unreleased() } else { row.missing() diff --git a/src/ui/sonarr_ui/library/series_details_ui.rs b/src/ui/sonarr_ui/library/series_details_ui.rs index d4965a7..647600d 100644 --- a/src/ui/sonarr_ui/library/series_details_ui.rs +++ b/src/ui/sonarr_ui/library/series_details_ui.rs @@ -247,7 +247,11 @@ fn draw_seasons_table(f: &mut Frame<'_>, app: &mut App<'_>, area: Rect) { size_on_disk, next_airing, .. - } = statistics; + } = if let Some(stats) = statistics { + stats + } else { + &SeasonStatistics::default() + }; let season_monitored = if season.monitored { "🏷" } else { "" }; let size = convert_to_gb(*size_on_disk);