base: Us and_then() instead of map() + flatten()

master
Damir Jelić 2021-01-26 19:28:17 +01:00
parent b3cfa48b45
commit 55430dd3d2
4 changed files with 12 additions and 17 deletions

View File

@ -104,7 +104,7 @@ pub fn hoist_and_deserialize_state_event(
if let AnySyncStateEvent::RoomMember(ref mut member) = ev { if let AnySyncStateEvent::RoomMember(ref mut member) = ev {
if member.prev_content.is_none() { if member.prev_content.is_none() {
member.prev_content = prev_content.map(|e| e.deserialize().ok()).flatten(); member.prev_content = prev_content.and_then(|e| e.deserialize().ok());
} }
} }
@ -121,7 +121,7 @@ fn hoist_member_event(
let mut e = event.deserialize()?; let mut e = event.deserialize()?;
if e.prev_content.is_none() { if e.prev_content.is_none() {
e.prev_content = prev_content.map(|e| e.deserialize().ok()).flatten(); e.prev_content = prev_content.and_then(|e| e.deserialize().ok());
} }
Ok(e) Ok(e)
@ -133,8 +133,7 @@ fn hoist_room_event_prev_content(
let prev_content = serde_json::from_str::<AdditionalEventData>(event.json().get()) let prev_content = serde_json::from_str::<AdditionalEventData>(event.json().get())
.map(|more_unsigned| more_unsigned.unsigned) .map(|more_unsigned| more_unsigned.unsigned)
.map(|additional| additional.prev_content)? .map(|additional| additional.prev_content)?
.map(|p| p.deserialize().ok()) .and_then(|p| p.deserialize().ok());
.flatten();
let mut ev = event.deserialize()?; let mut ev = event.deserialize()?;
@ -950,8 +949,7 @@ impl BaseClient {
for member in response.chunk.iter().filter_map(|e| { for member in response.chunk.iter().filter_map(|e| {
hoist_member_event(e) hoist_member_event(e)
.ok() .ok()
.map(|e| MemberEvent::try_from(e).ok()) .and_then(|e| MemberEvent::try_from(e).ok())
.flatten()
}) { }) {
if self if self
.store .store

View File

@ -336,14 +336,13 @@ impl Room {
.store .store
.get_state_event(self.room_id(), EventType::RoomPowerLevels, "") .get_state_event(self.room_id(), EventType::RoomPowerLevels, "")
.await? .await?
.map(|e| { .and_then(|e| {
if let AnySyncStateEvent::RoomPowerLevels(e) = e { if let AnySyncStateEvent::RoomPowerLevels(e) = e {
Some(e) Some(e)
} else { } else {
None None
} }
}) });
.flatten();
let ambiguous = self let ambiguous = self
.store .store

View File

@ -193,27 +193,26 @@ impl Store {
} }
pub fn get_joined_room(&self, room_id: &RoomId) -> Option<JoinedRoom> { pub fn get_joined_room(&self, room_id: &RoomId) -> Option<JoinedRoom> {
self.get_room(room_id).map(|r| r.joined()).flatten() self.get_room(room_id).and_then(|r| r.joined())
} }
pub fn get_invited_room(&self, room_id: &RoomId) -> Option<InvitedRoom> { pub fn get_invited_room(&self, room_id: &RoomId) -> Option<InvitedRoom> {
self.get_room(room_id).map(|r| r.invited()).flatten() self.get_room(room_id).and_then(|r| r.invited())
} }
pub fn get_left_room(&self, room_id: &RoomId) -> Option<LeftRoom> { pub fn get_left_room(&self, room_id: &RoomId) -> Option<LeftRoom> {
self.get_room(room_id).map(|r| r.left()).flatten() self.get_room(room_id).and_then(|r| r.left())
} }
pub fn get_room(&self, room_id: &RoomId) -> Option<RoomState> { pub fn get_room(&self, room_id: &RoomId) -> Option<RoomState> {
self.get_bare_room(room_id) self.get_bare_room(room_id)
.map(|r| match r.room_type() { .and_then(|r| match r.room_type() {
RoomType::Joined => Some(RoomState::Joined(JoinedRoom { inner: r })), RoomType::Joined => Some(RoomState::Joined(JoinedRoom { inner: r })),
RoomType::Left => Some(RoomState::Left(LeftRoom { inner: r })), RoomType::Left => Some(RoomState::Left(LeftRoom { inner: r })),
RoomType::Invited => self RoomType::Invited => self
.get_stripped_room(room_id) .get_stripped_room(room_id)
.map(|r| RoomState::Invited(InvitedRoom { inner: r })), .map(|r| RoomState::Invited(InvitedRoom { inner: r })),
}) })
.flatten()
} }
fn get_stripped_room(&self, room_id: &RoomId) -> Option<StrippedRoom> { fn get_stripped_room(&self, room_id: &RoomId) -> Option<StrippedRoom> {

View File

@ -351,8 +351,7 @@ impl SledStore {
.encode(); .encode();
let display_name = profile_changes let display_name = profile_changes
.map(|p| p.get(&event.state_key)) .and_then(|p| p.get(&event.state_key))
.flatten()
.as_ref() .as_ref()
.map(|m| m.displayname.as_deref()) .map(|m| m.displayname.as_deref())
.unwrap_or_else(|| Some(event.state_key.localpart())) .unwrap_or_else(|| Some(event.state_key.localpart()))
@ -390,7 +389,7 @@ impl SledStore {
)?; )?;
if let Some(profile) = if let Some(profile) =
profile_changes.map(|p| p.get(&event.state_key)).flatten() profile_changes.and_then(|p| p.get(&event.state_key))
{ {
profiles.insert( profiles.insert(
key.as_slice(), key.as_slice(),