//! Quoted-reply stripping, ported here because its permanent home does not//! exist yet.//!//! STRIP_QUOTED_REPLY is `packages/store/src/protocol/emailQuote.ts`. It belongs//! beside the email fingerprint, at//! `crates/cotton-store/src/protocol/email_quote.rs` — MOVE IT THERE rather//! than writing a second copy, because it has TWO callers in the TypeScript//! (the ingest here, and the one-time re-normalize `emailRestrip.test.ts`//! drives) and two spellings of "where does the quoted history begin" is a body//! stored one way and re-stripped another.//!//! A reply carries the whole conversation below it — the client pastes the//! prior thread under the new words. Cotton keeps every email in a thread as//! its OWN row, so that pasted history is pure duplication: it bloats the//! archive, drowns search, and makes a body read like the entire thread instead//! of the one new message.//!//! Deliberately conservative. It cuts only at unambiguous, client-generated//! quote boundaries, never at a signature or a paragraph, and it returns the//! body UNCHANGED when it finds no boundary — which is what lets a backfill//! leave already-clean rows untouched, writing no op for them. Idempotent://! stripping an already-stripped body is a no-op.use regex::Regex;use std::sync::OnceLock;/// A run of underscores/dashes on its own line — the divider Outlook/OWA and/// Gmail-web insert just before the quoted `From:` header.fn divider() -> &'staticRegex {static RE: OnceLock<Regex> = OnceLock::new(); RE.get_or_init(|| Regex::new(r"^\s*[_-]{5,}\s*$").expect("a literal pattern"))}/// "-----Original Message-----" (Outlook, no divider line of its own).fn original_message() -> &'staticRegex {static RE: OnceLock<Regex> = OnceLock::new(); RE.get_or_init(|| {Regex::new(r"(?i)^\s*-{2,}\s*original message\s*-{2,}\s*$").expect("a literal pattern") })}fn header_from() -> &'staticRegex {static RE: OnceLock<Regex> = OnceLock::new(); RE.get_or_init(|| Regex::new(r"^\s*>?\s*From:\s?\S").expect("a literal pattern"))}fn header_sent_or_date() -> &'staticRegex {static RE: OnceLock<Regex> = OnceLock::new(); RE.get_or_init(|| Regex::new(r"^\s*>?\s*(Sent|Date):\s?\S").expect("a literal pattern"))}/// The start of a Gmail-style attribution: `On <something>`.fn on_start() -> &'staticRegex {static RE: OnceLock<Regex> = OnceLock::new(); RE.get_or_init(|| Regex::new(r"^\s*>?\s*On\b").expect("a literal pattern"))}fn wrote_end() -> &'staticRegex {static RE: OnceLock<Regex> = OnceLock::new(); RE.get_or_init(|| Regex::new(r"\bwrote:\s*$").expect("a literal pattern"))}/// A `>`-quoted line — the plain-text quoting convention.fn quoted_line() -> &'staticRegex {static RE: OnceLock<Regex> = OnceLock::new(); RE.get_or_init(|| Regex::new(r"^\s*>").expect("a literal pattern"))}/// How many lines a wrapped `On … wrote:` attribution may span.const ON_WRAP_LOOKAHEAD: usize = 4;/// The 0-based index of the first line that begins quoted history, or `None`/// when the body carries none. Scans top-down and answers the earliest/// boundary.fn quote_boundary(lines: &[&str]) -> Option<usize> {for (index, line) in lines.iter().enumerate() {if divider().is_match(line) || original_message().is_match(line) {returnSome(index); }// Outlook header block with no divider: `From:` immediately (within two// lines) followed by `Sent:`/`Date:` — the PAIR, so a sentence that// merely starts "From the data…" is never mistaken for a header.if header_from().is_match(line) {for offset in1..=2 {ifletSome(next) = lines.get(index + offset) {if header_sent_or_date().is_match(next) {returnSome(index); } } } }// Gmail attribution. Require the terminal `wrote:` (on this line or// within a short wrap) so ordinary prose beginning "On Monday…" is// never cut.if on_start().is_match(line) {letmut joined = (*line).to_string();let end = (index + ON_WRAP_LOOKAHEAD).min(lines.len());for (step, next) in lines[index..end].iter().enumerate() {if step > 0 { joined = format!("{joined} {next}"); }if wrote_end().is_match(joined.trim_end()) {returnSome(index); } } }// A contiguous quoted region: the first `>`-prefixed line.if quoted_line().is_match(line) {returnSome(index); } }None}/// `body` with any trailing quoted-reply history removed.////// Unchanged when there is no quote boundary. When it cuts, it keeps everything/// above the boundary and trims trailing blank space. Never answers empty for a/// non-empty input: if the cut would blank the body (a body that is nothing but/// quoted history), the original is kept — the archive never loses a body to a/// formatting guess.pubfn strip_quoted_reply(body: &str) -> String {if body.is_empty() {returnString::new(); }// `body.split(/\r?\n/)` — a lone `\r` is NOT a break, so this splits on// `\n` and drops one trailing `\r` per line rather than using `lines()`,// which treats `\r\n` and `\n` the same but also refuses a final empty// line the JavaScript keeps.let lines: Vec<&str> = body .split('\n') .map(|line| line.strip_suffix('\r').unwrap_or(line)) .collect();letSome(boundary) = quote_boundary(&lines) else {return body.to_string(); };let kept = lines[..boundary].join("\n");let kept = kept.trim_end_matches(cotton_store::protocol::js::is_js_whitespace);if kept.is_empty() { body.to_string() } else { kept.to_string() }}#[cfg(test)]#[path = "engine_shim_tests.rs"]mod tests;