[fractal] API: Fix booleans being serialized as strings
- From: Daniel Garcia Moreno <danigm src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [fractal] API: Fix booleans being serialized as strings
- Date: Thu, 13 Feb 2020 07:13:59 +0000 (UTC)
commit c632c91e481fbe024905db16dda080db3a771f3e
Author: Alejandro DomÃnguez <adomu net-c com>
Date: Sat Jan 25 19:34:11 2020 +0100
API: Fix booleans being serialized as strings
fractal-matrix-api/src/backend/sync.rs | 6 ++-
.../src/r0/directory/post_public_rooms.rs | 38 +++++++++++++----
fractal-matrix-api/src/r0/sync/sync_events.rs | 48 +++++++++++++++++-----
fractal-matrix-api/src/serde.rs | 12 ------
4 files changed, 71 insertions(+), 33 deletions(-)
---
diff --git a/fractal-matrix-api/src/backend/sync.rs b/fractal-matrix-api/src/backend/sync.rs
index 4c520be7..30fec620 100644
--- a/fractal-matrix-api/src/backend/sync.rs
+++ b/fractal-matrix-api/src/backend/sync.rs
@@ -83,8 +83,10 @@ pub fn sync(
let params = SyncParameters {
access_token,
filter,
- since: since.clone(),
- include_state: IncludeState::Changed(timeout),
+ include_state: IncludeState::Changed {
+ since: since.clone().unwrap_or_default(),
+ timeout,
+ },
set_presence: Default::default(),
};
diff --git a/fractal-matrix-api/src/r0/directory/post_public_rooms.rs
b/fractal-matrix-api/src/r0/directory/post_public_rooms.rs
index 4b6cf5af..2336e23c 100644
--- a/fractal-matrix-api/src/r0/directory/post_public_rooms.rs
+++ b/fractal-matrix-api/src/r0/directory/post_public_rooms.rs
@@ -5,7 +5,8 @@ use reqwest::Error;
use reqwest::Request;
use ruma_identifiers::RoomAliasId;
use ruma_identifiers::RoomId;
-use serde::{Deserialize, Serialize};
+use serde::ser::SerializeMap;
+use serde::{Deserialize, Serialize, Serializer};
use url::Host;
use url::Url;
@@ -23,25 +24,46 @@ pub struct Body {
pub limit: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub since: Option<String>,
- // This field doesn't follow the spec but for some reason
- // it fails with matrix.org if it's not set this way
#[serde(skip_serializing_if = "Option::is_none")]
pub filter: Option<Filter>,
#[serde(flatten)]
pub third_party_networks: ThirdPartyNetworks,
}
-#[derive(Clone, Debug, Serialize)]
-#[serde(tag = "include_all_networks", content = "third_party_instance_id")]
+#[derive(Clone, Debug)]
pub enum ThirdPartyNetworks {
- #[serde(rename = "false")]
None,
- #[serde(rename = "false")]
Only(String),
- #[serde(rename = "true")]
All,
}
+impl Serialize for ThirdPartyNetworks {
+ fn serialize<S>(&self, ser: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ match self {
+ ThirdPartyNetworks::None => {
+ let mut serialized_map = ser.serialize_map(Some(1))?;
+ serialized_map.serialize_entry("include_all_networks", &false)?;
+ serialized_map.end()
+ }
+ ThirdPartyNetworks::Only(network) => {
+ let mut serialized_map = ser.serialize_map(Some(2))?;
+
+ serialized_map.serialize_entry("include_all_networks", &false)?;
+ serialized_map.serialize_entry("third_party_instance_id", &network)?;
+ serialized_map.end()
+ }
+ ThirdPartyNetworks::All => {
+ let mut serialized_map = ser.serialize_map(Some(1))?;
+ serialized_map.serialize_entry("include_all_networks", &true)?;
+ serialized_map.end()
+ }
+ }
+ }
+}
+
impl Default for ThirdPartyNetworks {
fn default() -> Self {
ThirdPartyNetworks::None
diff --git a/fractal-matrix-api/src/r0/sync/sync_events.rs b/fractal-matrix-api/src/r0/sync/sync_events.rs
index 87d5a188..d3c2e6ae 100644
--- a/fractal-matrix-api/src/r0/sync/sync_events.rs
+++ b/fractal-matrix-api/src/r0/sync/sync_events.rs
@@ -1,10 +1,10 @@
use crate::r0::filter::{serialize_filter_as_str, Filter};
use crate::r0::AccessToken;
-use crate::serde::duration_as_millis;
use reqwest::Client;
use reqwest::Error;
use reqwest::Request;
-use serde::{Deserialize, Serialize};
+use serde::ser::SerializeMap;
+use serde::{Deserialize, Serialize, Serializer};
use serde_json::Value as JsonValue;
use std::collections::HashMap;
use std::time::Duration;
@@ -16,27 +16,53 @@ pub struct Parameters<'a> {
#[serde(serialize_with = "serialize_filter_as_str")]
#[serde(skip_serializing_if = "Filter::is_default")]
pub filter: Filter<'a>,
- #[serde(skip_serializing_if = "Option::is_none")]
- pub since: Option<String>,
#[serde(flatten)]
pub include_state: IncludeState,
#[serde(skip_serializing_if = "MarkPresence::is_default")]
pub set_presence: MarkPresence,
}
-#[derive(Clone, Copy, Debug, PartialEq, Serialize)]
-#[serde(tag = "full_state", content = "timeout")]
+#[derive(Clone, Debug, PartialEq)]
pub enum IncludeState {
- #[serde(rename = "false")]
- #[serde(with = "duration_as_millis")]
- Changed(Duration),
- #[serde(rename = "true")]
+ Changed { since: String, timeout: Duration },
Full,
}
+impl Serialize for IncludeState {
+ fn serialize<S>(&self, ser: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ match self {
+ IncludeState::Changed { since, timeout } => {
+ let mut serialized_map;
+
+ if since.is_empty() {
+ serialized_map = ser.serialize_map(Some(2))?;
+ } else {
+ serialized_map = ser.serialize_map(Some(3))?;
+ serialized_map.serialize_entry("since", &since)?;
+ };
+
+ serialized_map.serialize_entry("full_state", &false)?;
+ serialized_map.serialize_entry("timeout", &(timeout.as_millis() as u64))?;
+ serialized_map.end()
+ }
+ IncludeState::Full => {
+ let mut serialized_map = ser.serialize_map(Some(1))?;
+ serialized_map.serialize_entry("full_state", &true)?;
+ serialized_map.end()
+ }
+ }
+ }
+}
+
impl Default for IncludeState {
fn default() -> Self {
- IncludeState::Changed(Default::default())
+ IncludeState::Changed {
+ since: Default::default(),
+ timeout: Default::default(),
+ }
}
}
diff --git a/fractal-matrix-api/src/serde.rs b/fractal-matrix-api/src/serde.rs
index 47a02df0..c9bdec8a 100644
--- a/fractal-matrix-api/src/serde.rs
+++ b/fractal-matrix-api/src/serde.rs
@@ -120,15 +120,3 @@ pub mod host_list {
ser.collect_seq(host_list.iter().map(ToString::to_string))
}
}
-
-pub mod duration_as_millis {
- use serde::Serializer;
- use std::time::Duration;
-
- pub fn serialize<S>(duration: &Duration, ser: S) -> Result<S::Ok, S::Error>
- where
- S: Serializer,
- {
- ser.serialize_u64(duration.as_millis() as u64)
- }
-}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]