diff --git a/crates/ruma-serde/CHANGELOG.md b/crates/ruma-serde/CHANGELOG.md index 9fea6db3..d6f1077d 100644 --- a/crates/ruma-serde/CHANGELOG.md +++ b/crates/ruma-serde/CHANGELOG.md @@ -3,6 +3,7 @@ Breaking changes: * Remove the `empty` module from the public API +* Remove the `time` module Improvements: diff --git a/crates/ruma-serde/src/lib.rs b/crates/ruma-serde/src/lib.rs index a66fa58d..c933523b 100644 --- a/crates/ruma-serde/src/lib.rs +++ b/crates/ruma-serde/src/lib.rs @@ -15,7 +15,6 @@ mod raw; pub mod single_element_seq; mod strings; pub mod test; -pub mod time; pub mod urlencoded; pub use buf::{json_to_buf, slice_to_buf}; diff --git a/crates/ruma-serde/src/time.rs b/crates/ruma-serde/src/time.rs deleted file mode 100644 index 762608bb..00000000 --- a/crates/ruma-serde/src/time.rs +++ /dev/null @@ -1,6 +0,0 @@ -//! De-/serialization functions for `std::time::SystemTime` objects - -pub mod ms_since_unix_epoch; -pub mod opt_ms_since_unix_epoch; -pub mod opt_s_since_unix_epoch; -pub mod s_since_unix_epoch; diff --git a/crates/ruma-serde/src/time/ms_since_unix_epoch.rs b/crates/ruma-serde/src/time/ms_since_unix_epoch.rs deleted file mode 100644 index 70532343..00000000 --- a/crates/ruma-serde/src/time/ms_since_unix_epoch.rs +++ /dev/null @@ -1,87 +0,0 @@ -//! De-/serialization functions for `std::time::SystemTime` objects represented as milliseconds -//! since the UNIX epoch. Delegates to `js_int::UInt` to ensure integer size is within bounds. - -use std::{ - convert::TryInto, - time::{Duration, SystemTime, UNIX_EPOCH}, -}; - -use js_int::UInt; -use serde::{ - de::{self, Deserialize, Deserializer}, - ser::{self, Serialize, Serializer}, -}; - -/// Serialize a SystemTime. -/// -/// Will fail if integer is greater than the maximum integer that can be unambiguously represented -/// by an f64. -pub fn serialize(time: &SystemTime, serializer: S) -> Result -where - S: Serializer, -{ - let time_since_epoch = time.duration_since(UNIX_EPOCH).map_err(ser::Error::custom)?; - let uint: UInt = time_since_epoch.as_millis().try_into().map_err(ser::Error::custom)?; - - uint.serialize(serializer) -} - -/// Deserializes a SystemTime. -/// -/// Will fail if integer is greater than the maximum integer that can be unambiguously represented -/// by an f64. -pub fn deserialize<'de, D>(deserializer: D) -> Result -where - D: Deserializer<'de>, -{ - let millis = UInt::deserialize(deserializer)?; - UNIX_EPOCH - .checked_add(Duration::from_millis(millis.into())) - .ok_or_else(|| de::Error::custom("input too large for SystemTime")) -} - -#[cfg(test)] -mod tests { - use std::time::{Duration, SystemTime, UNIX_EPOCH}; - - use matches::assert_matches; - use serde::{Deserialize, Serialize}; - use serde_json::json; - - #[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] - struct SystemTimeTest { - #[serde(with = "super")] - timestamp: SystemTime, - } - - #[test] - fn deserialize() { - let json = json!({ "timestamp": 3000 }); - - assert_matches!( - serde_json::from_value::(json), - Ok(SystemTimeTest { timestamp }) - if timestamp == UNIX_EPOCH + Duration::from_millis(3000) - ); - } - - #[test] - fn issue_446() { - let json = json!({ "timestamp": 15_159_743_990_000_u64 }); - - assert_matches!( - serde_json::from_value::(json), - Ok(SystemTimeTest { timestamp }) - if timestamp == UNIX_EPOCH + Duration::from_millis(15_159_743_990_000) - ); - } - - #[test] - fn serialize() { - let request = SystemTimeTest { timestamp: UNIX_EPOCH + Duration::new(2, 0) }; - assert_matches!( - serde_json::to_value(&request), - Ok(value) if value == json!({ "timestamp": 2000 }) - ); - } -} diff --git a/crates/ruma-serde/src/time/opt_ms_since_unix_epoch.rs b/crates/ruma-serde/src/time/opt_ms_since_unix_epoch.rs deleted file mode 100644 index f7eb7746..00000000 --- a/crates/ruma-serde/src/time/opt_ms_since_unix_epoch.rs +++ /dev/null @@ -1,98 +0,0 @@ -//! De-/serialization functions for `Option` objects represented as -//! milliseconds since the UNIX epoch. Delegates to `js_int::UInt` to ensure integer size is within -//! bounds. - -use std::time::{Duration, SystemTime, UNIX_EPOCH}; - -use js_int::UInt; -use serde::{ - de::{self, Deserialize, Deserializer}, - ser::{Serialize, Serializer}, -}; - -/// Serialize an `Option`. -/// -/// Will fail if integer is greater than the maximum integer that can be unambiguously represented -/// by an f64. -pub fn serialize(opt_time: &Option, serializer: S) -> Result -where - S: Serializer, -{ - match opt_time { - Some(time) => super::ms_since_unix_epoch::serialize(time, serializer), - None => Option::::serialize(&None, serializer), - } -} - -/// Deserializes an `Option`. -/// -/// Will fail if integer is greater than the maximum integer that can be unambiguously represented -/// by an f64. -pub fn deserialize<'de, D>(deserializer: D) -> Result, D::Error> -where - D: Deserializer<'de>, -{ - Option::::deserialize(deserializer)? - .map(|millis| { - UNIX_EPOCH - .checked_add(Duration::from_millis(millis.into())) - .ok_or_else(|| de::Error::custom("input too large for SystemTime")) - }) - .transpose() -} - -#[cfg(test)] -mod tests { - use std::time::{Duration, SystemTime, UNIX_EPOCH}; - - use serde::{Deserialize, Serialize}; - use serde_json::json; - - #[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] - struct SystemTimeTest { - #[serde(with = "super", default, skip_serializing_if = "Option::is_none")] - timestamp: Option, - } - - #[test] - fn deserialize_some() { - let json = json!({ "timestamp": 3000 }); - - assert_eq!( - serde_json::from_value::(json).unwrap(), - SystemTimeTest { timestamp: Some(UNIX_EPOCH + Duration::from_millis(3000)) }, - ); - } - - #[test] - fn deserialize_none_by_absence() { - let json = json!({}); - - assert_eq!( - serde_json::from_value::(json).unwrap(), - SystemTimeTest { timestamp: None }, - ); - } - - #[test] - fn deserialize_none_by_null() { - let json = json!({ "timestamp": null }); - - assert_eq!( - serde_json::from_value::(json).unwrap(), - SystemTimeTest { timestamp: None }, - ); - } - - #[test] - fn serialize_some() { - let request = SystemTimeTest { timestamp: Some(UNIX_EPOCH + Duration::new(2, 0)) }; - assert_eq!(serde_json::to_value(&request).unwrap(), json!({ "timestamp": 2000 })); - } - - #[test] - fn serialize_none() { - let request = SystemTimeTest { timestamp: None }; - assert_eq!(serde_json::to_value(&request).unwrap(), json!({})); - } -} diff --git a/crates/ruma-serde/src/time/opt_s_since_unix_epoch.rs b/crates/ruma-serde/src/time/opt_s_since_unix_epoch.rs deleted file mode 100644 index 5a19ba11..00000000 --- a/crates/ruma-serde/src/time/opt_s_since_unix_epoch.rs +++ /dev/null @@ -1,98 +0,0 @@ -//! De-/serialization functions for `Option` objects represented as -//! seconds since the UNIX epoch. Delegates to `js_int::UInt` to ensure integer size is within -//! bounds. - -use std::time::{Duration, SystemTime, UNIX_EPOCH}; - -use js_int::UInt; -use serde::{ - de::{self, Deserialize, Deserializer}, - ser::{Serialize, Serializer}, -}; - -/// Serialize an `Option`. -/// -/// Will fail if integer is greater than the maximum integer that can be unambiguously represented -/// by an f64. -pub fn serialize(opt_time: &Option, serializer: S) -> Result -where - S: Serializer, -{ - match opt_time { - Some(time) => super::s_since_unix_epoch::serialize(time, serializer), - None => Option::::serialize(&None, serializer), - } -} - -/// Deserializes an `Option`. -/// -/// Will fail if integer is greater than the maximum integer that can be unambiguously represented -/// by an f64. -pub fn deserialize<'de, D>(deserializer: D) -> Result, D::Error> -where - D: Deserializer<'de>, -{ - Option::::deserialize(deserializer)? - .map(|secs| { - UNIX_EPOCH - .checked_add(Duration::from_secs(secs.into())) - .ok_or_else(|| de::Error::custom("input too large for SystemTime")) - }) - .transpose() -} - -#[cfg(test)] -mod tests { - use std::time::{Duration, SystemTime, UNIX_EPOCH}; - - use serde::{Deserialize, Serialize}; - use serde_json::json; - - #[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] - struct SystemTimeTest { - #[serde(with = "super", default, skip_serializing_if = "Option::is_none")] - timestamp: Option, - } - - #[test] - fn deserialize_some() { - let json = json!({ "timestamp": 3000 }); - - assert_eq!( - serde_json::from_value::(json).unwrap(), - SystemTimeTest { timestamp: Some(UNIX_EPOCH + Duration::from_secs(3000)) }, - ); - } - - #[test] - fn deserialize_none_by_absence() { - let json = json!({}); - - assert_eq!( - serde_json::from_value::(json).unwrap(), - SystemTimeTest { timestamp: None }, - ); - } - - #[test] - fn deserialize_none_by_null() { - let json = json!({ "timestamp": null }); - - assert_eq!( - serde_json::from_value::(json).unwrap(), - SystemTimeTest { timestamp: None }, - ); - } - - #[test] - fn serialize_some() { - let request = SystemTimeTest { timestamp: Some(UNIX_EPOCH + Duration::new(2000, 0)) }; - assert_eq!(serde_json::to_value(&request).unwrap(), json!({ "timestamp": 2000 })); - } - - #[test] - fn serialize_none() { - let request = SystemTimeTest { timestamp: None }; - assert_eq!(serde_json::to_value(&request).unwrap(), json!({})); - } -} diff --git a/crates/ruma-serde/src/time/s_since_unix_epoch.rs b/crates/ruma-serde/src/time/s_since_unix_epoch.rs deleted file mode 100644 index 103b7a3f..00000000 --- a/crates/ruma-serde/src/time/s_since_unix_epoch.rs +++ /dev/null @@ -1,71 +0,0 @@ -//! De-/serialization functions for `std::time::SystemTime` objects represented as seconds since -//! the UNIX epoch. Delegates to `js_int::UInt` to ensure integer size is within bounds. - -use std::{ - convert::TryInto, - time::{Duration, SystemTime, UNIX_EPOCH}, -}; - -use js_int::UInt; -use serde::{ - de::{self, Deserialize, Deserializer}, - ser::{self, Serialize, Serializer}, -}; - -/// Serialize a SystemTime. -/// -/// Will fail if integer is greater than the maximum integer that can be unambiguously represented -/// by an f64. -pub fn serialize(time: &SystemTime, serializer: S) -> Result -where - S: Serializer, -{ - let time_since_epoch = time.duration_since(UNIX_EPOCH).map_err(ser::Error::custom)?; - let uint: UInt = time_since_epoch.as_secs().try_into().map_err(ser::Error::custom)?; - - uint.serialize(serializer) -} - -/// Deserializes a SystemTime. -/// -/// Will fail if integer is greater than the maximum integer that can be unambiguously represented -/// by an f64. -pub fn deserialize<'de, D>(deserializer: D) -> Result -where - D: Deserializer<'de>, -{ - let secs = UInt::deserialize(deserializer)?; - UNIX_EPOCH - .checked_add(Duration::from_secs(secs.into())) - .ok_or_else(|| de::Error::custom("input too large for SystemTime")) -} - -#[cfg(test)] -mod tests { - use std::time::{Duration, SystemTime, UNIX_EPOCH}; - - use serde::{Deserialize, Serialize}; - use serde_json::json; - - #[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] - struct SystemTimeTest { - #[serde(with = "super")] - timestamp: SystemTime, - } - - #[test] - fn deserialize() { - let json = json!({ "timestamp": 3000 }); - - assert_eq!( - serde_json::from_value::(json).unwrap(), - SystemTimeTest { timestamp: UNIX_EPOCH + Duration::from_secs(3000) }, - ); - } - - #[test] - fn serialize() { - let request = SystemTimeTest { timestamp: UNIX_EPOCH + Duration::new(2000, 0) }; - assert_eq!(serde_json::to_value(&request).unwrap(), json!({ "timestamp": 2000 })); - } -}