Use as_variant crate for shorter code

This commit is contained in:
Jonas Platte 2023-08-09 13:46:50 +02:00
parent ed03c0d2fa
commit 399adc911f
11 changed files with 37 additions and 104 deletions

View File

@ -8,6 +8,7 @@ resolver = "2"
rust-version = "1.70"
[workspace.dependencies]
as_variant = "1.2.0"
assert_matches2 = "0.1.0"
assign = "1.1.1"
base64 = "0.21.0"

View File

@ -52,6 +52,7 @@ compat-null = []
compat-optional = []
[dependencies]
as_variant = { workspace = true }
base64 = { workspace = true }
bytes = "1.0.1"
form_urlencoded = "1.0.0"

View File

@ -14,6 +14,7 @@
use std::{convert::TryInto as _, error::Error as StdError};
use as_variant::as_variant;
use bytes::BufMut;
use serde::{Deserialize, Serialize};
@ -328,20 +329,14 @@ impl<'a> SendAccessToken<'a> {
///
/// Returns `Some(_)` if `self` contains an access token.
pub fn get_required_for_endpoint(self) -> Option<&'a str> {
match self {
Self::IfRequired(tok) | Self::Always(tok) => Some(tok),
Self::None => None,
}
as_variant!(self, Self::IfRequired | Self::Always)
}
/// Get the access token for an endpoint that should not require one.
///
/// Returns `Some(_)` only if `self` is `SendAccessToken::Always(_)`.
pub fn get_not_required_for_endpoint(self) -> Option<&'a str> {
match self {
Self::Always(tok) => Some(tok),
Self::IfRequired(_) | Self::None => None,
}
as_variant!(self, Self::Always)
}
}

View File

