instagram_hashtag_parser/
lib.rs

1#[cfg(feature = "with-crate-hashtag")]
2use hashtag::HashtagParser;
3
4#[cfg(feature = "with-regex")]
5use once_cell::sync::Lazy;
6#[cfg(feature = "with-regex")]
7use regex::Regex;
8
9#[cfg(feature = "with-regex")]
10// https://stackoverflow.com/a/31965629/918930
11static RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"#((\w|[\u00A1-\uFFFF])+)").unwrap());
12
13#[cfg(feature = "with-crate-hashtag")]
14pub fn hashtags_with_crate_hashtag(s: &str) -> Vec<String> {
15    HashtagParser::new(s)
16        .into_iter()
17        .map(|tag| tag.text.to_string())
18        .collect::<Vec<_>>()
19}
20
21#[cfg(feature = "with-regex")]
22pub fn hashtags_with_regex(s: &str) -> Vec<String> {
23    RE.captures_iter(s)
24        .into_iter()
25        .map(|x| x[1].to_string())
26        .collect()
27}
28
29#[cfg(feature = "with-crate-hashtag")]
30#[cfg(test)]
31mod tests_with_crate_hashtag {
32    use super::*;
33
34    #[test]
35    fn simple() {
36        assert_eq!(
37            hashtags_with_crate_hashtag("#rust is #awesome"),
38            vec!["rust".to_owned(), "awesome".to_owned()]
39        );
40
41        assert_eq!(
42            hashtags_with_crate_hashtag("#我#我的"),
43            vec!["我".to_owned(), "我的".to_owned()]
44        );
45    }
46}
47
48#[cfg(feature = "with-regex")]
49#[cfg(test)]
50mod tests_with_regex {
51    use super::*;
52
53    #[test]
54    fn simple() {
55        assert_eq!(
56            hashtags_with_regex("#rust is #awesome"),
57            vec!["rust".to_owned(), "awesome".to_owned()]
58        );
59
60        assert_eq!(
61            hashtags_with_regex("#我#我的"),
62            vec!["我".to_owned(), "我的".to_owned()]
63        );
64    }
65}