Skip to content

Commit

Permalink
Create token module
Browse files Browse the repository at this point in the history
  • Loading branch information
lindhe committed Aug 13, 2023
1 parent 77c4f15 commit 6abf95d
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 55 deletions.
125 changes: 72 additions & 53 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// vim: foldmethod=marker :
pub use crate::metadata::{WebiconFamily, WebiconVendorMetadata};
use emojis::Emoji;

pub use self::metadata::WebiconFamily;

pub mod metadata {
//! Helpers for handling metadata for webicons.
Expand Down Expand Up @@ -112,48 +112,80 @@ pub mod metadata {
//}}}
}

/// Gracefully converts an emoji shortcode to the string representation of the unicode character.
///
/// # Examples
///
/// ```rust
/// use webicons::{normalize_id, WebiconFamily};
///
/// assert_eq!("1f600", normalize_id("grinning", WebiconFamily::Emojis));
/// ```
pub fn normalize_id(id: &str, family: WebiconFamily) -> String {
if family == WebiconFamily::Emojis && !unic_emoji_char::is_emoji(str_to_char(&id)) {
get_id_from_shortcode(id)
} else {
String::from(id)
pub mod token {
//! Helpers for handling misc webicon representations (strings, IDs, etc).
//{{{

use crate::metadata::WebiconFamily;
use emojis::Emoji;

/// Gracefully converts an emoji shortcode to the string representation of the unicode character.
///
/// # Examples
///
/// ```rust
/// use webicons::{token, WebiconFamily};
///
/// assert_eq!("1f600", token::normalize_id("grinning", WebiconFamily::Emojis));
/// ```
pub fn normalize_id(id: &str, family: WebiconFamily) -> String {
if family == WebiconFamily::Emojis && !unic_emoji_char::is_emoji(str_to_char(&id)) {
get_id_from_shortcode(id)
} else {
String::from(id)
}
}
}

/// Gets an emoji given its ID.
///
/// # Examples
///
/// ```rust
/// assert_eq!("😀", webicons::get_emoji_from_id("1f600").as_str());
/// ```
pub fn get_emoji_from_id(id: &str) -> &Emoji {
let i = u32::from_str_radix(id, 16).unwrap();
let emoji_string = String::from(char::from_u32(i).unwrap());
emojis::get(&emoji_string).expect(&format!("Unable to get emoji from id {}.", id))
}
/// Gets an emoji given its ID.
///
/// # Examples
///
/// ```rust
/// assert_eq!("😀", webicons::token::get_emoji_from_id("1f600").as_str());
/// ```
pub fn get_emoji_from_id(id: &str) -> &Emoji {
let i = u32::from_str_radix(id, 16).unwrap();
let emoji_string = String::from(char::from_u32(i).unwrap());
emojis::get(&emoji_string).expect(&format!("Unable to get emoji from id {}.", id))
}

/// Gets an emoji's ID given its shortcode.
fn get_id_from_shortcode(shortcode: &str) -> String {
let emoji: &Emoji = match emojis::get_by_shortcode(shortcode) {
Some(emoji) => emoji,
None => panic!("Unable to find shortcode {}", shortcode),
};
format!("{:x}", str_to_char(emoji.as_str()) as u32)
}
/// Gets an emoji's ID given its shortcode.
fn get_id_from_shortcode(shortcode: &str) -> String {
let emoji: &Emoji = match emojis::get_by_shortcode(shortcode) {
Some(emoji) => emoji,
None => panic!("Unable to find shortcode {}", shortcode),
};
format!("{:x}", str_to_char(emoji.as_str()) as u32)
}

/// Converts the first character of a str ("abc") to a char ('a').
fn str_to_char(s: &str) -> char {
s.chars().nth(0).unwrap()
}

#[cfg(test)]
mod tests {
//! Unit tests for private parts of token module.
//{{{

use super::*;

#[test]
fn test_str_to_char() {
assert_eq!('a', str_to_char("a"));
assert_eq!('a', str_to_char("abc"));
assert_eq!('😀', str_to_char("😀"));
assert_eq!('\u{1f600}', str_to_char("\u{1f600}"));
}

/// Converts the first character of a str ("abc") to a char ('a').
fn str_to_char(s: &str) -> char {
s.chars().nth(0).unwrap()
#[test]
fn test_get_id_from_shortcode() {
let id = get_id_from_shortcode("grinning");
assert_eq!("1f600", id);
}
//}}}
}
//}}}
}

pub mod html {
Expand Down Expand Up @@ -221,18 +253,5 @@ mod tests {
);
}

#[test]
fn test_str_to_char() {
assert_eq!('a', str_to_char("a"));
assert_eq!('a', str_to_char("abc"));
assert_eq!('😀', str_to_char("😀"));
assert_eq!('\u{1f600}', str_to_char("\u{1f600}"));
}

#[test]
fn test_get_id_from_shortcode() {
let id = get_id_from_shortcode("grinning");
assert_eq!("1f600", id);
}
//}}}
}
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ fn get_webicon(family: &str, id: &str, vendor: Option<String>) -> (ContentType,
let default_vendor = metadata::get_default_vendor(DEFAULT_CONFIG_FILE_PATH, family).to_string();
let vendor = vendor.unwrap_or(default_vendor);
let family = metadata::WebiconFamily::from_str(family).expect("Invalid webicon family.");
let id = normalize_id(id, family);
let id = token::normalize_id(id, family);

let metadata = metadata::get_metadata(DEFAULT_CONFIG_FILE_PATH, family, &vendor);
let emoji = get_emoji_from_id(&id).as_str();
let emoji = token::get_emoji_from_id(&id).as_str();
let title = format!("{} ({})", emoji, id);
let html = webicons::html::make_html(&metadata, &title);

Expand Down

0 comments on commit 6abf95d

Please sign in to comment.