Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apply some of clippy's suggestions #1

Merged
merged 9 commits into from
Oct 20, 2023
7 changes: 0 additions & 7 deletions src/error.rs

This file was deleted.

61 changes: 34 additions & 27 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,22 @@
//! println!("Hour: {:?}", hour);
//! }
//! ```
#![warn(clippy::all, clippy::pedantic, clippy::nursery)]
use std::cmp::Ordering;

use chrono::{Duration, Local, NaiveTime, Timelike};

use thiserror::Error;
use OfficeHoursError::InvalidTimeSlice;

use crate::error::OfficeHoursError;

mod error;
mod r#macro;

#[derive(Error, Debug)]
pub enum OfficeHoursError<'a> {
#[error("Could not convert `{0:?}` to chrono::NaiveTime")]
InvalidTimeSlice(&'a [u32]),
}

/// Simple enum to store all the valid hours in a day.
pub enum Clock {
TwelveAm = 0,
Expand Down Expand Up @@ -92,9 +97,9 @@ pub enum Clock {
ElevenPm = 23,
}

/// Trait to implement helper functions in the style of [From].
/// Trait to implement helper functions in the style of [`From`].
pub trait FromNaiveTime {
/// Makes a new [NaiveTime out of a **&[u32]** slice.
/// Makes a new [`NaiveTime`] out of a `&[u32]` slice.
///
/// The slice supports a maximum length of 4 parts: `hour`,
/// `minute`, `second`, and `millisecond`.
Expand All @@ -106,7 +111,7 @@ pub trait FromNaiveTime {
///
/// # Errors
///
/// Returns [Err] if an invalid hour, minute, second,
/// Returns [`Err`] if an invalid hour, minute, second,
/// and/or millisecond.
///
/// # Examples
Expand All @@ -128,10 +133,10 @@ pub trait FromNaiveTime {
/// );
/// ```
///
/// See [NaiveTime::from_hms_milli_opt] for further information.
/// See [`NaiveTime::from_hms_milli_opt`] for further information.
fn from_slice_u32(slice: &[u32]) -> Result<NaiveTime, OfficeHoursError>;

/// Makes a new [NaiveTime] out of a [Clock].
/// Makes a new [`NaiveTime`] out of a [`Clock`].
///
/// # Examples
///
Expand All @@ -146,11 +151,11 @@ pub trait FromNaiveTime {
/// ```
fn from_time(time: Clock) -> NaiveTime;

/// Makes a new [NaiveTime] from the hour given as a [u32].
/// Makes a new [`NaiveTime`] from the hour given as a [`u32`].
///
/// # Errors
///
/// Returns [None] when given an invalid hour.
/// Returns [`None`] when given an invalid hour.
///
/// # Examples
///
Expand All @@ -168,7 +173,7 @@ pub trait FromNaiveTime {

impl FromNaiveTime for NaiveTime {
fn from_slice_u32(slice: &[u32]) -> Result<NaiveTime, OfficeHoursError> {
let time = NaiveTime::from_hms_milli_opt(
let time = Self::from_hms_milli_opt(
*slice.first().unwrap_or(&0),
*slice.get(1).unwrap_or(&0),
*slice.get(2).unwrap_or(&0),
Expand All @@ -182,11 +187,11 @@ impl FromNaiveTime for NaiveTime {
// -- SAFETY --
// Make sure all variants of the [Clock] enum continues
// to hold hours of the day that valid (0 -> 23)
unsafe { NaiveTime::from_time_u32(hour as u32).unwrap_unchecked() }
unsafe { Self::from_time_u32(hour as u32).unwrap_unchecked() }
}

fn from_time_u32(hour: u32) -> Option<NaiveTime> {
NaiveTime::from_hms_opt(hour, 0, 0)
Self::from_hms_opt(hour, 0, 0)
}
}

Expand Down Expand Up @@ -220,7 +225,7 @@ impl Default for OfficeHours {
}

impl OfficeHours {
/// Makes a new [OfficeHours] from the starting and finishing time.
/// Makes a new [`OfficeHours`] from the starting and finishing time.
///
/// # Examples
///
Expand All @@ -229,6 +234,7 @@ impl OfficeHours {
/// let morning = OfficeHours::new(Clock::NineAm, Clock::TwelvePm);
/// let afternoon = OfficeHours::new(Clock::TwelvePm, Clock::FivePm);
/// ```
#[must_use]
pub fn new(start: Clock, finish: Clock) -> Self {
Self {
start: NaiveTime::from_time(start),
Expand All @@ -238,7 +244,7 @@ impl OfficeHours {

/// Returns an iterator over the office hours.
///
/// The iterator yields all [NaiveTime]'s from
/// The iterator yields all [`NaiveTime`] instances from
/// the starting hour to finishing hour.
///
/// # Examples
Expand All @@ -251,14 +257,15 @@ impl OfficeHours {
/// println!("Hours: {:?}", time);
/// }
/// ```
pub fn iter(&self) -> OfficeHoursIter {
#[must_use]
pub const fn iter(&self) -> OfficeHoursIter {
OfficeHoursIter {
start: self.start,
finish: self.finish,
}
}

/// Collect the contents of [Self::hours_iter] into a [Vec<u32>].
/// Collect the contents of [`Self::hours_iter`] into a [`Vec<u32>`].
///
/// # Examples
///
Expand All @@ -270,6 +277,7 @@ impl OfficeHours {
/// let night_shift = OfficeHours::new(Clock::TenPm, Clock::FiveAm);
/// assert_eq!(night_shift.hours(), vec![22, 23, 0, 1, 2, 3, 4]);
/// ```
#[must_use]
pub fn hours(&self) -> Vec<u32> {
self.hours_iter().collect()
}
Expand All @@ -278,7 +286,7 @@ impl OfficeHours {
///
/// The iterator yields all hours from
/// the starting hour to finishing hour in its
/// raw [u32] form.
/// raw [`u32`] form.
///
/// # Examples
///
Expand Down Expand Up @@ -308,11 +316,13 @@ impl OfficeHours {
/// println!("Phew, still on break!")
/// }
/// ```
#[must_use]
pub fn now(&self) -> bool {
OfficeHours::now_from_time(&self.start, &self.finish, &Local::now().time())
Self::now_from_time(&self.start, &self.finish, &Local::now().time())
}

#[doc(hidden)]
#[must_use]
pub fn now_from_time(start: &NaiveTime, finish: &NaiveTime, now: &NaiveTime) -> bool {
match start.cmp(finish) {
Ordering::Equal => start == now,
Expand Down Expand Up @@ -343,14 +353,14 @@ impl<'a> TryFrom<(&'a [u32], &'a [u32])> for OfficeHours {
/// ```
fn try_from(office_hours: (&'a [u32], &'a [u32])) -> Result<Self, Self::Error> {
let (start, finish) = office_hours;
Ok(OfficeHours {
Ok(Self {
start: NaiveTime::from_slice_u32(start)?,
finish: NaiveTime::from_slice_u32(finish)?,
})
}
}

/// Iterator over [OfficeHours] that returns the hourly [NaiveTime].
/// Iterator over [`OfficeHours`] that returns the hourly [`NaiveTime`].
pub struct OfficeHoursIter {
start: NaiveTime,
finish: NaiveTime,
Expand All @@ -375,13 +385,10 @@ impl Iterator for OfficeHoursIter {
}
}

impl IntoIterator for OfficeHours {
impl IntoIterator for &OfficeHours {
type Item = NaiveTime;
type IntoIter = OfficeHoursIter;
fn into_iter(self) -> OfficeHoursIter {
OfficeHoursIter {
start: self.start,
finish: self.finish,
}
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
4 changes: 2 additions & 2 deletions src/macro.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// Creates an [OfficeHours] that takes in a list of statements to execute
/// Creates an [`OfficeHours`] that takes in a list of statements to execute
/// when the current time is between office hours.
///
/// - Only execute code between the default hours of 9am and 5pm.
Expand All @@ -16,7 +16,7 @@
/// println!("Between 5pm and 10pm")
/// });
/// ```
/// [OfficeHours]: crate::OfficeHours
/// [`OfficeHours`]: crate::OfficeHours
#[macro_export]
macro_rules! office_hours {
({ $($code:stmt)* }) => {{
Expand Down