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

View File

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

View File

@ -193,27 +193,26 @@ impl Store {
}
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> {
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> {
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> {
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::Left => Some(RoomState::Left(LeftRoom { inner: r })),
RoomType::Invited => self
.get_stripped_room(room_id)
.map(|r| RoomState::Invited(InvitedRoom { inner: r })),
})
.flatten()
}
fn get_stripped_room(&self, room_id: &RoomId) -> Option<StrippedRoom> {

View File

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