diff --git a/crates/diesel_crud_trait/src/error.rs b/crates/diesel_crud_trait/src/error.rs index 0933980..157bb05 100644 --- a/crates/diesel_crud_trait/src/error.rs +++ b/crates/diesel_crud_trait/src/error.rs @@ -2,7 +2,7 @@ use diesel::result::Error; use thiserror::Error; /// Error type for CRUD operations -#[derive(Debug, Error)] +#[derive(Debug, PartialEq, Error)] pub enum CrudError { #[error("Resource not found")] NotFound, diff --git a/src/axum/wrappers.rs b/src/axum/wrappers.rs index fa0e3f6..141bfc1 100644 --- a/src/axum/wrappers.rs +++ b/src/axum/wrappers.rs @@ -3,12 +3,14 @@ use derive_more::{Constructor, From}; use into_response_derive::IntoResponse; use serde::Serialize; -#[derive(Debug, Clone, Serialize, From, Constructor)] +#[derive(Debug, Clone, PartialEq, Default, Serialize, From, Constructor)] pub struct Array { pub data: Vec, } -#[derive(Debug, Clone, Copy, Serialize, IntoResponse, From, Constructor)] +#[derive( + Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, IntoResponse, From, Constructor, +)] pub struct Count { pub count: usize, } diff --git a/src/time/common.rs b/src/time/common.rs index 31b50d5..ae20c67 100644 --- a/src/time/common.rs +++ b/src/time/common.rs @@ -1,9 +1,62 @@ use chrono::NaiveDateTime; -use derive_more::Constructor; +use derive_more::{Constructor, From}; -#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Constructor)] +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Constructor, From)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct DateTimeInterval { pub start: NaiveDateTime, pub end: NaiveDateTime, } + +impl DateTimeInterval { + /// Creates a new `DateTimeInterval` from the given `start` and `end` `NaiveDateTime`s. + /// The `start` must be before the `end`. + /// If `start` is equal to or after `end`, this function will return `None`. + /// # Arguments + /// * `start` - The start of the interval. + /// * `end` - The end of the interval. + /// # Returns + /// A new `DateTimeInterval` if `start` is before `end`, otherwise `None`. + /// # Examples + /// ``` + /// use chrono::{NaiveDateTime, Duration, Utc}; + /// use lib::time::common::DateTimeInterval; + /// + /// let start = Utc::now().naive_utc(); + /// let end = start + Duration::days(1); + /// let interval = DateTimeInterval::new_safe(start, end); + /// assert_eq!(interval, Some((start, end).into())); + /// ``` + /// Illegal interval: + /// ``` + /// use chrono::{NaiveDateTime, Utc}; + /// use lib::time::common::DateTimeInterval; + /// let start = Utc::now().naive_utc(); + /// let end = start; + /// let interval = DateTimeInterval::new_safe(start, end); + /// assert_eq!(interval, None); + pub fn new_safe(start: NaiveDateTime, end: NaiveDateTime) -> Option { + if start < end { + Some(Self::new(start, end)) + } else { + None + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use chrono::DateTime; + + #[test] + fn test_new_safe() { + let start = DateTime::from_timestamp(0, 0).unwrap().naive_utc(); + let end = DateTime::from_timestamp(1, 0).unwrap().naive_utc(); + assert_eq!( + DateTimeInterval::new_safe(start, end), + Some(DateTimeInterval::new(start, end)) + ); + assert_eq!(DateTimeInterval::new_safe(end, start), None); + } +}