From f41697c800976c81766d749f67a62779e2c05093 Mon Sep 17 00:00:00 2001 From: TheGeneralist <180094941+thegeneralist01@users.noreply.github.com> Date: Wed, 21 Oct 2026 20:45:35 +0200 Subject: [PATCH] fix(twitter): use Cow for tweet metadata to deserialize escaped JSON strings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When tweet text contains JSON escape sequences (such as newlines \n, escaped quotes \", or unicode escapes), serde_json cannot borrow a &str slice from the input string because unescaping requires allocation. Deserializing to borrowed &str returned an error which caused tweet_metadata_from_json to silently fall back to Default::default(), producing titles like "Thread by @unknown" or "Tweet — @unknown". Using std::borrow::Cow<\x27a, str> allows serde_json to borrow unescaped slices with zero allocation while cleanly allocating for strings with escaped characters. --- crates/archivr-core/src/capture.rs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/crates/archivr-core/src/capture.rs b/crates/archivr-core/src/capture.rs index ba66594..b974bf5 100644 --- a/crates/archivr-core/src/capture.rs +++ b/crates/archivr-core/src/capture.rs @@ -971,7 +971,7 @@ struct TweetJsonMetadata<'a> { #[serde(borrow)] author: Option>, #[serde(borrow)] - full_text: Option<&'a str>, + full_text: Option>, #[serde(borrow)] article: Option>, } @@ -979,13 +979,13 @@ struct TweetJsonMetadata<'a> { #[derive(serde::Deserialize)] struct TweetAuthorMetadata<'a> { #[serde(borrow)] - screen_name: Option<&'a str>, + screen_name: Option>, } #[derive(serde::Deserialize)] struct TweetArticleMetadata<'a> { #[serde(borrow)] - title: Option<&'a str>, + title: Option>, } fn tweet_metadata_from_json(json_str: &str) -> PlatformMetadata { @@ -3776,6 +3776,20 @@ mod tests { ); assert_eq!(meta.title, None); } + #[test] + fn tweet_title_extracted_from_json_with_escapes() { + let json = r#"{ + "full_text": "First line\n\nSecond line with \"quotes\"", + "author": { "screen_name": "rustacean", "name": "The Rustacean" } + }"#; + let meta = tweet_metadata_from_json(json); + assert_eq!(meta.author, Some("rustacean".to_string())); + assert_eq!( + meta.caption, + Some("First line\n\nSecond line with \"quotes\"".to_string()) + ); + assert_eq!(meta.title, None); + } #[test] fn tweet_prefers_article_title() {