Skip to content

Commit

Permalink
Tests pass! Let's only do OffsetDateTime
Browse files Browse the repository at this point in the history
  • Loading branch information
fasterthanlime committed Jul 31, 2024
1 parent 3fdd1a1 commit 9e0c8ed
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 134 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Run tests (including doctests)
run: cargo test --all-features
run: cargo test -F full
142 changes: 9 additions & 133 deletions src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ use std::{
ops::{Deref, DerefMut},
};

/// A newtype wrapper around [time] types, for which [crate::JsonSerialize],
/// [crate::JsonDeserialize], and [crate::ToStatic] can be implemented.
/// A newtype wrapper around [time::OffsetDateTime] types, for which various traits
/// can be implemented:
///
/// It tries to be as transparent as possible, implementing `Deref` and `DerefMut`,
/// - [crate::JsonSerialize] through the `time-serialize` feature
/// - [crate::JsonDeserialize] through the `time-deserialize` feature
/// - [crate::ToStatic] through the `time-types` feature (enabled by either of the above)
///
/// This wrapper tries to be as transparent as possible, implementing `Deref` and `DerefMut`,
/// forwarding `Debug and `Display`, etc.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
Expand Down Expand Up @@ -56,24 +60,6 @@ where
}
}

#[cfg(feature = "time-types")]
impl crate::ToStatic for time::Date {
type Output = Self;

fn to_static(&self) -> Self::Output {
*self
}
}

#[cfg(feature = "time-types")]
impl crate::ToStatic for time::Time {
type Output = Self;

fn to_static(&self) -> Self::Output {
*self
}
}

#[cfg(feature = "time-types")]
impl crate::ToStatic for time::OffsetDateTime {
type Output = Self;
Expand All @@ -83,32 +69,6 @@ impl crate::ToStatic for time::OffsetDateTime {
}
}

#[cfg(feature = "time-serialize")]
impl crate::JsonSerialize for Rfc3339<time::Date> {
fn json_serialize(&self, s: &mut crate::JsonSerializer) {
// Note: we assume there's no need to escape the string
let buf = s.as_mut_vec();
buf.push(b'"');
self.0
.format_into(buf, &time::format_description::well_known::Rfc3339)
.unwrap();
buf.push(b'"');
}
}

#[cfg(feature = "time-serialize")]
impl crate::JsonSerialize for Rfc3339<time::Time> {
fn json_serialize(&self, s: &mut crate::JsonSerializer) {
// Note: we assume there's no need to escape the string
let buf = s.as_mut_vec();
buf.push(b'"');
self.0
.format_into(buf, &time::format_description::well_known::Rfc3339)
.unwrap();
buf.push(b'"');
}
}

#[cfg(feature = "time-serialize")]
impl crate::JsonSerialize for Rfc3339<time::OffsetDateTime> {
fn json_serialize(&self, s: &mut crate::JsonSerializer) {
Expand All @@ -122,44 +82,6 @@ impl crate::JsonSerialize for Rfc3339<time::OffsetDateTime> {
}
}

#[cfg(feature = "time-deserialize")]
impl<'src, 'val> crate::JsonDeserialize<'src, 'val> for Rfc3339<time::Date>
where
'src: 'val,
{
fn json_deserialize(
value: Option<&'val crate::JsonValue<'src>>,
) -> Result<Self, crate::MerdeJsonError> {
use crate::JsonValueExt;
let s = value
.and_then(|v| v.as_cow_str().ok())
.ok_or(crate::MerdeJsonError::MissingValue)?;
Ok(Rfc3339(
time::Date::parse(s, &time::format_description::well_known::Rfc3339)
.map_err(|_| crate::MerdeJsonError::InvalidDateTimeValue)?,
))
}
}

#[cfg(feature = "time-deserialize")]
impl<'src, 'val> crate::JsonDeserialize<'src, 'val> for Rfc3339<time::Time>
where
'src: 'val,
{
fn json_deserialize(
value: Option<&'val crate::JsonValue<'src>>,
) -> Result<Self, crate::MerdeJsonError> {
use crate::JsonValueExt;
let s = value
.and_then(|v| v.as_cow_str().ok())
.ok_or(crate::MerdeJsonError::MissingValue)?;
Ok(Rfc3339(
time::Time::parse(s, &time::format_description::well_known::Rfc3339)
.map_err(|_| crate::MerdeJsonError::InvalidDateTimeValue)?,
))
}
}

#[cfg(feature = "time-deserialize")]
impl<'src, 'val> crate::JsonDeserialize<'src, 'val> for Rfc3339<time::OffsetDateTime>
where
Expand All @@ -179,30 +101,12 @@ where
}
}

#[cfg(test)]
#[cfg(all(test, feature = "time-serialize", feature = "time-deserialize"))]
mod tests {
use crate::{from_str, JsonSerialize, ToRustValue};

use super::*;
use time::macros::{date, datetime, time};

#[test]
fn test_rfc3339_date_roundtrip() {
let original = Rfc3339(date!(2023 - 05 - 15));
let serialized = original.to_json_string();
let deserialized: Rfc3339<time::Date> =
from_str(&serialized).unwrap().to_rust_value().unwrap();
assert_eq!(original, deserialized);
}

#[test]
fn test_rfc3339_time_roundtrip() {
let original = Rfc3339(time!(14:30:00));
let serialized = original.to_json_string();
let deserialized: Rfc3339<time::Time> =
from_str(&serialized).unwrap().to_rust_value().unwrap();
assert_eq!(original, deserialized);
}
use time::macros::datetime;

#[test]
fn test_rfc3339_offset_date_time_roundtrip() {
Expand All @@ -213,41 +117,13 @@ mod tests {
assert_eq!(original, deserialized);
}

#[test]
fn test_rfc3339_date_serialization() {
let date = Rfc3339(date!(2023 - 05 - 15));
let serialized = date.to_json_string();
assert_eq!(serialized, r#""2023-05-15""#);
}

#[test]
fn test_rfc3339_time_serialization() {
let time = Rfc3339(time!(14:30:00));
let serialized = time.to_json_string();
assert_eq!(serialized, r#""14:30:00""#);
}

#[test]
fn test_rfc3339_offset_date_time_serialization() {
let dt = Rfc3339(datetime!(2023-05-15 14:30:00 UTC));
let serialized = dt.to_json_string();
assert_eq!(serialized, r#""2023-05-15T14:30:00Z""#);
}

#[test]
fn test_rfc3339_date_deserialization() {
let json = r#""2023-05-15""#;
let deserialized: Rfc3339<time::Date> = from_str(json).unwrap().to_rust_value().unwrap();
assert_eq!(deserialized, Rfc3339(date!(2023 - 05 - 15)));
}

#[test]
fn test_rfc3339_time_deserialization() {
let json = r#""14:30:00""#;
let deserialized: Rfc3339<time::Time> = from_str(json).unwrap().to_rust_value().unwrap();
assert_eq!(deserialized, Rfc3339(time!(14:30:00)));
}

#[test]
fn test_rfc3339_offset_date_time_deserialization() {
let json = r#""2023-05-15T14:30:00Z""#;
Expand Down

0 comments on commit 9e0c8ed

Please sign in to comment.