apply new rustfmt.toml changes, fix some clippy lints

Signed-off-by: strawberry <strawberry@puppygock.gay>
This commit is contained in:
strawberry 2024-12-15 00:05:47 -05:00
parent 0317cc8cc5
commit 77e0b76408
No known key found for this signature in database
296 changed files with 7147 additions and 4300 deletions

View file

@ -35,7 +35,8 @@ impl Builder {
{
Self {
event_type: content.event_type().into(),
content: to_raw_value(content).expect("Builder failed to serialize state event content to RawValue"),
content: to_raw_value(content)
.expect("Builder failed to serialize state event content to RawValue"),
state_key: Some(state_key),
..Self::default()
}
@ -47,7 +48,8 @@ impl Builder {
{
Self {
event_type: content.event_type().into(),
content: to_raw_value(content).expect("Builder failed to serialize timeline event content to RawValue"),
content: to_raw_value(content)
.expect("Builder failed to serialize timeline event content to RawValue"),
..Self::default()
}
}

View file

@ -21,8 +21,8 @@ impl Count {
#[must_use]
pub fn from_signed(signed: i64) -> Self {
match signed {
i64::MIN..=0 => Self::Backfilled(signed),
_ => Self::Normal(signed as u64),
| i64::MIN..=0 => Self::Backfilled(signed),
| _ => Self::Normal(signed as u64),
}
}
@ -31,8 +31,8 @@ impl Count {
pub fn into_unsigned(self) -> u64 {
self.debug_assert_valid();
match self {
Self::Normal(i) => i,
Self::Backfilled(i) => i as u64,
| Self::Normal(i) => i,
| Self::Backfilled(i) => i as u64,
}
}
@ -41,8 +41,8 @@ impl Count {
pub fn into_signed(self) -> i64 {
self.debug_assert_valid();
match self {
Self::Normal(i) => i as i64,
Self::Backfilled(i) => i,
| Self::Normal(i) => i as i64,
| Self::Backfilled(i) => i,
}
}
@ -51,27 +51,27 @@ impl Count {
pub fn into_normal(self) -> Self {
self.debug_assert_valid();
match self {
Self::Normal(i) => Self::Normal(i),
Self::Backfilled(_) => Self::Normal(0),
| Self::Normal(i) => Self::Normal(i),
| Self::Backfilled(_) => Self::Normal(0),
}
}
#[inline]
pub fn checked_inc(self, dir: Direction) -> Result<Self, Error> {
match dir {
Direction::Forward => self.checked_add(1),
Direction::Backward => self.checked_sub(1),
| Direction::Forward => self.checked_add(1),
| Direction::Backward => self.checked_sub(1),
}
}
#[inline]
pub fn checked_add(self, add: u64) -> Result<Self, Error> {
Ok(match self {
Self::Normal(i) => Self::Normal(
| Self::Normal(i) => Self::Normal(
i.checked_add(add)
.ok_or_else(|| err!(Arithmetic("Count::Normal overflow")))?,
),
Self::Backfilled(i) => Self::Backfilled(
| Self::Backfilled(i) => Self::Backfilled(
i.checked_add(add as i64)
.ok_or_else(|| err!(Arithmetic("Count::Backfilled overflow")))?,
),
@ -81,11 +81,11 @@ impl Count {
#[inline]
pub fn checked_sub(self, sub: u64) -> Result<Self, Error> {
Ok(match self {
Self::Normal(i) => Self::Normal(
| Self::Normal(i) => Self::Normal(
i.checked_sub(sub)
.ok_or_else(|| err!(Arithmetic("Count::Normal underflow")))?,
),
Self::Backfilled(i) => Self::Backfilled(
| Self::Backfilled(i) => Self::Backfilled(
i.checked_sub(sub as i64)
.ok_or_else(|| err!(Arithmetic("Count::Backfilled underflow")))?,
),
@ -96,8 +96,8 @@ impl Count {
#[must_use]
pub fn saturating_inc(self, dir: Direction) -> Self {
match dir {
Direction::Forward => self.saturating_add(1),
Direction::Backward => self.saturating_sub(1),
| Direction::Forward => self.saturating_add(1),
| Direction::Backward => self.saturating_sub(1),
}
}
@ -105,8 +105,8 @@ impl Count {
#[must_use]
pub fn saturating_add(self, add: u64) -> Self {
match self {
Self::Normal(i) => Self::Normal(i.saturating_add(add)),
Self::Backfilled(i) => Self::Backfilled(i.saturating_add(add as i64)),
| Self::Normal(i) => Self::Normal(i.saturating_add(add)),
| Self::Backfilled(i) => Self::Backfilled(i.saturating_add(add as i64)),
}
}
@ -114,8 +114,8 @@ impl Count {
#[must_use]
pub fn saturating_sub(self, sub: u64) -> Self {
match self {
Self::Normal(i) => Self::Normal(i.saturating_sub(sub)),
Self::Backfilled(i) => Self::Backfilled(i.saturating_sub(sub as i64)),
| Self::Normal(i) => Self::Normal(i.saturating_sub(sub)),
| Self::Backfilled(i) => Self::Backfilled(i.saturating_sub(sub as i64)),
}
}
@ -139,8 +139,8 @@ impl Display for Count {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
self.debug_assert_valid();
match self {
Self::Normal(i) => write!(f, "{i}"),
Self::Backfilled(i) => write!(f, "{i}"),
| Self::Normal(i) => write!(f, "{i}"),
| Self::Backfilled(i) => write!(f, "{i}"),
}
}
}

View file

@ -19,13 +19,19 @@ impl Event for Pdu {
fn content(&self) -> &RawJsonValue { &self.content }
fn origin_server_ts(&self) -> MilliSecondsSinceUnixEpoch { MilliSecondsSinceUnixEpoch(self.origin_server_ts) }
fn origin_server_ts(&self) -> MilliSecondsSinceUnixEpoch {
MilliSecondsSinceUnixEpoch(self.origin_server_ts)
}
fn state_key(&self) -> Option<&str> { self.state_key.as_deref() }
fn prev_events(&self) -> impl DoubleEndedIterator<Item = &Self::Id> + Send + '_ { self.prev_events.iter() }
fn prev_events(&self) -> impl DoubleEndedIterator<Item = &Self::Id> + Send + '_ {
self.prev_events.iter()
}
fn auth_events(&self) -> impl DoubleEndedIterator<Item = &Self::Id> + Send + '_ { self.auth_events.iter() }
fn auth_events(&self) -> impl DoubleEndedIterator<Item = &Self::Id> + Send + '_ {
self.auth_events.iter()
}
fn redacts(&self) -> Option<&Self::Id> { self.redacts.as_ref() }
}

View file

@ -8,7 +8,8 @@ use crate::{err, Result};
/// Returns a tuple of the new `EventId` and the PDU as a `BTreeMap<String,
/// CanonicalJsonValue>`.
pub fn gen_event_id_canonical_json(
pdu: &RawJsonValue, room_version_id: &RoomVersionId,
pdu: &RawJsonValue,
room_version_id: &RoomVersionId,
) -> Result<(OwnedEventId, CanonicalJsonObject)> {
let value: CanonicalJsonObject = serde_json::from_str(pdu.get())
.map_err(|e| err!(BadServerResponse(warn!("Error parsing incoming event: {e:?}"))))?;
@ -19,7 +20,10 @@ pub fn gen_event_id_canonical_json(
}
/// Generates a correct eventId for the incoming pdu.
pub fn gen_event_id(value: &CanonicalJsonObject, room_version_id: &RoomVersionId) -> Result<OwnedEventId> {
pub fn gen_event_id(
value: &CanonicalJsonObject,
room_version_id: &RoomVersionId,
) -> Result<OwnedEventId> {
let reference_hash = ruma::signatures::reference_hash(value, room_version_id)?;
let event_id: OwnedEventId = format!("${reference_hash}").try_into()?;

View file

@ -84,7 +84,7 @@ fn matches_url(&self, filter: &RoomEventFilter) -> bool {
.is_some_and(Value::is_string);
match url_filter {
UrlFilter::EventsWithUrl => url,
UrlFilter::EventsWithoutUrl => !url,
| UrlFilter::EventsWithUrl => url,
| UrlFilter::EventsWithoutUrl => !url,
}
}

View file

@ -15,7 +15,8 @@ mod unsigned;
use std::{cmp::Ordering, sync::Arc};
use ruma::{
events::TimelineEventType, CanonicalJsonObject, CanonicalJsonValue, EventId, OwnedRoomId, OwnedUserId, UInt,
events::TimelineEventType, CanonicalJsonObject, CanonicalJsonValue, EventId, OwnedRoomId,
OwnedUserId, UInt,
};
use serde::{Deserialize, Serialize};
use serde_json::value::RawValue as RawJsonValue;

View file

@ -29,10 +29,10 @@ impl RawId {
#[must_use]
pub fn shortroomid(self) -> [u8; INT_LEN] {
match self {
Self::Normal(raw) => raw[0..INT_LEN]
| Self::Normal(raw) => raw[0..INT_LEN]
.try_into()
.expect("normal raw shortroomid array from slice"),
Self::Backfilled(raw) => raw[0..INT_LEN]
| Self::Backfilled(raw) => raw[0..INT_LEN]
.try_into()
.expect("backfilled raw shortroomid array from slice"),
}
@ -42,10 +42,10 @@ impl RawId {
#[must_use]
pub fn shorteventid(self) -> [u8; INT_LEN] {
match self {
Self::Normal(raw) => raw[INT_LEN..INT_LEN * 2]
| Self::Normal(raw) => raw[INT_LEN..INT_LEN * 2]
.try_into()
.expect("normal raw shorteventid array from slice"),
Self::Backfilled(raw) => raw[INT_LEN * 2..INT_LEN * 3]
| Self::Backfilled(raw) => raw[INT_LEN * 2..INT_LEN * 3]
.try_into()
.expect("backfilled raw shorteventid array from slice"),
}
@ -55,8 +55,8 @@ impl RawId {
#[must_use]
pub fn as_bytes(&self) -> &[u8] {
match self {
Self::Normal(ref raw) => raw,
Self::Backfilled(ref raw) => raw,
| Self::Normal(ref raw) => raw,
| Self::Backfilled(ref raw) => raw,
}
}
}
@ -70,17 +70,17 @@ impl From<&[u8]> for RawId {
#[inline]
fn from(id: &[u8]) -> Self {
match id.len() {
Self::NORMAL_LEN => Self::Normal(
| Self::NORMAL_LEN => Self::Normal(
id[0..Self::NORMAL_LEN]
.try_into()
.expect("normal RawId from [u8]"),
),
Self::BACKFILLED_LEN => Self::Backfilled(
| Self::BACKFILLED_LEN => Self::Backfilled(
id[0..Self::BACKFILLED_LEN]
.try_into()
.expect("backfilled RawId from [u8]"),
),
_ => unimplemented!("unrecognized RawId length"),
| _ => unimplemented!("unrecognized RawId length"),
}
}
}
@ -95,11 +95,11 @@ impl From<Id> for RawId {
vec.extend(id.shortroomid.to_be_bytes());
id.shorteventid.debug_assert_valid();
match id.shorteventid {
Count::Normal(shorteventid) => {
| Count::Normal(shorteventid) => {
vec.extend(shorteventid.to_be_bytes());
Self::Normal(vec.as_ref().try_into().expect("RawVec into RawId::Normal"))
},
Count::Backfilled(shorteventid) => {
| Count::Backfilled(shorteventid) => {
vec.extend(0_u64.to_be_bytes());
vec.extend(shorteventid.to_be_bytes());
Self::Backfilled(

View file

@ -22,8 +22,8 @@ struct ExtractRedactedBecause {
pub fn redact(&mut self, room_version_id: &RoomVersionId, reason: &Self) -> Result {
self.unsigned = None;
let mut content =
serde_json::from_str(self.content.get()).map_err(|_| Error::bad_database("PDU in db has invalid content."))?;
let mut content = serde_json::from_str(self.content.get())
.map_err(|_| Error::bad_database("PDU in db has invalid content."))?;
redact_content_in_place(&mut content, room_version_id, self.kind.to_string())
.map_err(|e| Error::Redaction(self.sender.server_name().to_owned(), e))?;
@ -75,7 +75,9 @@ pub fn is_redacted(&self) -> bool {
#[must_use]
pub fn copy_redacts(&self) -> (Option<Arc<EventId>>, Box<RawJsonValue>) {
if self.kind == TimelineEventType::RoomRedaction {
if let Ok(mut content) = serde_json::from_str::<RoomRedactionEventContent>(self.content.get()) {
if let Ok(mut content) =
serde_json::from_str::<RoomRedactionEventContent>(self.content.get())
{
if let Some(redacts) = content.redacts {
return (Some(redacts.into()), self.content.clone());
} else if let Some(redacts) = self.redacts.clone() {

View file

@ -1,8 +1,8 @@
use ruma::{
events::{
room::member::RoomMemberEventContent, space::child::HierarchySpaceChildEvent, AnyEphemeralRoomEvent,
AnyMessageLikeEvent, AnyStateEvent, AnyStrippedStateEvent, AnySyncStateEvent, AnySyncTimelineEvent,
AnyTimelineEvent, StateEvent,
room::member::RoomMemberEventContent, space::child::HierarchySpaceChildEvent,
AnyEphemeralRoomEvent, AnyMessageLikeEvent, AnyStateEvent, AnyStrippedStateEvent,
AnySyncStateEvent, AnySyncTimelineEvent, AnyTimelineEvent, StateEvent,
},
serde::Raw,
};

View file

@ -13,8 +13,8 @@ pub fn remove_transaction_id(&mut self) -> Result {
return Ok(());
};
let mut unsigned: BTreeMap<String, Box<RawJsonValue>> =
serde_json::from_str(unsigned.get()).map_err(|e| err!(Database("Invalid unsigned in pdu event: {e}")))?;
let mut unsigned: BTreeMap<String, Box<RawJsonValue>> = serde_json::from_str(unsigned.get())
.map_err(|e| err!(Database("Invalid unsigned in pdu event: {e}")))?;
unsigned.remove("transaction_id");
self.unsigned = to_raw_value(&unsigned)
@ -97,7 +97,9 @@ where
#[implement(Pdu)]
#[must_use]
pub fn get_unsigned_as_value(&self) -> JsonValue { self.get_unsigned::<JsonValue>().unwrap_or_default() }
pub fn get_unsigned_as_value(&self) -> JsonValue {
self.get_unsigned::<JsonValue>().unwrap_or_default()
}
#[implement(Pdu)]
pub fn get_unsigned<T>(&self) -> Result<JsonValue> {