|
1 | 1 | // vim: foldmethod=marker :
|
2 |
| -pub use crate::metadata::{WebiconFamily, WebiconVendorMetadata}; |
3 |
| -use emojis::Emoji; |
| 2 | + |
| 3 | +pub use self::metadata::WebiconFamily; |
4 | 4 |
|
5 | 5 | pub mod metadata {
|
6 | 6 | //! Helpers for handling metadata for webicons.
|
@@ -112,48 +112,80 @@ pub mod metadata {
|
112 | 112 | //}}}
|
113 | 113 | }
|
114 | 114 |
|
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 | + } |
129 | 137 | }
|
130 |
| -} |
131 | 138 |
|
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 | + } |
144 | 151 |
|
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 | + } |
153 | 180 |
|
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 | + //}}} |
157 | 189 | }
|
158 | 190 |
|
159 | 191 | pub mod html {
|
@@ -221,18 +253,5 @@ mod tests {
|
221 | 253 | );
|
222 | 254 | }
|
223 | 255 |
|
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 |
| - } |
237 | 256 | //}}}
|
238 | 257 | }
|
0 commit comments