diff --git a/src/api/time_repr.rs b/src/api/time_repr.rs index bd511bb..3a9228c 100644 --- a/src/api/time_repr.rs +++ b/src/api/time_repr.rs @@ -2,13 +2,11 @@ use crate::response::ValueResponse; use serde::{Deserialize, Serialize}; use std::marker::PhantomData; use std::time::SystemTime; -use time::formatting::Formattable; use time::macros::format_description; -use time::parsing::Parsable; use time::{format_description, OffsetDateTime}; pub(crate) trait FormatWrapper: std::fmt::Debug { - type Format: 'static + ?Sized + Parsable + Formattable; + type Format: 'static + ?Sized; const FORMAT: &'static Self::Format; } @@ -48,20 +46,31 @@ impl From> for SystemTime { } } -impl Serialize for TimeParam { +#[cfg(feature = "server")] +impl Serialize for TimeParam +where + F::Format: time::formatting::Formattable, +{ fn serialize(&self, serializer: S) -> Result { self.0 - .format(&F::FORMAT) + .format(F::FORMAT) .map_err(serde::ser::Error::custom)? .serialize(serializer) } } -impl<'de, F: FormatWrapper> Deserialize<'de> for TimeParam { +#[cfg(feature = "client")] +impl<'de, F: FormatWrapper> Deserialize<'de> for TimeParam +where + F::Format: time::parsing::Parsable, +{ fn deserialize>(deserializer: D) -> Result { struct Visitor(PhantomData); - impl serde::de::Visitor<'_> for Visitor { + impl serde::de::Visitor<'_> for Visitor + where + F::Format: time::parsing::Parsable, + { type Value = TimeParam; fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { @@ -69,7 +78,7 @@ impl<'de, F: FormatWrapper> Deserialize<'de> for TimeParam { } fn visit_str(self, value: &str) -> Result { - match time::PrimitiveDateTime::parse(value, &F::FORMAT) { + match time::PrimitiveDateTime::parse(value, F::FORMAT) { Ok(value) => Ok(TimeParam(value.assume_utc(), PhantomData)), Err(err) => Err(serde::de::Error::custom(err)), } @@ -81,7 +90,10 @@ impl<'de, F: FormatWrapper> Deserialize<'de> for TimeParam { } #[derive(Debug, Serialize, Deserialize)] -#[serde(bound = "F: FormatWrapper")] +#[serde(bound( + serialize = "TimeParam: Serialize", + deserialize = "TimeParam: Deserialize<'de>" +))] #[serde(transparent)] pub(crate) struct TimeResponse(ValueResponse>);