@ -1,5 +1,6 @@
use std::{collections::BTreeMap, fmt};
use as_variant::as_variant;
use js_int::{Int, UInt};
use serde::{de::Deserializer, ser::Serializer, Deserialize, Serialize};
use serde_json::{to_string as to_json_string, Value as JsonValue};
@ -76,58 +77,37 @@ pub enum CanonicalJsonValue {
impl CanonicalJsonValue {
/// If the `CanonicalJsonValue` is a `Bool`, return the inner value.
pub fn as_bool(&self) -> Option<bool> {
match self {
Self::Bool(b) => Some(*b),
_ => None,
}
as_variant!(self, Self::Bool).copied()
}
/// If the `CanonicalJsonValue` is an `Integer`, return the inner value.
pub fn as_integer(&self) -> Option<Int> {
match self {
Self::Integer(i) => Some(*i),
_ => None,
}
as_variant!(self, Self::Integer).copied()
}
/// If the `CanonicalJsonValue` is a `String`, return a reference to the inner value.
pub fn as_str(&self) -> Option<&str> {
match self {
Self::String(s) => Some(s),
_ => None,
}
as_variant!(self, Self::String)
}
/// If the `CanonicalJsonValue` is an `Array`, return a reference to the inner value.
pub fn as_array(&self) -> Option<&[CanonicalJsonValue]> {
match self {
Self::Array(a) => Some(a),
_ => None,
}
as_variant!(self, Self::Array)
}
/// If the `CanonicalJsonValue` is an `Object`, return a reference to the inner value.
pub fn as_object(&self) -> Option<&CanonicalJsonObject> {
match self {
Self::Object(o) => Some(o),
_ => None,
}
as_variant!(self, Self::Object)
}
/// If the `CanonicalJsonValue` is an `Array`, return a mutable reference to the inner value.
pub fn as_array_mut(&mut self) -> Option<&mut Vec<CanonicalJsonValue>> {
match self {
Self::Array(a) => Some(a),
_ => None,
}
as_variant!(self, Self::Array)
}
/// If the `CanonicalJsonValue` is an `Object`, return a mutable reference to the inner value.
pub fn as_object_mut(&mut self) -> Option<&mut CanonicalJsonObject> {
match self {
Self::Object(o) => Some(o),
_ => None,
}
as_variant!(self, Self::Object)
}
/// Returns `true` if the `CanonicalJsonValue` is a `Bool`.

View File

@ -1,5 +1,6 @@
use std::collections::BTreeMap;
use as_variant::as_variant;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_json::value::{RawValue as RawJsonValue, Value as JsonValue};
@ -36,10 +37,7 @@ impl Action {
/// The sound that should be played with this action, if any.
pub fn sound(&self) -> Option<&str> {
match self {
Action::SetTweak(Tweak::Sound(sound)) => Some(sound),
_ => None,
}
as_variant!(self, Action::SetTweak(Tweak::Sound(sound)) => sound)
}
}

View File

@ -1,5 +1,6 @@
use std::collections::BTreeMap;
use as_variant::as_variant;
use js_int::Int;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_json::{to_value as to_json_value, value::Value as JsonValue};
@ -122,26 +123,17 @@ impl ScalarJsonValue {
/// If the `ScalarJsonValue` is a `Bool`, return the inner value.
pub fn as_bool(&self) -> Option<bool> {
match self {
Self::Bool(b) => Some(*b),
_ => None,
}
as_variant!(self, Self::Bool).copied()
}
/// If the `ScalarJsonValue` is an `Integer`, return the inner value.
pub fn as_integer(&self) -> Option<Int> {
match self {
Self::Integer(i) => Some(*i),
_ => None,
}
as_variant!(self, Self::Integer).copied()
}
/// If the `ScalarJsonValue` is a `String`, return a reference to the inner value.
pub fn as_str(&self) -> Option<&str> {
match self {
Self::String(s) => Some(s),
_ => None,
}
as_variant!(self, Self::String)
}
}
@ -249,34 +241,22 @@ impl FlattenedJsonValue {
/// If the `FlattenedJsonValue` is a `Bool`, return the inner value.
pub fn as_bool(&self) -> Option<bool> {
match self {
Self::Bool(b) => Some(*b),
_ => None,
}
as_variant!(self, Self::Bool).copied()
}
/// If the `FlattenedJsonValue` is an `Integer`, return the inner value.
pub fn as_integer(&self) -> Option<Int> {
match self {
Self::Integer(i) => Some(*i),
_ => None,
}
as_variant!(self, Self::Integer).copied()
}
/// If the `FlattenedJsonValue` is a `String`, return a reference to the inner value.
pub fn as_str(&self) -> Option<&str> {
match self {
Self::String(s) => Some(s),
_ => None,
}
as_variant!(self, Self::String)
}
/// If the `FlattenedJsonValue` is an `Array`, return a reference to the inner value.
pub fn as_array(&self) -> Option<&[ScalarJsonValue]> {
match self {
Self::Array(a) => Some(a),
_ => None,
}
as_variant!(self, Self::Array)
}
}

View File

@ -48,6 +48,7 @@ compat-optional = []
compat-tag-info = []
[dependencies]
as_variant = { workspace = true }
indexmap = { version = "2.0.0", features = ["serde"] }
js_int = { workspace = true, features = ["serde"] }
js_option = "0.1.0"

View File

@ -1,5 +1,6 @@
#![allow(clippy::exhaustive_structs)]
use as_variant::as_variant;
use ruma_common::{
serde::{from_raw_json_value, Raw},
EventId, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedRoomId, OwnedUserId, RoomId,
@ -663,10 +664,7 @@ impl_possibly_redacted_event!(
/// Get the inner `OriginalMessageLikeEvent` if this is an unredacted event.
pub fn as_original(&self) -> Option<&OriginalMessageLikeEvent<C>> {
match self {
Self::Original(v) => Some(v),
_ => None,
}
as_variant!(self, Self::Original)
}
}
);
@ -677,10 +675,7 @@ impl_possibly_redacted_event!(
) {
/// Get the inner `OriginalSyncMessageLikeEvent` if this is an unredacted event.
pub fn as_original(&self) -> Option<&OriginalSyncMessageLikeEvent<C>> {
match self {
Self::Original(v) => Some(v),
_ => None,
}
as_variant!(self, Self::Original)
}
/// Convert this sync event into a full event (one with a `room_id` field).
@ -716,10 +711,7 @@ impl_possibly_redacted_event!(
/// Get the inner `OriginalStateEvent` if this is an unredacted event.
pub fn as_original(&self) -> Option<&OriginalStateEvent<C>> {
match self {
Self::Original(v) => Some(v),
_ => None,
}
as_variant!(self, Self::Original)
}
}
);
@ -739,10 +731,7 @@ impl_possibly_redacted_event!(
/// Get the inner `OriginalSyncStateEvent` if this is an unredacted event.
pub fn as_original(&self) -> Option<&OriginalSyncStateEvent<C>> {
match self {
Self::Original(v) => Some(v),
_ => None,
}
as_variant!(self, Self::Original)
}
/// Convert this sync event into a full event (one with a `room_id` field).

View File

@ -2,6 +2,7 @@
//!
//! [`m.room.redaction`]: https://spec.matrix.org/latest/client-server-api/#mroomredaction
use as_variant::as_variant;
use js_int::Int;
#[cfg(feature = "canonical-json")]
use ruma_common::canonical_json::RedactionEvent;
@ -313,10 +314,7 @@ impl RoomRedactionEvent {
/// Get the inner `RoomRedactionEvent` if this is an unredacted event.
pub fn as_original(&self) -> Option<&OriginalRoomRedactionEvent> {
match self {
Self::Original(v) => Some(v),
_ => None,
}
as_variant!(self, Self::Original)
}
}
@ -368,10 +366,7 @@ impl SyncRoomRedactionEvent {
/// Get the inner `SyncRoomRedactionEvent` if this is an unredacted event.
pub fn as_original(&self) -> Option<&OriginalSyncRoomRedactionEvent> {
match self {
Self::Original(v) => Some(v),
_ => None,
}
as_variant!(self, Self::Original)
}
/// Convert this sync event into a full event (one with a `room_id` field).

View File

@ -15,6 +15,7 @@ all-features = true
rustdoc-args = ["--cfg", "docsrs"]
[dependencies]
as_variant = { workspace = true }
html5ever = "0.26.0"
phf = { version = "0.11.1", features = ["macros"] }
tracing = { workspace = true, features = ["attributes"] }

View File

@ -1,5 +1,6 @@
use std::{collections::BTreeSet, fmt, io};
use as_variant::as_variant;
use html5ever::{
local_name, namespace_url, ns, parse_fragment,
serialize::{serialize, Serialize, SerializeOpts, Serializer, TraversalScope},
@ -290,26 +291,17 @@ impl Node {
/// Returns the `ElementData` of this `Node` if it is a `NodeData::Element`.
pub fn as_element(&self) -> Option<&ElementData> {
match &self.data {
NodeData::Element(data) => Some(data),
_ => None,
}
as_variant!(&self.data, NodeData::Element)
}
/// Returns the mutable `ElementData` of this `Node` if it is a `NodeData::Element`.
pub fn as_element_mut(&mut self) -> Option<&mut ElementData> {
match &mut self.data {
NodeData::Element(data) => Some(data),
_ => None,
}
as_variant!(&mut self.data, NodeData::Element)
}
/// Returns the mutable text content of this `Node`, if it is a `NodeData::Text`.
pub fn as_text_mut(&mut self) -> Option<&mut StrTendril> {
match &mut self.data {
NodeData::Text(data) => Some(data),
_ => None,
}
as_variant!(&mut self.data, NodeData::Text)
}
}