-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Switch channelIDs to base64 (#48)
* feat: Switch channelIDs to base64 * change channelID to random char string * fix group add (again) * reduce number of times metadata fetched * remove ChannelType, ChannelId * change channel id to "session_id" to be clear where it comes from * convert `From<String>` trait to `from_str() -> Result` Closes #47
- Loading branch information
Showing
6 changed files
with
177 additions
and
97 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use std::fmt; | ||
|
||
use base64; | ||
use rand::RngCore; | ||
use serde::ser::{Serialize, Serializer}; | ||
|
||
const CHANNELID_LEN: usize = 16; | ||
|
||
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)] | ||
pub struct ChannelID { | ||
value: [u8; CHANNELID_LEN], | ||
} | ||
|
||
impl ChannelID { | ||
pub fn to_string(self) -> String { | ||
base64::encode_config(&self.value, base64::URL_SAFE_NO_PAD) | ||
} | ||
|
||
pub fn from_str<'a>(string: &'a str) -> Result<ChannelID, base64::DecodeError> { | ||
let bytes = base64::decode_config(string, base64::URL_SAFE_NO_PAD)?; | ||
let mut array = [0; 16]; | ||
array.copy_from_slice(&bytes[..16]); | ||
Ok(ChannelID { value: array }) | ||
} | ||
} | ||
|
||
impl Default for ChannelID { | ||
fn default() -> Self { | ||
let mut rng = rand::thread_rng(); | ||
let mut bytes = [0; CHANNELID_LEN]; | ||
rng.fill_bytes(&mut bytes); | ||
Self { value: bytes } | ||
} | ||
} | ||
|
||
impl fmt::Display for ChannelID { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
// calling to_string() causes a stack overflow. | ||
let as_b64 = base64::encode_config(&self.value, base64::URL_SAFE_NO_PAD); | ||
write!(f, "{}", as_b64) | ||
} | ||
} | ||
|
||
impl Serialize for ChannelID { | ||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
serializer.serialize_str(&self.to_string()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_parse() { | ||
let raw_id = "j6jLPVPeQR6diyrkQinRAQ"; | ||
// From URLSafe b64 | ||
let chan = ChannelID::from_str(raw_id).unwrap(); | ||
assert!(chan.to_string() == raw_id.to_owned()); | ||
ChannelID::from_str("invalid").expect_err("rejected"); | ||
let output = format!("{}", chan); | ||
assert_eq!("j6jLPVPeQR6diyrkQinRAQ".to_owned(), output); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.