//! The server's half of `MessageWriteSink`.//!//! The port of `packages/server/src/messages/sink.ts`.//!//! [`cotton_core::messages::apply_message_write`] lives in core because the//! PHONE calls it too — one implementation of what writing a message does to a//! thread, so a row composed on a phone, a row composed here and a row the//! relay mirrors cannot drift. Core therefore cannot know what it is writing//! INTO, and takes a sink of row-level changes; this turns those into writes on//! the op writer.//!//! The TypeScript's sink makes the insert-or-update decision itself, and says//! why: PostgREST's upsert path fills in a default `created_at`, so upserting a//! thread's two-column denorm patch would stamp the thread as created today//! every time somebody messaged. There is no such hazard here and so no such//! branch — a [`Writer`] upsert is an OP carrying exactly the columns in the//! patch, and the fold applies exactly those. The decision the TypeScript had//! to make is one this transport does not pose.use cotton_store::outbox::LocalChange;use cotton_store::protocol::types::OpKind;usecrate::mcp::writer::Writer;/// Queue one batch of planned changes onto the writer.////// It does NOT commit: the caller decides what else belongs in the same op, and/// the batch reaching the writer exactly once is `apply_message_write`'s/// guarantee, not this function's.////// Fallible for one reason only: a planned delete. Core's planner plans no/// delete — a message write inserts and patches — and a delete has to say/// which of the five kinds it is, which a planned row change cannot. So a/// `Delete` here is refused before anything of the batch is queued, rather/// than minted as a delete that names no kind. Everything else is memory; the/// failure that matters — the commit — happens once, after, where the caller/// can name it.pubfn queue_changes(writer: &mutWriter<'_>, changes: &[LocalChange]) -> Result<(), String> {ifletSome(change) = changes.iter().find(|change| change.kind == OpKind::Delete) {returnErr(format!("the message planner plans no delete; a delete here must declare its kind \ ({} {})", change.table, change.row_id )); }for change in changes {// `plan_message_write` never plans a patchless upsert; a change that// carried none would be an op claiming a row and saying nothing about// it, which the fold would apply as an empty update. Skipping is the// TypeScript's own `patch ?? {}` read one step earlier.ifletSome(patch) = change.patch.as_ref() { writer.upsert(&change.table, &change.row_id, patch.clone()); } }Ok(())}