Move nick hue functions to their own module
This commit is contained in:
parent
3bc50dcf26
commit
e6898cc9f7
3 changed files with 20 additions and 14 deletions
|
|
@ -18,15 +18,14 @@ Procedure when bumping the version number:
|
||||||
- `euphoxide::bot` module (enable the `bot` feature to use)
|
- `euphoxide::bot` module (enable the `bot` feature to use)
|
||||||
- `euphoxide::Emoji` for finding, replacing and removing colon-delimited emoji in text
|
- `euphoxide::Emoji` for finding, replacing and removing colon-delimited emoji in text
|
||||||
- `euphoxide::api::Time::new`
|
- `euphoxide::api::Time::new`
|
||||||
- `euphoxide::nick_hue_without_removing_emoji`
|
- `euphoxide::nick::hue`
|
||||||
- Debug logging using the `log` crate
|
- Debug logging using the `log` crate
|
||||||
- `testbot_instance` example using the new `euphoxide::bot::instance::Instance`
|
- `testbot_instance` example using the new `euphoxide::bot::instance::Instance`
|
||||||
- VSCode project settings
|
- VSCode project settings
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
- `euphoxide::conn` module redesigned and rewritten (backwards-incompatible)
|
- `euphoxide::conn` module redesigned and rewritten (backwards-incompatible)
|
||||||
- `euphoxide::nick_hue` takes emoji into account (backwards-incompatible)
|
- `euphoxide::nick_hue` moved to `euphoxide::nick::hue_without_removing_emoji`
|
||||||
- `euphoxide::nick_hue_without_removing_emoji` has the old behaviour
|
|
||||||
- Renamed `testbot` example to `testbot_manual`
|
- Renamed `testbot` example to `testbot_manual`
|
||||||
|
|
||||||
### Removed
|
### Removed
|
||||||
|
|
|
||||||
|
|
@ -14,8 +14,7 @@ pub mod api;
|
||||||
pub mod bot;
|
pub mod bot;
|
||||||
pub mod conn;
|
pub mod conn;
|
||||||
mod emoji;
|
mod emoji;
|
||||||
mod huehash;
|
pub mod nick;
|
||||||
mod replies;
|
mod replies;
|
||||||
|
|
||||||
pub use emoji::Emoji;
|
pub use emoji::Emoji;
|
||||||
pub use huehash::{nick_hue, nick_hue_without_removing_emoji};
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,9 @@
|
||||||
|
//! Nick-related utility functions.
|
||||||
|
|
||||||
use crate::emoji::Emoji;
|
use crate::emoji::Emoji;
|
||||||
|
|
||||||
/// Does not remove emoji.
|
/// Does not remove emoji.
|
||||||
fn normalize(text: &str) -> String {
|
fn hue_normalize(text: &str) -> String {
|
||||||
text.chars()
|
text.chars()
|
||||||
.filter(|&c| c.is_ascii_alphanumeric() || c == '_' || c == '-')
|
.filter(|&c| c.is_ascii_alphanumeric() || c == '_' || c == '-')
|
||||||
.map(|c| c.to_ascii_lowercase())
|
.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)
|
const GREENIE_OFFSET: i64 = 148 - 192; // 148 - hue_hash("greenie", 0)
|
||||||
|
|
||||||
/// Calculate the nick hue without removing colon-delimited emoji as part of
|
/// Calculate a nick's hue like [`hue`] but without removing colon-delimited
|
||||||
/// normalization.
|
/// emoji as part of normalization.
|
||||||
///
|
///
|
||||||
/// This should be slightly faster than [`nick_hue`] but produces incorrect
|
/// This should be slightly faster than [`hue`] but produces incorrect results
|
||||||
/// results if any colon-delimited emoji are present.
|
/// if any colon-delimited emoji are present.
|
||||||
pub fn nick_hue_without_removing_emoji(nick: &str) -> u8 {
|
pub fn hue_without_removing_emoji(nick: &str) -> u8 {
|
||||||
let normalized = normalize(nick);
|
let normalized = hue_normalize(nick);
|
||||||
if normalized.is_empty() {
|
if normalized.is_empty() {
|
||||||
hue_hash(nick, GREENIE_OFFSET)
|
hue_hash(nick, GREENIE_OFFSET)
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -38,6 +40,12 @@ pub fn nick_hue_without_removing_emoji(nick: &str) -> u8 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn nick_hue(emoji: &Emoji, nick: &str) -> u8 {
|
/// Calculate a nick's hue.
|
||||||
nick_hue_without_removing_emoji(&emoji.remove(nick))
|
///
|
||||||
|
/// 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))
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue