Skip to content

Commit

Permalink
Add an example and fix a type conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronFeickert committed Sep 9, 2022
1 parent b3ef359 commit f7289dd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
2 changes: 1 addition & 1 deletion base_layer/common_types/src/dammsum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub fn compute_checksum(data: &Vec<u8>) -> u8 {
}
}

result as u8
result
}

/// Determine whether the array ends with a valid checksum
Expand Down
33 changes: 26 additions & 7 deletions base_layer/common_types/src/emoji.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,32 @@ use crate::{
///
/// An emoji ID can be instantiated either from a public key or from a string of emoji characters, and can be
/// converted to either form as well. Checksum validation is done automatically on instantiation.
///
/// # Example
///
/// ```
/// use tari_common_types::emoji::EmojiId;
///
/// // Construct an emoji ID from an emoji string (this can fail)
/// let emoji_string = "πŸŒ΄πŸ©πŸ”ŒπŸ“ŒπŸš‘πŸŒ°πŸŽ“πŸŒ΄πŸŠπŸŒπŸ’•πŸ’‘πŸœπŸ“‰πŸ‘›πŸ΅πŸ‘›πŸ½πŸŽ‚πŸ»πŸŒ€πŸ“πŸ˜ΏπŸ­πŸΌπŸ€πŸŽͺπŸ’”πŸ’ΈπŸ…πŸ”‹πŸŽ’πŸ‘‘";
/// let emoji_id_from_emoji_string = EmojiId::from_emoji_string(emoji_string);
/// assert!(emoji_id_from_emoji_string.is_ok());
///
/// // Get the public key
/// let public_key = emoji_id_from_emoji_string.unwrap().to_public_key();
///
/// // Reconstruct the emoji ID from the public key (this cannot fail)
/// let emoji_id_from_public_key = EmojiId::from_public_key(&public_key);
///
/// // An emoji ID is deterministic
/// assert_eq!(emoji_id_from_public_key.to_emoji_string(), emoji_string);
///
/// // Oh no! We swapped the first two emoji characters by mistake, so this should fail
/// let invalid_emoji_string = "πŸ©πŸŒ΄πŸ”ŒπŸ“ŒπŸš‘πŸŒ°πŸŽ“πŸŒ΄πŸŠπŸŒπŸ’•πŸ’‘πŸœπŸ“‰πŸ‘›πŸ΅πŸ‘›πŸ½πŸŽ‚πŸ»πŸŒ€πŸ“πŸ˜ΏπŸ­πŸΌπŸ€πŸŽͺπŸ’”πŸ’ΈπŸ…πŸ”‹πŸŽ’πŸ‘‘";
/// assert!(EmojiId::from_emoji_string(invalid_emoji_string).is_err());
/// ```
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub struct EmojiId(PublicKey);

const DICT_SIZE: usize = 256; // number of elements in the symbol dictionary
const INTERNAL_SIZE: usize = 32; // number of bytes used for the internal representation (without checksum)
Expand Down Expand Up @@ -77,10 +103,6 @@ lazy_static! {
};
}

/// Internally, an EmojiId is a public key
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub struct EmojiId(PublicKey);

/// Returns the current emoji set as a character array
pub const fn emoji_set() -> [char; DICT_SIZE] {
EMOJI
Expand Down Expand Up @@ -136,8 +158,6 @@ impl EmojiId {
Self(public_key.clone())
}

/// Convert an emoji ID from a hex string

/// Convert the emoji ID to an emoji string with checksum
pub fn to_emoji_string(&self) -> String {
// Convert the public key to bytes and compute the checksum
Expand Down Expand Up @@ -183,7 +203,6 @@ mod test {

// Check the size of the corresponding emoji string
let emoji_string = emoji_id_from_public_key.to_emoji_string();
println!("{}", emoji_string); // TODO TEST ONLY

assert_eq!(emoji_string.chars().count(), INTERNAL_SIZE + CHECKSUM_SIZE);

Expand Down

0 comments on commit f7289dd

Please sign in to comment.