From 6d129ebffc7c566d30c6ae7c2f9235f70b6fc1be Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 27 Nov 2026 23:23:57 +0100 Subject: [PATCH] cultcache-rs: a hand impl that describes no rows keeps the file's rows; test type-level refusal and bytes vs type_name Co-Authored-By: Claude Sonnet 5.5 --- .../cultcache-rs-derive/src/lib.rs | 17 ++++++++++ packages/cultcache-rs/src/lib.rs | 34 +++++++++++++++++-- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/packages/cultcache-rs/cultcache-rs-derive/src/lib.rs b/packages/cultcache-rs/cultcache-rs-derive/src/lib.rs index f4811df5..10442711 100644 --- a/packages/cultcache-rs/cultcache-rs-derive/src/lib.rs +++ b/packages/cultcache-rs/cultcache-rs-derive/src/lib.rs @@ -1177,4 +1177,21 @@ mod tests { assert!(tokens.find("\"Ident\"").unwrap() < tokens.find("\"RawBytes\"").unwrap(), "key order: {tokens}"); } + + #[test] + fn a_type_level_attribute_other_than_type_name_is_refused_on_slots_and_unions() { + let message = slots_result(r#"#[cultcache(type = "t")] struct S { #[cultcache(key = 0)] a: String }"#).unwrap_err().to_string(); + assert!(message.contains("unsupported cultcache attribute"), "{message}"); + let input: DeriveInput = syn::parse_str(r#"#[cultcache(global)] enum U { #[cultcache(key = 0)] A }"#).unwrap(); + let message = union_codec(input).unwrap_err().to_string(); + assert!(message.contains("unsupported cultcache attribute"), "{message}"); + } + + #[test] + fn an_explicit_type_name_wins_over_bytes_and_bytes_names_a_byte_array() { + let tokens = entry_tokens(r#"#[cultcache(type = "t")] struct S { #[cultcache(key = 0, bytes, type_name = "Ns.Blob")] a: Vec }"#); + assert!(tokens.contains("\"Ns.Blob\"") && !tokens.contains("\"System.Byte[]\""), "{tokens}"); + let tokens = entry_tokens(r#"#[cultcache(type = "t")] struct S { #[cultcache(key = 0, bytes)] a: Vec }"#); + assert!(tokens.contains("\"System.Byte[]\""), "{tokens}"); + } } diff --git a/packages/cultcache-rs/src/lib.rs b/packages/cultcache-rs/src/lib.rs index 7dd6c73c..0d4cf810 100644 --- a/packages/cultcache-rs/src/lib.rs +++ b/packages/cultcache-rs/src/lib.rs @@ -4881,7 +4881,7 @@ fn encode_store_snapshot( })); } } - // An id a registered type owns is described from that type at every write, whatever the file holds. An entry the file holds for + // An id a registered type owns and describes (a hand impl describes no rows) is described from that type at every write, whatever the file holds. An entry the file holds for // any other id is written back as the file holds it: this runtime keeps no description of another runtime's schema, so it // never replaces one. It describes such an id itself only when the file has no entry for it, or when a registered type owns // the id under another name (a rename, where the registered type names the id). @@ -4893,7 +4893,7 @@ fn encode_store_snapshot( let mut catalog = BTreeMap::::new(); for (schema_id, document_type) in named { if schema_id == document_type - && let Some(members) = described_types.get(&schema_id) + && let Some(members) = described_types.get(&schema_id).filter(|members| !members.is_empty()) { let entry = described(&schema_id, document_type, members); catalog.entry(entry.0.clone()).or_insert(entry); @@ -8396,6 +8396,36 @@ mod tests { Ok(()) } + #[derive(Clone, serde::Serialize, serde::Deserialize)] + struct HandWritten { + a: String, + } + + impl DatabaseEntry for HandWritten { + const TYPE: &'static str = "handnote"; + } + + #[test] + fn a_hand_impl_that_describes_no_rows_keeps_the_rows_the_file_holds() -> Result<()> { + let temp = tempfile::tempdir()?; + let path = temp.path().join("store.cc"); + let row = note_rows().remove(0); + let held = PersistedSchemaCatalogEntry( + "handnote".into(), "handnote".into(), "handnote.v1".into(), "handnote".into(), "{}".into(), vec!["handnote".into()], vec![row.clone()], + ); + let record = PersistedRecord("carried".into(), "handnote".into(), "2026-09-30T00:00:00Z".into(), rmp_serde::to_vec(&HandWritten { a: "x".into() })?); + std::fs::write(&path, rmp_serde::to_vec(&PersistedStoreSnapshot("cultcache.store.v1".into(), vec![held], vec![record]))?)?; + let mut cache = CultCache::new(); + cache.register_entry_type::()?; + cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&path))?; + cache.pull_all_backing_stores()?; + cache.put("another", &HandWritten { a: "y".into() })?; + let catalog = written_catalog(&path)?; + assert_eq!(catalog.len(), 1); + assert_eq!(catalog[0].6, vec![row], "one row before, one row after"); + Ok(()) + } + #[test] fn a_type_no_registration_describes_gets_members_empty() -> Result<()> { let temp = tempfile::tempdir()?;