diff --git a/CHANGELOG.md b/CHANGELOG.md index 4aec2ce..99e9cc6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,15 +18,14 @@ Procedure when bumping the version number: - `euphoxide::bot` module (enable the `bot` feature to use) - `euphoxide::Emoji` for finding, replacing and removing colon-delimited emoji in text - `euphoxide::api::Time::new` -- `euphoxide::nick_hue_without_removing_emoji` +- `euphoxide::nick::hue` - Debug logging using the `log` crate - `testbot_instance` example using the new `euphoxide::bot::instance::Instance` - VSCode project settings ### Changed - `euphoxide::conn` module redesigned and rewritten (backwards-incompatible) -- `euphoxide::nick_hue` takes emoji into account (backwards-incompatible) - - `euphoxide::nick_hue_without_removing_emoji` has the old behaviour +- `euphoxide::nick_hue` moved to `euphoxide::nick::hue_without_removing_emoji` - Renamed `testbot` example to `testbot_manual` ### Removed diff --git a/src/lib.rs b/src/lib.rs index 6b96b8e..66fb34e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,8 +14,7 @@ pub mod api; pub mod bot; pub mod conn; mod emoji; -mod huehash; +pub mod nick; mod replies; pub use emoji::Emoji; -pub use huehash::{nick_hue, nick_hue_without_removing_emoji}; diff --git a/src/huehash.rs b/src/nick.rs similarity index 53% rename from src/huehash.rs rename to src/nick.rs index d44ce88..d4a176f 100644 --- a/src/huehash.rs +++ b/src/nick.rs @@ -1,7 +1,9 @@ +//! Nick-related utility functions. + use crate::emoji::Emoji; /// Does not remove emoji. -fn normalize(text: &str) -> String { +fn hue_normalize(text: &str) -> String { text.chars() .filter(|&c| c.is_ascii_alphanumeric() || c == '_' || c == '-') .map(|c| c.to_ascii_lowercase()) @@ -24,13 +26,13 @@ fn hue_hash(text: &str, offset: i64) -> u8 { const GREENIE_OFFSET: i64 = 148 - 192; // 148 - hue_hash("greenie", 0) -/// Calculate the nick hue without removing colon-delimited emoji as part of -/// normalization. +/// Calculate a nick's hue like [`hue`] but without removing colon-delimited +/// emoji as part of normalization. /// -/// This should be slightly faster than [`nick_hue`] but produces incorrect -/// results if any colon-delimited emoji are present. -pub fn nick_hue_without_removing_emoji(nick: &str) -> u8 { - let normalized = normalize(nick); +/// This should be slightly faster than [`hue`] but produces incorrect results +/// if any colon-delimited emoji are present. +pub fn hue_without_removing_emoji(nick: &str) -> u8 { + let normalized = hue_normalize(nick); if normalized.is_empty() { hue_hash(nick, GREENIE_OFFSET) } else { @@ -38,6 +40,12 @@ pub fn nick_hue_without_removing_emoji(nick: &str) -> u8 { } } -pub fn nick_hue(emoji: &Emoji, nick: &str) -> u8 { - nick_hue_without_removing_emoji(&emoji.remove(nick)) +/// Calculate a nick's hue. +/// +/// This is a reimplementation of [euphoria's nick hue hashing algorithm][0]. It +/// should always return the same value as the official client's implementation. +/// +/// [0]: https://github.com/euphoria-io/heim/blob/master/client/lib/hueHash.js +pub fn hue(emoji: &Emoji, nick: &str) -> u8 { + hue_without_removing_emoji(&emoji.remove(nick)) }