pub mod records;
use serde::{Deserialize, Serialize};
pub use records::{
format_system_time_iso8601, format_unix_secs_iso8601, has_nsfw_self_label, now_iso8601, Embed,
FollowRecord, ListBlockRecord, ListItemRecord, ListRecordsResponse, ModListRecord, PostRecord,
RecordEmbed, RecordWithMediaEmbed, ReplyRef, RepoRecordItem, SelfLabel, SelfLabels, StrongRef,
NSFW_SELF_LABEL_VALUES,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ByteSlice {
pub byte_start: usize,
pub byte_end: usize,
}
impl ByteSlice {
#[must_use]
pub const fn new(byte_start: usize, byte_end: usize) -> Self {
Self {
byte_start,
byte_end,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "$type")]
pub enum FacetFeature {
#[serde(rename = "app.bsky.richtext.facet#mention")]
Mention {
did: String,
},
#[serde(rename = "app.bsky.richtext.facet#link")]
Link {
uri: String,
},
#[serde(rename = "app.bsky.richtext.facet#tag")]
Tag {
tag: String,
},
#[serde(other)]
Unknown,
}
impl FacetFeature {
#[must_use]
pub fn as_mention_did(&self) -> Option<&str> {
match self {
Self::Mention { did } => Some(did.as_str()),
_ => None,
}
}
#[must_use]
pub fn is_mention(&self) -> bool {
matches!(self, Self::Mention { .. })
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Facet {
pub index: ByteSlice,
pub features: Vec<FacetFeature>,
}
impl Facet {
#[must_use]
pub fn new(index: ByteSlice, features: Vec<FacetFeature>) -> Self {
Self { index, features }
}
#[must_use]
pub fn link(byte_start: usize, byte_end: usize, uri: impl Into<String>) -> Self {
Self {
index: ByteSlice::new(byte_start, byte_end),
features: vec![FacetFeature::Link { uri: uri.into() }],
}
}
pub fn mentioned_dids(&self) -> impl Iterator<Item = &str> {
self.features
.iter()
.filter_map(FacetFeature::as_mention_did)
}
#[must_use]
pub fn contains_mention_did(&self, target_did: &str) -> bool {
self.mentioned_dids().any(|d| d == target_did)
}
}
pub type FacetIndex = ByteSlice;
#[must_use]
pub fn extract_link_facets(text: &str) -> Vec<Facet> {
use std::sync::LazyLock;
static URL_REGEX: LazyLock<Option<regex::Regex>> =
LazyLock::new(|| regex::Regex::new(r"(?-u:\b)(?i:https?)://[\x21-\x7E]+").ok());
let mut facets = Vec::new();
let Some(ref re) = *URL_REGEX else {
return facets;
};
for mat in re.find_iter(text) {
let raw = mat.as_str();
let trimmed = trim_trailing_url_punctuation(raw);
if trimmed.is_empty() {
continue;
}
if let Ok(parsed) = url::Url::parse(trimmed) {
if (parsed.scheme() == "http" || parsed.scheme() == "https") && parsed.has_host() {
let start = mat.start();
let end = start + trimmed.len();
facets.push(Facet::link(start, end, trimmed));
}
}
}
facets
}
fn trim_trailing_url_punctuation(raw_url: &str) -> &str {
let mut url = raw_url;
loop {
if url.is_empty() {
break;
}
let last_char = match url.chars().next_back() {
Some(c) => c,
None => break,
};
match last_char {
'.' | ',' | ';' | ':' | '!' | '?' | '"' | '\'' | '`' | '>' | '<' | '*' | '~'
| '”' | '“' | '’' | '‘' | '»' | '«' | '›' | '‹' | '„' | '‟' | '‚'
| '…' | '⋯' | '‥' | '—' | '–'
| '。' | '、' | '!' | '?' | ':' | ';' | ',' | '.'
| '\u{200B}' | '\u{200C}' | '\u{200D}' | '\u{200E}' | '\u{200F}' | '\u{FEFF}' => {
url = &url[..url.len() - last_char.len_utf8()];
}
')' if is_unbalanced_closing(url, '(', ')') => {
url = &url[..url.len() - 1];
}
']' if is_unbalanced_closing(url, '[', ']') => {
url = &url[..url.len() - 1];
}
'}' if is_unbalanced_closing(url, '{', '}') => {
url = &url[..url.len() - 1];
}
')' if is_unbalanced_closing(url, '(', ')') => {
url = &url[..url.len() - ')'.len_utf8()];
}
']' if is_unbalanced_closing(url, '[', ']') => {
url = &url[..url.len() - ']'.len_utf8()];
}
'}' if is_unbalanced_closing(url, '{', '}') => {
url = &url[..url.len() - '}'.len_utf8()];
}
'》' if is_unbalanced_closing(url, '《', '》') => {
url = &url[..url.len() - '》'.len_utf8()];
}
'〉' if is_unbalanced_closing(url, '〈', '〉') => {
url = &url[..url.len() - '〉'.len_utf8()];
}
'」' if is_unbalanced_closing(url, '「', '」') => {
url = &url[..url.len() - '」'.len_utf8()];
}
'』' if is_unbalanced_closing(url, '『', '』') => {
url = &url[..url.len() - '』'.len_utf8()];
}
'】' if is_unbalanced_closing(url, '【', '】') => {
url = &url[..url.len() - '】'.len_utf8()];
}
'〕' if is_unbalanced_closing(url, '〔', '〕') => {
url = &url[..url.len() - '〕'.len_utf8()];
}
'〗' if is_unbalanced_closing(url, '〖', '〗') => {
url = &url[..url.len() - '〗'.len_utf8()];
}
'〙' if is_unbalanced_closing(url, '〘', '〙') => {
url = &url[..url.len() - '〙'.len_utf8()];
}
_ => break,
}
}
url
}
fn is_unbalanced_closing(url: &str, open: char, close: char) -> bool {
let open_count = url.chars().filter(|&c| c == open).count();
let close_count = url.chars().filter(|&c| c == close).count();
close_count > open_count
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic, missing_docs)]
mod tests {
use super::*;
#[test]
fn test_extract_link_facets_empty_and_no_links() {
assert!(extract_link_facets("").is_empty());
assert!(extract_link_facets(" ").is_empty());
assert!(extract_link_facets("Hello world! No links here.").is_empty());
assert!(extract_link_facets("Just http:// or https:// with no host").is_empty());
assert!(extract_link_facets("ftp://example.com is not http/https").is_empty());
}
#[test]
fn test_extract_link_facets_simple() {
let text = "Visit https://skybouncer.mike10010100.com/auth to activate";
let facets = extract_link_facets(text);
assert_eq!(facets.len(), 1);
let facet = &facets[0];
assert_eq!(
&text[facet.index.byte_start..facet.index.byte_end],
"https://skybouncer.mike10010100.com/auth"
);
match &facet.features[0] {
FacetFeature::Link { uri } => {
assert_eq!(uri, "https://skybouncer.mike10010100.com/auth");
}
_ => panic!("expected Link feature"),
}
}
#[test]
fn test_extract_link_facets_emojis_and_multibyte() {
let text =
"👋 Welcome to Skybouncer! Auth: https://skybouncer.mike10010100.com/auth 🎉 Enjoy!";
let facets = extract_link_facets(text);
assert_eq!(facets.len(), 1);
let facet = &facets[0];
assert_eq!(
&text[facet.index.byte_start..facet.index.byte_end],
"https://skybouncer.mike10010100.com/auth"
);
let char_count_before = text
.chars()
.take_while(|c| *c != 'h' || !text.contains("https://"))
.count();
assert!(facet.index.byte_start > char_count_before);
let text_adjacent = "Auth: 🔗https://skybouncer.mike10010100.com/auth";
let facets_adjacent = extract_link_facets(text_adjacent);
assert_eq!(facets_adjacent.len(), 1);
assert_eq!(
&text_adjacent[facets_adjacent[0].index.byte_start..facets_adjacent[0].index.byte_end],
"https://skybouncer.mike10010100.com/auth"
);
let text_zwj = "Leader 👨💻: https://skybouncer.mike10010100.com/auth";
let facets_zwj = extract_link_facets(text_zwj);
assert_eq!(facets_zwj.len(), 1);
assert_eq!(
&text_zwj[facets_zwj[0].index.byte_start..facets_zwj[0].index.byte_end],
"https://skybouncer.mike10010100.com/auth"
);
let text_rtl = "مرحبا \u{200F}https://skybouncer.mike10010100.com/auth";
let facets_rtl = extract_link_facets(text_rtl);
assert_eq!(facets_rtl.len(), 1);
assert_eq!(
&text_rtl[facets_rtl[0].index.byte_start..facets_rtl[0].index.byte_end],
"https://skybouncer.mike10010100.com/auth"
);
let text_cjk_direct = "请访问https://skybouncer.mike10010100.com/auth进行授权";
let facets_cjk_direct = extract_link_facets(text_cjk_direct);
assert_eq!(facets_cjk_direct.len(), 1);
assert_eq!(
&text_cjk_direct
[facets_cjk_direct[0].index.byte_start..facets_cjk_direct[0].index.byte_end],
"https://skybouncer.mike10010100.com/auth"
);
let text_jp_direct = "詳細はhttps://skybouncer.mike10010100.com/authです";
let facets_jp_direct = extract_link_facets(text_jp_direct);
assert_eq!(facets_jp_direct.len(), 1);
assert_eq!(
&text_jp_direct
[facets_jp_direct[0].index.byte_start..facets_jp_direct[0].index.byte_end],
"https://skybouncer.mike10010100.com/auth"
);
let text_upper = "Visit HTTPS://skybouncer.mike10010100.com/auth now";
let facets_upper = extract_link_facets(text_upper);
assert_eq!(facets_upper.len(), 1);
assert_eq!(
&text_upper[facets_upper[0].index.byte_start..facets_upper[0].index.byte_end],
"HTTPS://skybouncer.mike10010100.com/auth"
);
let text_mixed = "Visit Http://skybouncer.mike10010100.com/auth now";
let facets_mixed = extract_link_facets(text_mixed);
assert_eq!(facets_mixed.len(), 1);
assert_eq!(
&text_mixed[facets_mixed[0].index.byte_start..facets_mixed[0].index.byte_end],
"Http://skybouncer.mike10010100.com/auth"
);
let text_trailing_emoji = "Visit https://skybouncer.mike10010100.com/auth🚀 immediately!";
let facets_trailing_emoji = extract_link_facets(text_trailing_emoji);
assert_eq!(facets_trailing_emoji.len(), 1);
assert_eq!(
&text_trailing_emoji[facets_trailing_emoji[0].index.byte_start
..facets_trailing_emoji[0].index.byte_end],
"https://skybouncer.mike10010100.com/auth"
);
let text_invalid_prefix = "badprefixhttps://skybouncer.mike10010100.com/auth and 123https://skybouncer.mike10010100.com/auth";
assert!(extract_link_facets(text_invalid_prefix).is_empty());
}
#[test]
fn test_extract_link_facets_trailing_punctuation() {
let text = "Links: https://example.com/one., https://example.com/two! and https://example.com/three?";
let facets = extract_link_facets(text);
assert_eq!(facets.len(), 3);
assert_eq!(
&text[facets[0].index.byte_start..facets[0].index.byte_end],
"https://example.com/one"
);
assert_eq!(
&text[facets[1].index.byte_start..facets[1].index.byte_end],
"https://example.com/two"
);
assert_eq!(
&text[facets[2].index.byte_start..facets[2].index.byte_end],
"https://example.com/three"
);
}
#[test]
fn test_extract_link_facets_markdown_quotes_and_cjk_punctuation() {
let text = "Markdown: **https://example.com/bold**, *https://example.com/italic*, ~https://example.com/strike~, quotes: “https://example.com/quote”, ‘https://example.com/single’, angle: <https://example.com/angle>, ellipsis: https://example.com/more…, cjk: https://example.com/cjk。, brackets: 《https://example.com/book》, 【https://example.com/notice】, (https://example.com/fullparens), zwsp: https://example.com/hidden\u{200B}, percent: https://example.com/foo%20bar.";
let facets = extract_link_facets(text);
assert_eq!(facets.len(), 13);
let expected_urls = [
"https://example.com/bold",
"https://example.com/italic",
"https://example.com/strike",
"https://example.com/quote",
"https://example.com/single",
"https://example.com/angle",
"https://example.com/more",
"https://example.com/cjk",
"https://example.com/book",
"https://example.com/notice",
"https://example.com/fullparens",
"https://example.com/hidden",
"https://example.com/foo%20bar",
];
for (i, expected) in expected_urls.iter().enumerate() {
let slice = &text[facets[i].index.byte_start..facets[i].index.byte_end];
assert_eq!(slice, *expected, "failed at index {i}");
match &facets[i].features[0] {
FacetFeature::Link { uri } => assert_eq!(uri, *expected),
_ => panic!("expected Link feature"),
}
}
}
#[test]
fn test_extract_link_facets_parentheses_and_brackets() {
let text = "Auth URL is (https://example.com/auth) or [https://example.com/alt].";
let facets = extract_link_facets(text);
assert_eq!(facets.len(), 2);
assert_eq!(
&text[facets[0].index.byte_start..facets[0].index.byte_end],
"https://example.com/auth"
);
assert_eq!(
&text[facets[1].index.byte_start..facets[1].index.byte_end],
"https://example.com/alt"
);
}
#[test]
fn test_extract_link_facets_balanced_parentheses_in_url() {
let text = "Read https://en.wikipedia.org/wiki/Rust_(programming_language) today.";
let facets = extract_link_facets(text);
assert_eq!(facets.len(), 1);
assert_eq!(
&text[facets[0].index.byte_start..facets[0].index.byte_end],
"https://en.wikipedia.org/wiki/Rust_(programming_language)"
);
let text2 = "Read (https://en.wikipedia.org/wiki/Rust_(programming_language)) today.";
let facets2 = extract_link_facets(text2);
assert_eq!(facets2.len(), 1);
assert_eq!(
&text2[facets2[0].index.byte_start..facets2[0].index.byte_end],
"https://en.wikipedia.org/wiki/Rust_(programming_language)"
);
let text3 = "Check [Wikipedia](https://en.wikipedia.org/wiki/Rust_(programming_language)) for details.";
let facets3 = extract_link_facets(text3);
assert_eq!(facets3.len(), 1);
assert_eq!(
&text3[facets3[0].index.byte_start..facets3[0].index.byte_end],
"https://en.wikipedia.org/wiki/Rust_(programming_language)"
);
}
#[test]
fn test_extract_link_facets_adversarial_stress() {
let text_ip = "IPv4: http://127.0.0.1:8080/auth, IPv6: http://[::1]:9090/v1/auth?step=2";
let facets_ip = extract_link_facets(text_ip);
assert_eq!(facets_ip.len(), 2);
assert_eq!(
&text_ip[facets_ip[0].index.byte_start..facets_ip[0].index.byte_end],
"http://127.0.0.1:8080/auth"
);
assert_eq!(
&text_ip[facets_ip[1].index.byte_start..facets_ip[1].index.byte_end],
"http://[::1]:9090/v1/auth?step=2"
);
let text_heavy_unicode =
"Family 👨👩👧👦 flag 🇺🇸: https://example.com/family! Arabic مرحبا https://example.com/arabic?lang=ar#welcome Russian Привет: (https://example.com/russian).";
let facets_heavy = extract_link_facets(text_heavy_unicode);
assert_eq!(facets_heavy.len(), 3);
assert_eq!(
&text_heavy_unicode[facets_heavy[0].index.byte_start..facets_heavy[0].index.byte_end],
"https://example.com/family"
);
assert_eq!(
&text_heavy_unicode[facets_heavy[1].index.byte_start..facets_heavy[1].index.byte_end],
"https://example.com/arabic?lang=ar#welcome"
);
assert_eq!(
&text_heavy_unicode[facets_heavy[2].index.byte_start..facets_heavy[2].index.byte_end],
"https://example.com/russian"
);
for i in 0..facets_heavy.len() {
assert!(facets_heavy[i].index.byte_start < facets_heavy[i].index.byte_end);
if i > 0 {
assert!(facets_heavy[i - 1].index.byte_end <= facets_heavy[i].index.byte_start);
}
}
let text_punct =
"Check ((https://example.com/nested?q=1&b=2))... or [https://example.com/bracket]!";
let facets_punct = extract_link_facets(text_punct);
assert_eq!(facets_punct.len(), 2);
assert_eq!(
&text_punct[facets_punct[0].index.byte_start..facets_punct[0].index.byte_end],
"https://example.com/nested?q=1&b=2"
);
assert_eq!(
&text_punct[facets_punct[1].index.byte_start..facets_punct[1].index.byte_end],
"https://example.com/bracket"
);
}
}