Skip to content

Commit

Permalink
Add comment + clarify type names (#6880)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Nov 22, 2024
1 parent ac0bb97 commit 0206220
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
24 changes: 12 additions & 12 deletions backend/libraries/chat_events/src/stable_storage/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ impl KeyPrefix {

pub fn key_type(&self) -> KeyType {
match self {
KeyPrefix::DirectChat(_) => KeyType::DirectChat,
KeyPrefix::GroupChat(_) => KeyType::GroupChat,
KeyPrefix::Channel(_) => KeyType::Channel,
KeyPrefix::DirectChatThread(_) => KeyType::DirectChatThread,
KeyPrefix::GroupChatThread(_) => KeyType::GroupChatThread,
KeyPrefix::ChannelThread(_) => KeyType::ChannelThread,
KeyPrefix::DirectChat(_) => KeyType::DirectChatEvent,
KeyPrefix::GroupChat(_) => KeyType::GroupChatEvent,
KeyPrefix::Channel(_) => KeyType::ChannelEvent,
KeyPrefix::DirectChatThread(_) => KeyType::DirectChatThreadEvent,
KeyPrefix::GroupChatThread(_) => KeyType::GroupChatThreadEvent,
KeyPrefix::ChannelThread(_) => KeyType::ChannelThreadEvent,
}
}
}
Expand Down Expand Up @@ -140,12 +140,12 @@ impl TryFrom<&[u8]> for KeyPrefix {
let bytes = Cow::Borrowed(&bytes[1..]);

match key_type {
KeyType::DirectChat => Ok(KeyPrefix::DirectChat(DirectChatKeyPrefix::from_bytes(bytes))),
KeyType::GroupChat => Ok(KeyPrefix::GroupChat(GroupChatKeyPrefix::from_bytes(bytes))),
KeyType::Channel => Ok(KeyPrefix::Channel(ChannelKeyPrefix::from_bytes(bytes))),
KeyType::DirectChatThread => Ok(KeyPrefix::DirectChatThread(DirectChatThreadKeyPrefix::from_bytes(bytes))),
KeyType::GroupChatThread => Ok(KeyPrefix::GroupChatThread(GroupChatThreadKeyPrefix::from_bytes(bytes))),
KeyType::ChannelThread => Ok(KeyPrefix::ChannelThread(ChannelThreadKeyPrefix::from_bytes(bytes))),
KeyType::DirectChatEvent => Ok(KeyPrefix::DirectChat(DirectChatKeyPrefix::from_bytes(bytes))),
KeyType::GroupChatEvent => Ok(KeyPrefix::GroupChat(GroupChatKeyPrefix::from_bytes(bytes))),
KeyType::ChannelEvent => Ok(KeyPrefix::Channel(ChannelKeyPrefix::from_bytes(bytes))),
KeyType::DirectChatThreadEvent => Ok(KeyPrefix::DirectChatThread(DirectChatThreadKeyPrefix::from_bytes(bytes))),
KeyType::GroupChatThreadEvent => Ok(KeyPrefix::GroupChatThread(GroupChatThreadKeyPrefix::from_bytes(bytes))),
KeyType::ChannelThreadEvent => Ok(KeyPrefix::ChannelThread(ChannelThreadKeyPrefix::from_bytes(bytes))),
_ => Err(()),
}
}
Expand Down
28 changes: 16 additions & 12 deletions backend/libraries/stable_memory_map/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//! If you want to store data in this map and be able to iterate over it in order, then the keys
//! must maintain their ordering when represented as bytes, since the keys in the map are ordered
//! by their bytes.
use ic_stable_structures::memory_manager::VirtualMemory;
use ic_stable_structures::{DefaultMemoryImpl, StableBTreeMap};
use std::cell::RefCell;
Expand Down Expand Up @@ -29,25 +33,25 @@ pub fn with_map_mut<F: FnOnce(&mut StableBTreeMap<Vec<u8>, Vec<u8>, Memory>) ->
#[derive(Copy, Clone)]
#[repr(u8)]
pub enum KeyType {
DirectChat = 1,
GroupChat = 2,
Channel = 3,
DirectChatThread = 4,
GroupChatThread = 5,
ChannelThread = 6,
DirectChatEvent = 1,
GroupChatEvent = 2,
ChannelEvent = 3,
DirectChatThreadEvent = 4,
GroupChatThreadEvent = 5,
ChannelThreadEvent = 6,
ChatMember = 7,
CommunityMember = 8,
}

impl From<u8> for KeyType {
fn from(value: u8) -> Self {
match value {
1 => KeyType::DirectChat,
2 => KeyType::GroupChat,
3 => KeyType::Channel,
4 => KeyType::DirectChatThread,
5 => KeyType::GroupChatThread,
6 => KeyType::ChannelThread,
1 => KeyType::DirectChatEvent,
2 => KeyType::GroupChatEvent,
3 => KeyType::ChannelEvent,
4 => KeyType::DirectChatThreadEvent,
5 => KeyType::GroupChatThreadEvent,
6 => KeyType::ChannelThreadEvent,
7 => KeyType::ChatMember,
8 => KeyType::CommunityMember,
_ => unreachable!(),
Expand Down

0 comments on commit 0206220

Please sign in to comment.