Replace all uses of ruma_serde::empty::Empty

… and remove it from the public API.
This commit is contained in:
Jonas Platte 2021-04-05 18:57:42 +02:00
parent ae6183ce6e
commit 466b8679d6
No known key found for this signature in database
GPG Key ID: CC154DE0E30B7C67
7 changed files with 56 additions and 62 deletions

View File

@ -1,9 +1,6 @@
//! Types for the *m.dummy* event.
use std::ops::{Deref, DerefMut};
use ruma_events_macros::BasicEventContent;
use ruma_serde::empty::Empty;
use serde::{Deserialize, Serialize};
use crate::BasicEvent;
@ -22,35 +19,21 @@ pub type DummyEvent = BasicEvent<DummyEventContent>;
/// The payload for `DummyEvent`.
#[derive(Clone, Debug, Deserialize, Serialize, BasicEventContent)]
#[ruma_event(type = "m.dummy")]
pub struct DummyEventContent(pub Empty);
pub struct DummyEventContent {}
/// The to-device version of the payload for the `DummyEvent`.
pub type DummyToDeviceEventContent = DummyEventContent;
impl Deref for DummyEventContent {
type Target = Empty;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for DummyEventContent {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
#[cfg(test)]
mod tests {
use ruma_serde::Raw;
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
use super::{DummyEvent, DummyEventContent, Empty};
use super::{DummyEvent, DummyEventContent};
#[test]
fn serialization() {
let dummy_event = DummyEvent { content: DummyEventContent(Empty) };
let dummy_event = DummyEvent { content: DummyEventContent {} };
let actual = to_json_value(dummy_event).unwrap();
let expected = json!({

View File

@ -25,6 +25,7 @@ serde = { version = "1.0.118", features = ["derive"] }
serde_json = "1.0.61"
[dev-dependencies]
http = "0.2.2"
matches = "0.1.8"
[features]

View File

@ -6,7 +6,8 @@ use js_int::UInt;
use ruma_api::ruma_api;
use ruma_events::{room::member::MemberEventContent, EventType};
use ruma_identifiers::{EventId, RoomId, ServerName, UserId};
use ruma_serde::{empty::Empty, Raw};
use ruma_serde::Raw;
use serde::{Deserialize, Serialize};
ruma_api! {
metadata: {
@ -58,11 +59,14 @@ ruma_api! {
pub depth: UInt,
}
#[derive(Default)]
response: {
/// - no description -
/// An empty object.
///
/// Indicates that the event was accepted into the event graph.
#[ruma_api(body)]
#[serde(with = "crate::serde::v1_pdu")]
pub event: Empty,
pub empty: Empty,
}
}
@ -104,9 +108,12 @@ impl<'a> Request<'a> {
}
impl Response {
/// Creates a new `Response` with an empty event, to indicate the event was accepted into the
/// graph by the receiving homeserver.
pub fn new(event: Empty) -> Self {
Self { event }
/// Creates an empty `Response`.
pub fn new() -> Self {
Self { empty: Empty {} }
}
}
/// An empty object.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct Empty {}

View File

@ -6,7 +6,7 @@ use js_int::UInt;
use ruma_api::ruma_api;
use ruma_events::{room::member::MemberEventContent, EventType};
use ruma_identifiers::{EventId, RoomId, ServerName, UserId};
use ruma_serde::{empty::Empty, Raw};
use ruma_serde::Raw;
ruma_api! {
metadata: {
@ -58,11 +58,8 @@ ruma_api! {
pub depth: UInt,
}
response: {
/// - no description -
#[ruma_api(body)]
pub event: Empty,
}
#[derive(Default)]
response: {}
}
impl<'a> Request<'a> {
@ -103,9 +100,22 @@ impl<'a> Request<'a> {
}
impl Response {
/// Creates a new `Response` with an empty event, to indicate the event was accepted into the
/// graph by the receiving homeserver.
pub fn new(event: Empty) -> Self {
Self { event }
/// Creates an empty `Response`.
pub fn new() -> Self {
Self
}
}
#[cfg(all(test, feature = "server"))]
mod tests {
use std::convert::TryInto;
use super::Response;
#[test]
fn response_body() {
let res: http::Response<Vec<u8>> = Response::new().try_into().unwrap();
assert_eq!(res.body(), b"{}");
}
}

View File

@ -1,3 +1,9 @@
# 0.4.0 (unreleased)
Breaking changes:
* Remove the `empty` module from the public API
# 0.3.1
Bug fixes:

View File

@ -1,45 +1,32 @@
use std::fmt::{self, Formatter};
use std::fmt;
use serde::{
de::{Deserialize, Deserializer, MapAccess, Visitor},
ser::{Serialize, SerializeMap, Serializer},
de::{self, Deserialize},
Serialize,
};
/// A meaningless value that serializes to an empty JSON object.
///
/// This type is used in a few places where the Matrix specification requires an empty JSON object,
/// but it's wasteful to represent it as a `BTreeMap` in Rust code.
#[derive(Clone, Debug, PartialEq)]
pub struct Empty;
impl Serialize for Empty {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_map(Some(0))?.end()
}
}
#[derive(Clone, Debug, Serialize)]
pub struct Empty {}
impl<'de> Deserialize<'de> for Empty {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
D: de::Deserializer<'de>,
{
struct EmptyMapVisitor;
impl<'de> Visitor<'de> for EmptyMapVisitor {
impl<'de> de::Visitor<'de> for EmptyMapVisitor {
type Value = Empty;
fn expecting(&self, f: &mut Formatter<'_>) -> fmt::Result {
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "an object/map")
}
fn visit_map<A>(self, _map: A) -> Result<Self::Value, A::Error>
where
A: MapAccess<'de>,
A: de::MapAccess<'de>,
{
Ok(Empty)
Ok(Empty {})
}
}
@ -69,7 +56,7 @@ pub mod vec_as_map_of_empty {
S: Serializer,
T: Serialize + Eq + Ord,
{
vec.iter().map(|v| (v, Empty)).collect::<BTreeMap<_, _>>().serialize(serializer)
vec.iter().map(|v| (v, Empty {})).collect::<BTreeMap<_, _>>().serialize(serializer)
}
pub fn deserialize<'de, D, T>(deserializer: D) -> Result<Vec<T>, D::Error>

View File

@ -6,7 +6,7 @@ pub mod can_be_empty;
mod canonical_json;
mod cow;
pub mod duration;
pub mod empty;
mod empty;
pub mod json_string;
mod raw;
pub mod single_element_seq;