Skip to content

Commit

Permalink
chore: use try_<hours|minutes|...>
Browse files Browse the repository at this point in the history
- use .unwrap() where safe and .unwrap_or_default() in other places (good enough for now)
  • Loading branch information
icepuma committed Mar 18, 2024
1 parent 080983a commit c61f9e7
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 26 deletions.
4 changes: 3 additions & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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={}",
Expand Down
2 changes: 1 addition & 1 deletion src/client_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Local>::from_str("2021-11-21T23:58:09+01:00")?,
123456789,
false,
Expand Down
14 changes: 9 additions & 5 deletions src/commands/reports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!();
Expand All @@ -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
Expand Down Expand Up @@ -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!(
Expand All @@ -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!(
Expand Down
14 changes: 10 additions & 4 deletions src/commands/time_entries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -222,7 +222,7 @@ pub fn create(
}

fn launch_break() -> Duration {
Duration::hours(1)
Duration::try_hours(1).unwrap()
}

pub(super) fn calculate_duration(
Expand Down Expand Up @@ -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(""),
Expand Down Expand Up @@ -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(),
Expand Down
16 changes: 8 additions & 8 deletions src/commands/time_entries_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn test_calculate_duration() -> anyhow::Result<()> {
project: "fkbr".to_string(),
start: DateTime::<Local>::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,
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -92,15 +92,15 @@ 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 =
CreateTimeEntry {
project: "fkbr".to_string(),
start: DateTime::<Local>::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,
Expand All @@ -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 {
Expand Down Expand Up @@ -254,7 +254,7 @@ fn test_create_workday_with_pause_2_hours() -> anyhow::Result<()> {
description: Some("fkbr".to_string()),
start: DateTime::<Local>::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,
Expand Down Expand Up @@ -403,7 +403,7 @@ fn test_create_workday_with_pause_7_hours() -> anyhow::Result<()> {
description: Some("fkbr".to_string()),
start: DateTime::<Local>::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,
Expand Down
14 changes: 7 additions & 7 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl Range {
missing_days.push(it);
}

it += Duration::days(1);
it += Duration::try_days(1).unwrap();
}

Ok(missing_days)
Expand All @@ -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))
}
Expand All @@ -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()))
}
Expand Down Expand Up @@ -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(),
Expand All @@ -179,7 +179,7 @@ impl Range {
)
})?;

let end = start + Duration::days(1);
let end = start + Duration::try_days(1).unwrap();

Ok((start, end))
}
Expand Down

0 comments on commit c61f9e7

Please sign in to comment.