diff --git a/packages/cultnet-rs/README.md b/packages/cultnet-rs/README.md index 664b7154..18cb3d7f 100644 --- a/packages/cultnet-rs/README.md +++ b/packages/cultnet-rs/README.md @@ -113,8 +113,8 @@ document without re-encoding the payload first. The receiving Rust type does not have to be the sender's concrete Rust type. If the local registry binds a different `DatabaseEntry` to the same schema id, -`apply_raw_snapshot_response::(...)` hydrates the local alias type -directly: +`sync_raw_document_from_snapshot_response::(...)` hydrates one local +alias type directly: ```rust let mut target_registry = CultNetDocumentRegistry::new(); @@ -123,9 +123,10 @@ target_registry.register(CultNetDocumentBinding::for_entry_with_schema_id::( +let note = target_registry.sync_raw_document_from_snapshot_response::( &mut target_cache, &snapshot_response, + "note:bridge", )?; ``` diff --git a/packages/cultnet-rs/src/replication.rs b/packages/cultnet-rs/src/replication.rs index 492cf317..1597bd26 100644 --- a/packages/cultnet-rs/src/replication.rs +++ b/packages/cultnet-rs/src/replication.rs @@ -352,6 +352,40 @@ impl CultNetDocumentRegistry { Ok(applied) } + pub fn sync_document_from_snapshot_response( + &self, + cache: &mut CultCache, + response: &CultNetMessage, + record_key: &str, + ) -> Result + where + T: DatabaseEntry + Serialize + DeserializeOwned, + { + let CultNetMessage::SnapshotResponse { documents, .. } = response else { + return Err(anyhow!("expected cultnet.snapshot_response.v0")); + }; + let binding = self.require_binding(T::TYPE)?; + let document = documents + .iter() + .find(|document| { + document.schema_id == binding.schema_id && document.record_key == record_key + }) + .ok_or_else(|| { + anyhow!( + "No CultNet snapshot document for schema {:?} and record key {:?}", + binding.schema_id, + record_key + ) + })?; + self.apply_document_put_message::( + cache, + &CultNetMessage::DocumentPut { + message_id: "snapshot-sync".to_string(), + document: document.clone(), + }, + ) + } + pub fn apply_raw_snapshot_response( &self, cache: &mut CultCache, @@ -380,6 +414,40 @@ impl CultNetDocumentRegistry { Ok(applied) } + pub fn sync_raw_document_from_snapshot_response( + &self, + cache: &mut CultCache, + response: &CultNetMessage, + record_key: &str, + ) -> Result + where + T: DatabaseEntry + Serialize + DeserializeOwned, + { + let CultNetMessage::SnapshotResponseRaw { documents, .. } = response else { + return Err(anyhow!("expected cultnet.snapshot_response_raw.v0")); + }; + let binding = self.require_binding(T::TYPE)?; + let document = documents + .iter() + .find(|document| { + document.schema_id == binding.schema_id && document.record_key == record_key + }) + .ok_or_else(|| { + anyhow!( + "No raw CultNet snapshot document for schema {:?} and record key {:?}", + binding.schema_id, + record_key + ) + })?; + self.apply_raw_document_put_message::( + cache, + &CultNetMessage::DocumentPutRaw { + message_id: "snapshot-sync-raw".to_string(), + document: document.clone(), + }, + ) + } + fn document_record_from_envelope( &self, envelope: &CultCacheEnvelope, diff --git a/packages/cultnet-rs/tests/cultnet.rs b/packages/cultnet-rs/tests/cultnet.rs index 39ea2e39..8ff65fa0 100644 --- a/packages/cultnet-rs/tests/cultnet.rs +++ b/packages/cultnet-rs/tests/cultnet.rs @@ -1630,7 +1630,13 @@ fn document_registry_replicates_typed_cultcache_state() -> Result<()> { target.pull_all_backing_stores()?; let applied = registry.apply_snapshot_response::(&mut target, &snapshot)?; + let synced = registry.sync_document_from_snapshot_response::( + &mut target, + &snapshot, + "epiphany.persona", + )?; assert_eq!(applied, vec![payload.clone()]); + assert_eq!(synced, payload); assert_eq!( target.get_required::("epiphany.persona")?, payload @@ -1739,19 +1745,23 @@ fn raw_snapshot_replication_hydrates_same_schema_rust_aliases() -> Result<()> { target.pull_all_backing_stores()?; let applied = alias_registry - .apply_raw_snapshot_response::(&mut target, &raw_snapshot)?; + .sync_raw_document_from_snapshot_response::( + &mut target, + &raw_snapshot, + "epiphany.persona", + )?; assert_eq!( applied, - vec![GhostlightAgentStateUiFixture { + GhostlightAgentStateUiFixture { schema_version: "ghostlight.agent_state.v0".to_string(), agent_id: "epiphany.persona".to_string(), display_name: "Persona".to_string(), - }] + } ); assert_eq!( target.get_required::("epiphany.persona")?, - applied[0] + applied ); Ok(()) }