Replace all uses of ruma_serde::empty::Empty
… and remove it from the public API.
This commit is contained in:
parent
ae6183ce6e
commit
466b8679d6
@ -1,9 +1,6 @@
|
|||||||
//! Types for the *m.dummy* event.
|
//! Types for the *m.dummy* event.
|
||||||
|
|
||||||
use std::ops::{Deref, DerefMut};
|
|
||||||
|
|
||||||
use ruma_events_macros::BasicEventContent;
|
use ruma_events_macros::BasicEventContent;
|
||||||
use ruma_serde::empty::Empty;
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::BasicEvent;
|
use crate::BasicEvent;
|
||||||
@ -22,35 +19,21 @@ pub type DummyEvent = BasicEvent<DummyEventContent>;
|
|||||||
/// The payload for `DummyEvent`.
|
/// The payload for `DummyEvent`.
|
||||||
#[derive(Clone, Debug, Deserialize, Serialize, BasicEventContent)]
|
#[derive(Clone, Debug, Deserialize, Serialize, BasicEventContent)]
|
||||||
#[ruma_event(type = "m.dummy")]
|
#[ruma_event(type = "m.dummy")]
|
||||||
pub struct DummyEventContent(pub Empty);
|
pub struct DummyEventContent {}
|
||||||
|
|
||||||
/// The to-device version of the payload for the `DummyEvent`.
|
/// The to-device version of the payload for the `DummyEvent`.
|
||||||
pub type DummyToDeviceEventContent = DummyEventContent;
|
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use ruma_serde::Raw;
|
use ruma_serde::Raw;
|
||||||
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
|
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]
|
#[test]
|
||||||
fn serialization() {
|
fn serialization() {
|
||||||
let dummy_event = DummyEvent { content: DummyEventContent(Empty) };
|
let dummy_event = DummyEvent { content: DummyEventContent {} };
|
||||||
let actual = to_json_value(dummy_event).unwrap();
|
let actual = to_json_value(dummy_event).unwrap();
|
||||||
|
|
||||||
let expected = json!({
|
let expected = json!({
|
||||||
|
@ -25,6 +25,7 @@ serde = { version = "1.0.118", features = ["derive"] }
|
|||||||
serde_json = "1.0.61"
|
serde_json = "1.0.61"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
http = "0.2.2"
|
||||||
matches = "0.1.8"
|
matches = "0.1.8"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
|
@ -6,7 +6,8 @@ use js_int::UInt;
|
|||||||
use ruma_api::ruma_api;
|
use ruma_api::ruma_api;
|
||||||
use ruma_events::{room::member::MemberEventContent, EventType};
|
use ruma_events::{room::member::MemberEventContent, EventType};
|
||||||
use ruma_identifiers::{EventId, RoomId, ServerName, UserId};
|
use ruma_identifiers::{EventId, RoomId, ServerName, UserId};
|
||||||
use ruma_serde::{empty::Empty, Raw};
|
use ruma_serde::Raw;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
ruma_api! {
|
ruma_api! {
|
||||||
metadata: {
|
metadata: {
|
||||||
@ -58,11 +59,14 @@ ruma_api! {
|
|||||||
pub depth: UInt,
|
pub depth: UInt,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
response: {
|
response: {
|
||||||
/// - no description -
|
/// An empty object.
|
||||||
|
///
|
||||||
|
/// Indicates that the event was accepted into the event graph.
|
||||||
#[ruma_api(body)]
|
#[ruma_api(body)]
|
||||||
#[serde(with = "crate::serde::v1_pdu")]
|
#[serde(with = "crate::serde::v1_pdu")]
|
||||||
pub event: Empty,
|
pub empty: Empty,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,9 +108,12 @@ impl<'a> Request<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Response {
|
impl Response {
|
||||||
/// Creates a new `Response` with an empty event, to indicate the event was accepted into the
|
/// Creates an empty `Response`.
|
||||||
/// graph by the receiving homeserver.
|
pub fn new() -> Self {
|
||||||
pub fn new(event: Empty) -> Self {
|
Self { empty: Empty {} }
|
||||||
Self { event }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// An empty object.
|
||||||
|
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
|
||||||
|
pub struct Empty {}
|
||||||
|
@ -6,7 +6,7 @@ use js_int::UInt;
|
|||||||
use ruma_api::ruma_api;
|
use ruma_api::ruma_api;
|
||||||
use ruma_events::{room::member::MemberEventContent, EventType};
|
use ruma_events::{room::member::MemberEventContent, EventType};
|
||||||
use ruma_identifiers::{EventId, RoomId, ServerName, UserId};
|
use ruma_identifiers::{EventId, RoomId, ServerName, UserId};
|
||||||
use ruma_serde::{empty::Empty, Raw};
|
use ruma_serde::Raw;
|
||||||
|
|
||||||
ruma_api! {
|
ruma_api! {
|
||||||
metadata: {
|
metadata: {
|
||||||
@ -58,11 +58,8 @@ ruma_api! {
|
|||||||
pub depth: UInt,
|
pub depth: UInt,
|
||||||
}
|
}
|
||||||
|
|
||||||
response: {
|
#[derive(Default)]
|
||||||
/// - no description -
|
response: {}
|
||||||
#[ruma_api(body)]
|
|
||||||
pub event: Empty,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Request<'a> {
|
impl<'a> Request<'a> {
|
||||||
@ -103,9 +100,22 @@ impl<'a> Request<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Response {
|
impl Response {
|
||||||
/// Creates a new `Response` with an empty event, to indicate the event was accepted into the
|
/// Creates an empty `Response`.
|
||||||
/// graph by the receiving homeserver.
|
pub fn new() -> Self {
|
||||||
pub fn new(event: Empty) -> Self {
|
Self
|
||||||
Self { event }
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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"{}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,9 @@
|
|||||||
|
# 0.4.0 (unreleased)
|
||||||
|
|
||||||
|
Breaking changes:
|
||||||
|
|
||||||
|
* Remove the `empty` module from the public API
|
||||||
|
|
||||||
# 0.3.1
|
# 0.3.1
|
||||||
|
|
||||||
Bug fixes:
|
Bug fixes:
|
||||||
|
@ -1,45 +1,32 @@
|
|||||||
use std::fmt::{self, Formatter};
|
use std::fmt;
|
||||||
|
|
||||||
use serde::{
|
use serde::{
|
||||||
de::{Deserialize, Deserializer, MapAccess, Visitor},
|
de::{self, Deserialize},
|
||||||
ser::{Serialize, SerializeMap, Serializer},
|
Serialize,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// A meaningless value that serializes to an empty JSON object.
|
#[derive(Clone, Debug, Serialize)]
|
||||||
///
|
pub struct Empty {}
|
||||||
/// 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()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'de> Deserialize<'de> for Empty {
|
impl<'de> Deserialize<'de> for Empty {
|
||||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||||
where
|
where
|
||||||
D: Deserializer<'de>,
|
D: de::Deserializer<'de>,
|
||||||
{
|
{
|
||||||
struct EmptyMapVisitor;
|
struct EmptyMapVisitor;
|
||||||
|
|
||||||
impl<'de> Visitor<'de> for EmptyMapVisitor {
|
impl<'de> de::Visitor<'de> for EmptyMapVisitor {
|
||||||
type Value = Empty;
|
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")
|
write!(f, "an object/map")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_map<A>(self, _map: A) -> Result<Self::Value, A::Error>
|
fn visit_map<A>(self, _map: A) -> Result<Self::Value, A::Error>
|
||||||
where
|
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,
|
S: Serializer,
|
||||||
T: Serialize + Eq + Ord,
|
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>
|
pub fn deserialize<'de, D, T>(deserializer: D) -> Result<Vec<T>, D::Error>
|
||||||
|
@ -6,7 +6,7 @@ pub mod can_be_empty;
|
|||||||
mod canonical_json;
|
mod canonical_json;
|
||||||
mod cow;
|
mod cow;
|
||||||
pub mod duration;
|
pub mod duration;
|
||||||
pub mod empty;
|
mod empty;
|
||||||
pub mod json_string;
|
pub mod json_string;
|
||||||
mod raw;
|
mod raw;
|
||||||
pub mod single_element_seq;
|
pub mod single_element_seq;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user