Skip to content

Commit 6abf95d

Browse files
committed
Create token module
1 parent 77c4f15 commit 6abf95d

File tree

2 files changed

+74
-55
lines changed

2 files changed

+74
-55
lines changed

src/lib.rs

Lines changed: 72 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// vim: foldmethod=marker :
2-
pub use crate::metadata::{WebiconFamily, WebiconVendorMetadata};
3-
use emojis::Emoji;
2+
3+
pub use self::metadata::WebiconFamily;
44

55
pub mod metadata {
66
//! Helpers for handling metadata for webicons.
@@ -112,48 +112,80 @@ pub mod metadata {
112112
//}}}
113113
}
114114

115-
/// Gracefully converts an emoji shortcode to the string representation of the unicode character.
116-
///
117-
/// # Examples
118-
///
119-
/// ```rust
120-
/// use webicons::{normalize_id, WebiconFamily};
121-
///
122-
/// assert_eq!("1f600", normalize_id("grinning", WebiconFamily::Emojis));
123-
/// ```
124-
pub fn normalize_id(id: &str, family: WebiconFamily) -> String {
125-
if family == WebiconFamily::Emojis && !unic_emoji_char::is_emoji(str_to_char(&id)) {
126-
get_id_from_shortcode(id)
127-
} else {
128-
String::from(id)
115+
pub mod token {
116+
//! Helpers for handling misc webicon representations (strings, IDs, etc).
117+
//{{{
118+
119+
use crate::metadata::WebiconFamily;
120+
use emojis::Emoji;
121+
122+
/// Gracefully converts an emoji shortcode to the string representation of the unicode character.
123+
///
124+
/// # Examples
125+
///
126+
/// ```rust
127+
/// use webicons::{token, WebiconFamily};
128+
///
129+
/// assert_eq!("1f600", token::normalize_id("grinning", WebiconFamily::Emojis));
130+
/// ```
131+
pub fn normalize_id(id: &str, family: WebiconFamily) -> String {
132+
if family == WebiconFamily::Emojis && !unic_emoji_char::is_emoji(str_to_char(&id)) {
133+
get_id_from_shortcode(id)
134+
} else {
135+
String::from(id)
136+
}
129137
}
130-
}
131138

132-
/// Gets an emoji given its ID.
133-
///
134-
/// # Examples
135-
///
136-
/// ```rust
137-
/// assert_eq!("😀", webicons::get_emoji_from_id("1f600").as_str());
138-
/// ```
139-
pub fn get_emoji_from_id(id: &str) -> &Emoji {
140-
let i = u32::from_str_radix(id, 16).unwrap();
141-
let emoji_string = String::from(char::from_u32(i).unwrap());
142-
emojis::get(&emoji_string).expect(&format!("Unable to get emoji from id {}.", id))
143-
}
139+
/// Gets an emoji given its ID.
140+
///
141+
/// # Examples
142+
///
143+
/// ```rust
144+
/// assert_eq!("😀", webicons::token::get_emoji_from_id("1f600").as_str());
145+
/// ```
146+
pub fn get_emoji_from_id(id: &str) -> &Emoji {
147+
let i = u32::from_str_radix(id, 16).unwrap();
148+
let emoji_string = String::from(char::from_u32(i).unwrap());
149+
emojis::get(&emoji_string).expect(&format!("Unable to get emoji from id {}.", id))
150+
}
144151

145-
/// Gets an emoji's ID given its shortcode.
146-
fn get_id_from_shortcode(shortcode: &str) -> String {
147-
let emoji: &Emoji = match emojis::get_by_shortcode(shortcode) {
148-
Some(emoji) => emoji,
149-
None => panic!("Unable to find shortcode {}", shortcode),
150-
};
151-
format!("{:x}", str_to_char(emoji.as_str()) as u32)
152-
}
152+
/// Gets an emoji's ID given its shortcode.
153+
fn get_id_from_shortcode(shortcode: &str) -> String {
154+
let emoji: &Emoji = match emojis::get_by_shortcode(shortcode) {
155+
Some(emoji) => emoji,
156+
None => panic!("Unable to find shortcode {}", shortcode),
157+
};
158+
format!("{:x}", str_to_char(emoji.as_str()) as u32)
159+
}
160+
161+
/// Converts the first character of a str ("abc") to a char ('a').
162+
fn str_to_char(s: &str) -> char {
163+
s.chars().nth(0).unwrap()
164+
}
165+
166+
#[cfg(test)]
167+
mod tests {
168+
//! Unit tests for private parts of token module.
169+
//{{{
170+
171+
use super::*;
172+
173+
#[test]
174+
fn test_str_to_char() {
175+
assert_eq!('a', str_to_char("a"));
176+
assert_eq!('a', str_to_char("abc"));
177+
assert_eq!('😀', str_to_char("😀"));
178+
assert_eq!('\u{1f600}', str_to_char("\u{1f600}"));
179+
}
153180

154-
/// Converts the first character of a str ("abc") to a char ('a').
155-
fn str_to_char(s: &str) -> char {
156-
s.chars().nth(0).unwrap()
181+
#[test]
182+
fn test_get_id_from_shortcode() {
183+
let id = get_id_from_shortcode("grinning");
184+
assert_eq!("1f600", id);
185+
}
186+
//}}}
187+
}
188+
//}}}
157189
}
158190

159191
pub mod html {
@@ -221,18 +253,5 @@ mod tests {
221253
);
222254
}
223255

224-
#[test]
225-
fn test_str_to_char() {
226-
assert_eq!('a', str_to_char("a"));
227-
assert_eq!('a', str_to_char("abc"));
228-
assert_eq!('😀', str_to_char("😀"));
229-
assert_eq!('\u{1f600}', str_to_char("\u{1f600}"));
230-
}
231-
232-
#[test]
233-
fn test_get_id_from_shortcode() {
234-
let id = get_id_from_shortcode("grinning");
235-
assert_eq!("1f600", id);
236-
}
237256
//}}}
238257
}

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ fn get_webicon(family: &str, id: &str, vendor: Option<String>) -> (ContentType,
2222
let default_vendor = metadata::get_default_vendor(DEFAULT_CONFIG_FILE_PATH, family).to_string();
2323
let vendor = vendor.unwrap_or(default_vendor);
2424
let family = metadata::WebiconFamily::from_str(family).expect("Invalid webicon family.");
25-
let id = normalize_id(id, family);
25+
let id = token::normalize_id(id, family);
2626

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

0 commit comments

Comments
 (0)