client-api: Rename url filtering type & field

This commit is contained in:
Jonas Platte 2020-09-25 14:03:57 +02:00
parent 28f14712c1
commit 327900b411
No known key found for this signature in database
GPG Key ID: CC154DE0E30B7C67
2 changed files with 29 additions and 33 deletions

View File

@ -8,7 +8,8 @@ Bug fixes:
Breaking changes: Breaking changes:
* Update type of `contains_url` in `r0::filter::RoomEventFilter` from `Option<bool>` to `Option<ContainsUrlFilter>`. * Update `contains_url: Option<bool>` in `r0::filter::RoomEventFilter` to
`url_filter: Option<UrlFilter>`.
* Borrow strings in outgoing requests and responses. * Borrow strings in outgoing requests and responses.
* Explicit types may have to be updated from `endpoint::Request` to `endpoint::Request<'_>` on * Explicit types may have to be updated from `endpoint::Request` to `endpoint::Request<'_>` on
clients and `endpoint::IncomingRequest` on servers, the other way around for responses. clients and `endpoint::IncomingRequest` on servers, the other way around for responses.

View File

@ -25,35 +25,36 @@ pub enum EventFormat {
Federation, Federation,
} }
/// Options for the `contains_url` filter. /// Options for filtering based on the presence of a URL.
#[derive(Clone, Copy, Debug, Eq, PartialEq)] #[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ContainsUrlFilter { pub enum UrlFilter {
/// Includes only events with a url key in their content. /// Includes only events with a url key in their content.
EventsWithUrl, EventsWithUrl,
/// Excludes events with a url key in their content. /// Excludes events with a url key in their content.
EventsWithoutUrl, EventsWithoutUrl,
} }
impl Serialize for ContainsUrlFilter { impl Serialize for UrlFilter {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where where
S: Serializer, S: Serializer,
{ {
match *self { match *self {
ContainsUrlFilter::EventsWithUrl => serializer.serialize_bool(true), Self::EventsWithUrl => serializer.serialize_bool(true),
ContainsUrlFilter::EventsWithoutUrl => serializer.serialize_bool(false), Self::EventsWithoutUrl => serializer.serialize_bool(false),
} }
} }
} }
impl<'de> Deserialize<'de> for ContainsUrlFilter { impl<'de> Deserialize<'de> for UrlFilter {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where where
D: Deserializer<'de>, D: Deserializer<'de>,
{ {
Ok(match bool::deserialize(deserializer)? { Ok(match bool::deserialize(deserializer)? {
true => ContainsUrlFilter::EventsWithUrl, true => Self::EventsWithUrl,
false => ContainsUrlFilter::EventsWithoutUrl, false => Self::EventsWithoutUrl,
}) })
} }
} }
@ -110,10 +111,10 @@ pub struct RoomEventFilter<'a> {
/// Controls whether to include events with a URL key in their content. /// Controls whether to include events with a URL key in their content.
/// ///
/// * `None`: No filtering /// * `None`: No filtering
/// * `EventsWithUrl`: Only events with a URL /// * `Some(EventsWithUrl)`: Only events with a URL
/// * `EventsWithoutUrl`: Only events without a URL /// * `Some(EventsWithoutUrl)`: Only events without a URL
#[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "contains_url", skip_serializing_if = "Option::is_none")]
pub contains_url: Option<ContainsUrlFilter>, pub url_filter: Option<UrlFilter>,
/// Options to control lazy-loading of membership events. /// Options to control lazy-loading of membership events.
#[serde(flatten)] #[serde(flatten)]
@ -357,52 +358,46 @@ impl<'de> Deserialize<'de> for LazyLoadOptions {
mod tests { mod tests {
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::{ContainsUrlFilter, LazyLoadOptions}; use super::{LazyLoadOptions, UrlFilter};
#[test] #[test]
fn test_serializing_contains_url_filter_events_with_url() { fn serialize_contains_url_filter_events_with_url() {
let events_with_url = ContainsUrlFilter::EventsWithUrl; let events_with_url = UrlFilter::EventsWithUrl;
assert_eq!(to_json_value(events_with_url).unwrap(), json!(true)) assert_eq!(to_json_value(events_with_url).unwrap(), json!(true))
} }
#[test] #[test]
fn test_serializing_contains_url_filter_events_without_url() { fn serialize_contains_url_filter_events_without_url() {
let events_without_url = ContainsUrlFilter::EventsWithoutUrl; let events_without_url = UrlFilter::EventsWithoutUrl;
assert_eq!(to_json_value(events_without_url).unwrap(), json!(false)) assert_eq!(to_json_value(events_without_url).unwrap(), json!(false))
} }
#[test] #[test]
fn test_deserializing_contains_url_true() { fn deserialize_contains_url_true() {
let json = json!(true); let json = json!(true);
assert_eq!( assert_eq!(from_json_value::<UrlFilter>(json).unwrap(), UrlFilter::EventsWithUrl);
from_json_value::<ContainsUrlFilter>(json).unwrap(),
ContainsUrlFilter::EventsWithUrl
);
} }
#[test] #[test]
fn test_deserializing_contains_url_false() { fn deserialize_contains_url_false() {
let json = json!(false); let json = json!(false);
assert_eq!( assert_eq!(from_json_value::<UrlFilter>(json).unwrap(), UrlFilter::EventsWithoutUrl);
from_json_value::<ContainsUrlFilter>(json).unwrap(),
ContainsUrlFilter::EventsWithoutUrl
);
} }
#[test] #[test]
fn test_serializing_disabled_lazy_load() { fn serialize_disabled_lazy_load() {
let lazy_load_options = LazyLoadOptions::Disabled; let lazy_load_options = LazyLoadOptions::Disabled;
assert_eq!(to_json_value(lazy_load_options).unwrap(), json!({})); assert_eq!(to_json_value(lazy_load_options).unwrap(), json!({}));
} }
#[test] #[test]
fn test_serializing_lazy_load_no_redundant() { fn serialize_lazy_load_no_redundant() {
let lazy_load_options = LazyLoadOptions::Enabled { include_redundant_members: false }; let lazy_load_options = LazyLoadOptions::Enabled { include_redundant_members: false };
assert_eq!(to_json_value(lazy_load_options).unwrap(), json!({ "lazy_load_members": true })); assert_eq!(to_json_value(lazy_load_options).unwrap(), json!({ "lazy_load_members": true }));
} }
#[test] #[test]
fn test_serializing_lazy_load_with_redundant() { fn serialize_lazy_load_with_redundant() {
let lazy_load_options = LazyLoadOptions::Enabled { include_redundant_members: true }; let lazy_load_options = LazyLoadOptions::Enabled { include_redundant_members: true };
assert_eq!( assert_eq!(
to_json_value(lazy_load_options).unwrap(), to_json_value(lazy_load_options).unwrap(),
@ -411,13 +406,13 @@ mod tests {
} }
#[test] #[test]
fn test_deserializing_no_lazy_load() { fn deserialize_no_lazy_load() {
let json = json!({}); let json = json!({});
assert_eq!(from_json_value::<LazyLoadOptions>(json).unwrap(), LazyLoadOptions::Disabled,); assert_eq!(from_json_value::<LazyLoadOptions>(json).unwrap(), LazyLoadOptions::Disabled,);
} }
#[test] #[test]
fn test_deserializing_ignore_redundant_members_when_no_lazy_load() { fn deserialize_ignore_redundant_members_when_no_lazy_load() {
let json = json!({ "include_redundant_members": true }); let json = json!({ "include_redundant_members": true });
assert_eq!(from_json_value::<LazyLoadOptions>(json).unwrap(), LazyLoadOptions::Disabled,); assert_eq!(from_json_value::<LazyLoadOptions>(json).unwrap(), LazyLoadOptions::Disabled,);
} }