pikadick/
util.rs

1mod ascii_table;
2mod encoder_task;
3mod loading_reaction;
4mod timed_cache;
5
6pub use self::{
7    ascii_table::AsciiTable,
8    encoder_task::EncoderTask,
9    loading_reaction::LoadingReaction,
10    timed_cache::{
11        TimedCache,
12        TimedCacheEntry,
13    },
14};
15use once_cell::sync::Lazy;
16use regex::Regex;
17use url::Url;
18
19/// Source: <https://urlregex.com/>
20static URL_REGEX: Lazy<Regex> =
21    Lazy::new(|| Regex::new(include_str!("url_regex.txt")).expect("invalid url regex"));
22
23/// Get an iterator over urls in text.
24pub fn extract_urls(text: &str) -> impl Iterator<Item = Url> + '_ {
25    // Regex doesn't HAVE to be perfect.
26    // Ideally, it just needs to be aggressive since parsing it into a url will weed out invalids.
27    URL_REGEX
28        .find_iter(text)
29        .filter_map(|url_match| Url::parse(url_match.as_str()).ok())
30}