diff --git a/src/client.rs b/src/client.rs index 1fee6a5..09ff8ca 100644 --- a/src/client.rs +++ b/src/client.rs @@ -200,7 +200,9 @@ impl TogglClient { let start_date = start.format("%Y-%m-%d").to_string(); // End date is not inclusive, therefore we add one day - let end_date = (end + Duration::days(1)).format("%Y-%m-%d").to_string(); + let end_date = (end + Duration::try_days(1).unwrap()) + .format("%Y-%m-%d") + .to_string(); let uri = format!( "me/time_entries?start_date={}&end_date={}", diff --git a/src/client_tests.rs b/src/client_tests.rs index 24bca36..e42053c 100644 --- a/src/client_tests.rs +++ b/src/client_tests.rs @@ -392,7 +392,7 @@ fn create_time_entry() -> anyhow::Result<()> { &Some("Wurst".to_string()), 123456789, &Some(vec!["aa".to_string(), "bb".to_string()]), - Duration::seconds(200), + Duration::try_seconds(200).unwrap(), DateTime::::from_str("2021-11-21T23:58:09+01:00")?, 123456789, false, diff --git a/src/commands/reports.rs b/src/commands/reports.rs index 28df81f..3e23d50 100644 --- a/src/commands/reports.rs +++ b/src/commands/reports.rs @@ -58,7 +58,7 @@ pub fn detailed( for (user, time_entries) in time_entries_by_user { let total_hours = time_entries .iter() - .map(|time_entry| Duration::milliseconds(time_entry.dur as i64)) + .flat_map(|time_entry| Duration::try_milliseconds(time_entry.dur as i64)) .fold(Duration::zero(), |a, b| a + b); println!(); @@ -77,7 +77,9 @@ pub fn detailed( let hours = time_entries .iter() - .map(|time_entry| Duration::milliseconds(time_entry.dur as i64)) + .flat_map(|time_entry| { + Duration::try_milliseconds(time_entry.dur as i64) + }) .fold(Duration::zero(), |a, b| a + b); let start = time_entries @@ -121,8 +123,9 @@ pub fn detailed( // https://www.gesetze-im-internet.de/arbzg/__4.html#:~:text=Arbeitszeitgesetz%20(ArbZG),neun%20Stunden%20insgesamt%20zu%20unterbrechen. let formatted_break = if let Some(r#break) = r#break { // between 6 and less than 10 hours, break has to be at least 30 minutes - if (hours > Duration::hours(6) && hours < Duration::hours(10)) - && r#break < Duration::minutes(30) + if (hours > Duration::try_hours(6).unwrap() + && hours < Duration::try_hours(10).unwrap()) + && r#break < Duration::try_minutes(30).unwrap() { warnings.push( format!( @@ -133,7 +136,8 @@ pub fn detailed( ); } // more than 9 hours, break has to be at least 45 minutes - else if hours > Duration::hours(9) && r#break < Duration::minutes(45) + else if hours > Duration::try_hours(9).unwrap() + && r#break < Duration::try_minutes(45).unwrap() { warnings.push( format!( diff --git a/src/commands/time_entries.rs b/src/commands/time_entries.rs index b019925..7ddbf4d 100644 --- a/src/commands/time_entries.rs +++ b/src/commands/time_entries.rs @@ -133,7 +133,7 @@ fn collect_output_entries( let duration = if entry.duration.is_negative() { Duration::zero() } else { - Duration::seconds(entry.duration) + Duration::try_seconds(entry.duration).unwrap_or_default() }; output_entries.push(OutputEntry { @@ -222,7 +222,7 @@ pub fn create( } fn launch_break() -> Duration { - Duration::hours(1) + Duration::try_hours(1).unwrap() } pub(super) fn calculate_duration( @@ -471,7 +471,12 @@ fn output_values_table(output_entries: &[OutputEntry]) { let date_row = Row::new(vec![ TableCell::new(date.to_string().bold()), - TableCell::new(Duration::seconds(time_sum).hhmmss().bold()), + TableCell::new( + Duration::try_seconds(time_sum) + .unwrap_or_default() + .hhmmss() + .bold(), + ), TableCell::new(""), TableCell::new(""), TableCell::new(""), @@ -524,7 +529,8 @@ fn output_values_table(output_entries: &[OutputEntry]) { let total_sum_row = Row::new(vec![ TableCell::new("Total".bold()), TableCell::new( - Duration::seconds(total_time_sum) + Duration::try_seconds(total_time_sum) + .unwrap_or_default() .hhmmss() .bold() .underline(), diff --git a/src/commands/time_entries_tests.rs b/src/commands/time_entries_tests.rs index 950d401..3834983 100644 --- a/src/commands/time_entries_tests.rs +++ b/src/commands/time_entries_tests.rs @@ -30,7 +30,7 @@ fn test_calculate_duration() -> anyhow::Result<()> { project: "fkbr".to_string(), start: DateTime::::from_str("2021-11-21T22:58:09Z")?, end: None, - duration: Some(Duration::hours(2)), + duration: Some(Duration::try_hours(2).unwrap()), non_billable: false, lunch_break: false, description: None, @@ -39,7 +39,7 @@ fn test_calculate_duration() -> anyhow::Result<()> { assert_eq!( calculate_duration(&time_entry_with_duration_but_without_end)?, - Duration::hours(2) + Duration::try_hours(2).unwrap() ); let time_entry_without_duration_but_with_end = CreateTimeEntry { @@ -55,7 +55,7 @@ fn test_calculate_duration() -> anyhow::Result<()> { assert_eq!( calculate_duration(&time_entry_without_duration_but_with_end)?, - Duration::hours(2) + Duration::try_hours(2).unwrap() ); let time_entry_without_duration_and_without_end = CreateTimeEntry { @@ -92,7 +92,7 @@ fn test_calculate_duration() -> anyhow::Result<()> { calculate_duration( &time_entry_with_duration_but_without_end_and_lunch_break )?, - Duration::hours(1) + Duration::try_hours(1).unwrap() ); let time_entry_with_duration_but_without_end_and_lunch_break = @@ -100,7 +100,7 @@ fn test_calculate_duration() -> anyhow::Result<()> { project: "fkbr".to_string(), start: DateTime::::from_str("2021-11-21T22:58:09Z")?, end: None, - duration: Some(Duration::hours(2)), + duration: Duration::try_hours(2), non_billable: false, lunch_break: false, description: None, @@ -111,7 +111,7 @@ fn test_calculate_duration() -> anyhow::Result<()> { calculate_duration( &time_entry_with_duration_but_without_end_and_lunch_break )?, - Duration::hours(2) + Duration::try_hours(2).unwrap() ); let time_entry_with_start_is_the_same_as_end = CreateTimeEntry { @@ -254,7 +254,7 @@ fn test_create_workday_with_pause_2_hours() -> anyhow::Result<()> { description: Some("fkbr".to_string()), start: DateTime::::from_str("2021-11-21T22:58:09Z")?, end: None, - duration: Some(Duration::hours(2)), + duration: Duration::try_hours(2), lunch_break: false, project: "betamale gmbh".to_string(), tags: None, @@ -403,7 +403,7 @@ fn test_create_workday_with_pause_7_hours() -> anyhow::Result<()> { description: Some("fkbr".to_string()), start: DateTime::::from_str("2021-11-21T22:58:09+01:00")?, end: None, - duration: Some(Duration::hours(7)), + duration: Duration::try_hours(7), lunch_break: true, project: "betamale gmbh".to_string(), tags: None, diff --git a/src/model.rs b/src/model.rs index d9890a9..0bbf42b 100644 --- a/src/model.rs +++ b/src/model.rs @@ -93,7 +93,7 @@ impl Range { missing_days.push(it); } - it += Duration::days(1); + it += Duration::try_days(1).unwrap(); } Ok(missing_days) @@ -108,19 +108,19 @@ impl Range { .single() .ok_or_else(|| anyhow::anyhow!("Could not create start datetime"))?; - let end = start + Duration::days(1); + let end = start + Duration::try_days(1).unwrap(); Ok((start, end)) } Range::Yesterday => { - let now = Local::now() - Duration::days(1); + let now = Local::now() - Duration::try_days(1).unwrap(); let start = Local .with_ymd_and_hms(now.year(), now.month(), now.day(), 0, 0, 0) .single() .ok_or_else(|| anyhow::anyhow!("Could not create start datetime"))?; - let end = start + Duration::days(1); + let end = start + Duration::try_days(1).unwrap(); Ok((start, end)) } @@ -130,7 +130,7 @@ impl Range { Ok((now.beginning_of_week(), now.end_of_week())) } Range::LastWeek => { - let now = Local::now() - Duration::weeks(1); + let now = Local::now() - Duration::try_weeks(1).unwrap(); Ok((now.beginning_of_week(), now.end_of_week())) } @@ -161,7 +161,7 @@ impl Range { ) })?; - let end = end + Duration::days(1); + let end = end + Duration::try_days(1).unwrap(); Ok(( Local.from_local_datetime(&start).unwrap(), @@ -179,7 +179,7 @@ impl Range { ) })?; - let end = start + Duration::days(1); + let end = start + Duration::try_days(1).unwrap(); Ok((start, end)) }