Move nick hue functions to their own module

This commit is contained in:
Joscha 2023-01-30 15:55:39 +01:00
parent 3bc50dcf26
commit e6898cc9f7
3 changed files with 20 additions and 14 deletions

View file

@ -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

View file

@ -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};

View file

@ -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))
}