Make Result alias usable with any error type

This commit is contained in:
Jonas Platte 2021-10-13 11:56:46 +02:00
parent f2ef5677e0
commit 91afa1e0e0
No known key found for this signature in database
GPG key ID: CC154DE0E30B7C67
5 changed files with 13 additions and 15 deletions

View file

@ -60,7 +60,7 @@ use rocket::{get, tokio};
pub async fn sync_events_route( pub async fn sync_events_route(
db: DatabaseGuard, db: DatabaseGuard,
body: Ruma<sync_events::Request<'_>>, body: Ruma<sync_events::Request<'_>>,
) -> std::result::Result<RumaResponse<sync_events::Response>, RumaResponse<UiaaResponse>> { ) -> Result<RumaResponse<sync_events::Response>, RumaResponse<UiaaResponse>> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let sender_device = body.sender_device.as_ref().expect("user is authenticated"); let sender_device = body.sender_device.as_ref().expect("user is authenticated");
@ -182,7 +182,7 @@ async fn sync_helper(
full_state: bool, full_state: bool,
timeout: Option<Duration>, timeout: Option<Duration>,
// bool = caching allowed // bool = caching allowed
) -> std::result::Result<(sync_events::Response, bool), Error> { ) -> Result<(sync_events::Response, bool), Error> {
// TODO: match body.set_presence { // TODO: match body.set_presence {
db.rooms.edus.ping_presence(&sender_user)?; db.rooms.edus.ping_presence(&sender_user)?;

View file

@ -125,7 +125,7 @@ impl WildCardedDomain {
} }
impl std::str::FromStr for WildCardedDomain { impl std::str::FromStr for WildCardedDomain {
type Err = std::convert::Infallible; type Err = std::convert::Infallible;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
// maybe do some domain validation? // maybe do some domain validation?
Ok(if s.starts_with("*.") { Ok(if s.starts_with("*.") {
WildCardedDomain::WildCarded(s[1..].to_owned()) WildCardedDomain::WildCarded(s[1..].to_owned())
@ -137,7 +137,7 @@ impl std::str::FromStr for WildCardedDomain {
} }
} }
impl<'de> Deserialize<'de> for WildCardedDomain { impl<'de> Deserialize<'de> for WildCardedDomain {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error> fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where where
D: serde::de::Deserializer<'de>, D: serde::de::Deserializer<'de>,
{ {

View file

@ -485,7 +485,7 @@ impl Sending {
kind: OutgoingKind, kind: OutgoingKind,
events: Vec<SendingEventType>, events: Vec<SendingEventType>,
db: Arc<RwLock<Database>>, db: Arc<RwLock<Database>>,
) -> std::result::Result<OutgoingKind, (OutgoingKind, Error)> { ) -> Result<OutgoingKind, (OutgoingKind, Error)> {
let db = db.read().await; let db = db.read().await;
match &kind { match &kind {

View file

@ -20,7 +20,7 @@ use {
tracing::error, tracing::error,
}; };
pub type Result<T> = std::result::Result<T, Error>; pub type Result<T, E = Error> = std::result::Result<T, E>;
#[derive(Error, Debug)] #[derive(Error, Debug)]
pub enum Error { pub enum Error {

View file

@ -65,7 +65,6 @@ use std::{
mem, mem,
net::{IpAddr, SocketAddr}, net::{IpAddr, SocketAddr},
pin::Pin, pin::Pin,
result::Result as StdResult,
sync::{Arc, RwLock, RwLockWriteGuard}, sync::{Arc, RwLock, RwLockWriteGuard},
time::{Duration, Instant, SystemTime}, time::{Duration, Instant, SystemTime},
}; };
@ -956,7 +955,7 @@ pub(crate) async fn handle_incoming_pdu<'a>(
is_timeline_event: bool, is_timeline_event: bool,
db: &'a Database, db: &'a Database,
pub_key_map: &'a RwLock<BTreeMap<String, BTreeMap<String, String>>>, pub_key_map: &'a RwLock<BTreeMap<String, BTreeMap<String, String>>>,
) -> StdResult<Option<Vec<u8>>, String> { ) -> Result<Option<Vec<u8>>, String> {
match db.rooms.exists(room_id) { match db.rooms.exists(room_id) {
Ok(true) => {} Ok(true) => {}
_ => { _ => {
@ -1137,8 +1136,7 @@ fn handle_outlier_pdu<'a>(
value: BTreeMap<String, CanonicalJsonValue>, value: BTreeMap<String, CanonicalJsonValue>,
db: &'a Database, db: &'a Database,
pub_key_map: &'a RwLock<BTreeMap<String, BTreeMap<String, String>>>, pub_key_map: &'a RwLock<BTreeMap<String, BTreeMap<String, String>>>,
) -> AsyncRecursiveType<'a, StdResult<(Arc<PduEvent>, BTreeMap<String, CanonicalJsonValue>), String>> ) -> AsyncRecursiveType<'a, Result<(Arc<PduEvent>, BTreeMap<String, CanonicalJsonValue>), String>> {
{
Box::pin(async move { Box::pin(async move {
// TODO: For RoomVersion6 we must check that Raw<..> is canonical do we anywhere?: https://matrix.org/docs/spec/rooms/v6#canonical-json // TODO: For RoomVersion6 we must check that Raw<..> is canonical do we anywhere?: https://matrix.org/docs/spec/rooms/v6#canonical-json
@ -1301,7 +1299,7 @@ async fn upgrade_outlier_to_timeline_pdu(
db: &Database, db: &Database,
room_id: &RoomId, room_id: &RoomId,
pub_key_map: &RwLock<BTreeMap<String, BTreeMap<String, String>>>, pub_key_map: &RwLock<BTreeMap<String, BTreeMap<String, String>>>,
) -> StdResult<Option<Vec<u8>>, String> { ) -> Result<Option<Vec<u8>>, String> {
if let Ok(Some(pduid)) = db.rooms.get_pdu_id(&incoming_pdu.event_id) { if let Ok(Some(pduid)) = db.rooms.get_pdu_id(&incoming_pdu.event_id) {
return Ok(Some(pduid)); return Ok(Some(pduid));
} }
@ -1448,7 +1446,7 @@ async fn upgrade_outlier_to_timeline_pdu(
.map_err(|_| "Failed to get_or_create_shortstatekey".to_owned())?; .map_err(|_| "Failed to get_or_create_shortstatekey".to_owned())?;
Ok((shortstatekey, Arc::new(event_id))) Ok((shortstatekey, Arc::new(event_id)))
}) })
.collect::<StdResult<_, String>>()?, .collect::<Result<_, String>>()?,
), ),
Err(e) => { Err(e) => {
warn!("State resolution on prev events failed, either an event could not be found or deserialization: {}", e); warn!("State resolution on prev events failed, either an event could not be found or deserialization: {}", e);
@ -1630,7 +1628,7 @@ async fn upgrade_outlier_to_timeline_pdu(
.compress_state_event(*shortstatekey, id, &db.globals) .compress_state_event(*shortstatekey, id, &db.globals)
.map_err(|_| "Failed to compress_state_event".to_owned()) .map_err(|_| "Failed to compress_state_event".to_owned())
}) })
.collect::<StdResult<_, _>>()?; .collect::<Result<_, _>>()?;
// 13. Check if the event passes auth based on the "current state" of the room, if not "soft fail" it // 13. Check if the event passes auth based on the "current state" of the room, if not "soft fail" it
debug!("starting soft fail auth check"); debug!("starting soft fail auth check");
@ -1750,7 +1748,7 @@ async fn upgrade_outlier_to_timeline_pdu(
.compress_state_event(*k, id, &db.globals) .compress_state_event(*k, id, &db.globals)
.map_err(|_| "Failed to compress_state_event.".to_owned()) .map_err(|_| "Failed to compress_state_event.".to_owned())
}) })
.collect::<StdResult<_, _>>()? .collect::<Result<_, _>>()?
} else { } else {
// We do need to force an update to this room's state // We do need to force an update to this room's state
update_state = true; update_state = true;
@ -1812,7 +1810,7 @@ async fn upgrade_outlier_to_timeline_pdu(
.compress_state_event(shortstatekey, &event_id, &db.globals) .compress_state_event(shortstatekey, &event_id, &db.globals)
.map_err(|_| "Failed to compress state event".to_owned()) .map_err(|_| "Failed to compress state event".to_owned())
}) })
.collect::<StdResult<_, _>>()? .collect::<Result<_, _>>()?
}; };
// Set the new room state to the resolved state // Set the new room state to the resolved state