cultcache-rs: name, index and global lookups from the C# reference
#[cultcache(name)], #[cultcache(index[ = alias])] and struct-level #[cultcache(global)] in the DatabaseEntry derive; get_by_name, get_by_index and get_global on CultCache; a second record of a global type under another key is a typed CultCacheSecondGlobal refusal on every write path and on load. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
b88beddecb
commit
c4fae30677
2 changed files with 328 additions and 0 deletions
|
|
@ -22,6 +22,7 @@ fn database_entry_codec(input: DeriveInput) -> syn::Result<proc_macro2::TokenStr
|
|||
let ident = input.ident.clone();
|
||||
let mut entry_type: Option<LitStr> = None;
|
||||
let mut schema_name: Option<LitStr> = None;
|
||||
let mut global = false;
|
||||
|
||||
for attribute in input
|
||||
.attrs
|
||||
|
|
@ -37,6 +38,12 @@ fn database_entry_codec(input: DeriveInput) -> syn::Result<proc_macro2::TokenStr
|
|||
once(&schema_name, &meta, "schema")?;
|
||||
schema_name = Some(meta.value()?.parse()?);
|
||||
Ok(())
|
||||
} else if meta.path.is_ident("global") {
|
||||
if global {
|
||||
return Err(meta.error("repeated `global`: #[cultcache(global)] is given once"));
|
||||
}
|
||||
global = true;
|
||||
Ok(())
|
||||
} else {
|
||||
Err(meta.error("unsupported cultcache attribute"))
|
||||
}
|
||||
|
|
@ -54,11 +61,40 @@ fn database_entry_codec(input: DeriveInput) -> syn::Result<proc_macro2::TokenStr
|
|||
|
||||
let slots = named_slots(input.data, &ident, "DatabaseEntry")?;
|
||||
let codec = slot_codec(&ident, &slots);
|
||||
let name_member = slots.iter().find(|slot| slot.name).map(|slot| &slot.ident);
|
||||
let name_fn = name_member.map(|member| {
|
||||
quote! {
|
||||
fn cult_name(&self) -> ::std::option::Option<::std::string::String> {
|
||||
::cultcache_rs::CultLookupValue::lookup_value(&self.#member)
|
||||
}
|
||||
}
|
||||
});
|
||||
let index_members = slots.iter().filter_map(|slot| slot.index.as_ref().map(|alias| (alias, &slot.ident)));
|
||||
let index_pushes = index_members.map(|(alias, member)| {
|
||||
quote! {
|
||||
if let ::std::option::Option::Some(value) = ::cultcache_rs::CultLookupValue::lookup_value(&self.#member) {
|
||||
held.push((#alias, value));
|
||||
}
|
||||
}
|
||||
});
|
||||
let index_pushes: Vec<_> = index_pushes.collect();
|
||||
let indexes_fn = (!index_pushes.is_empty()).then(|| {
|
||||
quote! {
|
||||
fn cult_indexes(&self) -> ::std::vec::Vec<(&'static str, ::std::string::String)> {
|
||||
let mut held = ::std::vec::Vec::new();
|
||||
#(#index_pushes)*
|
||||
held
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Ok(quote! {
|
||||
impl ::cultcache_rs::DatabaseEntry for #ident {
|
||||
const TYPE: &'static str = #entry_type;
|
||||
const SCHEMA_NAME: &'static str = #schema_name;
|
||||
const GLOBAL: bool = #global;
|
||||
#name_fn
|
||||
#indexes_fn
|
||||
}
|
||||
|
||||
#codec
|
||||
|
|
@ -133,6 +169,8 @@ fn named_slots(data: Data, ident: &syn::Ident, owner: &str) -> syn::Result<Vec<C
|
|||
let mut key: Option<usize> = None;
|
||||
let mut default = false;
|
||||
let mut bytes = false;
|
||||
let mut name = false;
|
||||
let mut index: Option<String> = None;
|
||||
for attribute in field
|
||||
.attrs
|
||||
.iter()
|
||||
|
|
@ -157,6 +195,22 @@ fn named_slots(data: Data, ident: &syn::Ident, owner: &str) -> syn::Result<Vec<C
|
|||
} else if meta.path.is_ident("bytes") {
|
||||
bytes = true;
|
||||
Ok(())
|
||||
} else if owner == "DatabaseEntry" && meta.path.is_ident("name") {
|
||||
if name {
|
||||
return Err(meta.error("repeated `name`: #[cultcache(name)] is given once"));
|
||||
}
|
||||
name = true;
|
||||
Ok(())
|
||||
} else if owner == "DatabaseEntry" && meta.path.is_ident("index") {
|
||||
once(&index, &meta, "index")?;
|
||||
// A bare `index`, or an alias that is blank, names the field itself.
|
||||
let alias = if meta.input.peek(syn::Token![=]) {
|
||||
meta.value()?.parse::<LitStr>()?.value()
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
index = Some(if alias.trim().is_empty() { field_ident.to_string() } else { alias });
|
||||
Ok(())
|
||||
} else {
|
||||
Err(meta.error("unsupported field cultcache attribute"))
|
||||
}
|
||||
|
|
@ -180,8 +234,28 @@ fn named_slots(data: Data, ident: &syn::Ident, owner: &str) -> syn::Result<Vec<C
|
|||
ty: field.ty,
|
||||
default,
|
||||
bytes,
|
||||
name,
|
||||
index,
|
||||
});
|
||||
}
|
||||
if let Some(second) = slots.iter().filter(|slot| slot.name).nth(1) {
|
||||
return Err(syn::Error::new_spanned(
|
||||
&second.ident,
|
||||
format!("{owner} declares #[cultcache(name)] on more than one member; a record has one name"),
|
||||
));
|
||||
}
|
||||
for (position, slot) in slots.iter().enumerate() {
|
||||
let Some(alias) = &slot.index else { continue };
|
||||
if let Some(first) = slots[..position].iter().find(|earlier| earlier.index.as_ref() == Some(alias)) {
|
||||
return Err(syn::Error::new_spanned(
|
||||
&slot.ident,
|
||||
format!(
|
||||
"{owner} members {} and {} both declare index alias \"{alias}\"; an index alias must name one member",
|
||||
first.ident, slot.ident
|
||||
),
|
||||
));
|
||||
}
|
||||
}
|
||||
slots.sort_by_key(|slot| slot.key);
|
||||
Ok(slots)
|
||||
}
|
||||
|
|
@ -367,6 +441,10 @@ struct CultCacheSlot {
|
|||
ty: Type,
|
||||
default: bool,
|
||||
bytes: bool,
|
||||
/// `#[cultcache(name)]`: the member `get_by_name` finds the record by.
|
||||
name: bool,
|
||||
/// `#[cultcache(index)]` or `#[cultcache(index = "alias")]`: the alias `get_by_index` finds the record by.
|
||||
index: Option<String>,
|
||||
}
|
||||
|
||||
fn union_codec(input: DeriveInput) -> syn::Result<proc_macro2::TokenStream> {
|
||||
|
|
@ -842,4 +920,89 @@ mod tests {
|
|||
let input: DeriveInput = syn::parse_str(source).expect("the fixture parses");
|
||||
named_slots(input.data, &input.ident, "CultSlots").expect("the fixture is accepted");
|
||||
}
|
||||
fn entry_error(source: &str) -> String {
|
||||
let input: DeriveInput = syn::parse_str(source).expect("the fixture parses");
|
||||
database_entry_codec(input).expect_err("the fixture is refused").to_string()
|
||||
}
|
||||
|
||||
fn entry_tokens(source: &str) -> String {
|
||||
let input: DeriveInput = syn::parse_str(source).expect("the fixture parses");
|
||||
database_entry_codec(input).expect("the fixture is accepted").to_string().replace(' ', "")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn name_index_and_global_generate_the_lookup_surface() {
|
||||
let tokens = entry_tokens(
|
||||
r#"#[cultcache(type = "t", global)] struct S {
|
||||
#[cultcache(key = 0, name)] label: String,
|
||||
#[cultcache(key = 1, index)] code: String,
|
||||
#[cultcache(key = 2, index = "sku")] stock: Option<String>,
|
||||
}"#,
|
||||
);
|
||||
assert!(tokens.contains("constGLOBAL:bool=true"), "{tokens}");
|
||||
assert!(tokens.contains("fncult_name"), "{tokens}");
|
||||
assert!(tokens.contains("(\"code\",value)"), "{tokens}");
|
||||
assert!(tokens.contains("(\"sku\",value)"), "{tokens}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_record_without_lookups_generates_none_of_them() {
|
||||
let tokens = entry_tokens(r#"#[cultcache(type = "t")] struct S { #[cultcache(key = 0)] a: String }"#);
|
||||
assert!(tokens.contains("constGLOBAL:bool=false"), "{tokens}");
|
||||
assert!(!tokens.contains("fncult_name") && !tokens.contains("fncult_indexes"), "{tokens}");
|
||||
}
|
||||
|
||||
// ResolveIndexAlias: a blank alias is the member's name.
|
||||
#[test]
|
||||
fn a_blank_index_alias_is_the_member_name() {
|
||||
let tokens = entry_tokens(
|
||||
r#"#[cultcache(type = "t")] struct S { #[cultcache(key = 0, index = " ")] code: String }"#,
|
||||
);
|
||||
assert!(tokens.contains("(\"code\",value)"), "{tokens}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn two_names_are_refused_for_that_reason() {
|
||||
let message = entry_error(
|
||||
r#"#[cultcache(type = "t")] struct S { #[cultcache(key = 0, name)] a: String, #[cultcache(key = 1, name)] b: String }"#,
|
||||
);
|
||||
assert!(message.contains("more than one member"), "{message}");
|
||||
}
|
||||
|
||||
// D10: two members that declare one alias; a bare index is the member name, so it collides with an explicit alias of it.
|
||||
#[test]
|
||||
fn two_members_with_one_index_alias_are_refused_for_that_reason() {
|
||||
let message = entry_error(
|
||||
r#"#[cultcache(type = "t")] struct S { #[cultcache(key = 0, index = "k")] a: String, #[cultcache(key = 1, index = "k")] b: String }"#,
|
||||
);
|
||||
assert!(message.contains("both declare index alias \"k\""), "{message}");
|
||||
let message = entry_error(
|
||||
r#"#[cultcache(type = "t")] struct S { #[cultcache(key = 0, index)] a: String, #[cultcache(key = 1, index = "a")] b: String }"#,
|
||||
);
|
||||
assert!(message.contains("both declare index alias \"a\""), "{message}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_repeated_lookup_attribute_is_refused() {
|
||||
let message = entry_error(
|
||||
r#"#[cultcache(type = "t")] struct S { #[cultcache(key = 0, name, name)] a: String }"#,
|
||||
);
|
||||
assert!(message.contains("repeated `name`"), "{message}");
|
||||
let message = entry_error(
|
||||
r#"#[cultcache(type = "t")] struct S { #[cultcache(key = 0, index, index = "x")] a: String }"#,
|
||||
);
|
||||
assert!(message.contains("repeated `index`"), "{message}");
|
||||
let message = entry_error(r#"#[cultcache(type = "t", global, global)] struct S { #[cultcache(key = 0)] a: String }"#);
|
||||
assert!(message.contains("repeated `global`"), "{message}");
|
||||
}
|
||||
|
||||
// Only a DatabaseEntry is looked up: a nested slot record carrying the attributes is refused as before.
|
||||
#[test]
|
||||
fn lookup_attributes_are_refused_on_a_slots_record() {
|
||||
let message = slots_error(r#"struct S { #[cultcache(key = 0, name)] a: String }"#);
|
||||
assert!(message.contains("unsupported field cultcache attribute"), "{message}");
|
||||
let message = slots_error(r#"struct S { #[cultcache(key = 0, index)] a: String }"#);
|
||||
assert!(message.contains("unsupported field cultcache attribute"), "{message}");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -162,8 +162,68 @@ pub use cultcache_rs_derive::DatabaseEntry;
|
|||
pub trait DatabaseEntry: Serialize + DeserializeOwned + Clone + Send + 'static {
|
||||
const TYPE: &'static str;
|
||||
const SCHEMA_NAME: &'static str = "DatabaseEntry";
|
||||
/// `#[cultcache(global)]`: the cache holds at most one record of this type, found by `get_global`.
|
||||
const GLOBAL: bool = false;
|
||||
/// The `#[cultcache(name)]` member's value, which `get_by_name` finds the record by. `None` is no name.
|
||||
fn cult_name(&self) -> Option<String> {
|
||||
None
|
||||
}
|
||||
/// Each `#[cultcache(index)]` member as (alias, value), which `get_by_index` finds the record by.
|
||||
fn cult_indexes(&self) -> Vec<(&'static str, String)> {
|
||||
Vec::new()
|
||||
}
|
||||
}
|
||||
|
||||
/// What a `#[cultcache(name)]` or `#[cultcache(index)]` member says it is called: its text, or `None` for a member
|
||||
/// that holds no value (an `Option` that is `None`). The cache treats an empty name and a blank index value as none.
|
||||
pub trait CultLookupValue {
|
||||
fn lookup_value(&self) -> Option<String>;
|
||||
}
|
||||
|
||||
impl CultLookupValue for String {
|
||||
fn lookup_value(&self) -> Option<String> {
|
||||
Some(self.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: CultLookupValue> CultLookupValue for Option<T> {
|
||||
fn lookup_value(&self) -> Option<String> {
|
||||
self.as_ref().and_then(CultLookupValue::lookup_value)
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! lookup_value_by_display {
|
||||
($($ty:ty),* $(,)?) => {$(
|
||||
impl CultLookupValue for $ty {
|
||||
fn lookup_value(&self) -> Option<String> {
|
||||
Some(self.to_string())
|
||||
}
|
||||
}
|
||||
)*};
|
||||
}
|
||||
|
||||
lookup_value_by_display!(bool, char, i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize);
|
||||
|
||||
/// A global type already holds a record under another key; a second would leave `get_global` with two answers.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct CultCacheSecondGlobal {
|
||||
pub schema_name: String,
|
||||
pub existing_key: String,
|
||||
pub key: String,
|
||||
}
|
||||
|
||||
impl std::fmt::Display for CultCacheSecondGlobal {
|
||||
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(
|
||||
formatter,
|
||||
"{} is a global and already has record {}; {} would be a second.",
|
||||
self.schema_name, self.existing_key, self.key
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for CultCacheSecondGlobal {}
|
||||
|
||||
/// A type written as a positional slots array: what the `CultSlots` and `DatabaseEntry`
|
||||
/// derives implement, and what a `CultUnion` arm must carry.
|
||||
#[diagnostic::on_unimplemented(
|
||||
|
|
@ -2655,6 +2715,8 @@ struct CultCacheStoreRegistration {
|
|||
|
||||
pub struct CultCache {
|
||||
definitions: BTreeMap<String, &'static str>,
|
||||
/// The registered types declared `#[cultcache(global)]`.
|
||||
globals: BTreeSet<String>,
|
||||
schema_name_definitions: BTreeMap<String, String>,
|
||||
entries: BTreeMap<(String, String), CultCacheEnvelope>,
|
||||
stores: Vec<CultCacheStoreRegistration>,
|
||||
|
|
@ -2664,6 +2726,7 @@ impl CultCache {
|
|||
pub fn new() -> Self {
|
||||
Self {
|
||||
definitions: BTreeMap::new(),
|
||||
globals: BTreeSet::new(),
|
||||
schema_name_definitions: BTreeMap::new(),
|
||||
entries: BTreeMap::new(),
|
||||
stores: Vec::new(),
|
||||
|
|
@ -2694,6 +2757,11 @@ impl CultCache {
|
|||
));
|
||||
}
|
||||
self.definitions.insert(T::TYPE.to_string(), T::SCHEMA_NAME);
|
||||
if T::GLOBAL {
|
||||
self.globals.insert(T::TYPE.to_string());
|
||||
} else {
|
||||
self.globals.remove(T::TYPE);
|
||||
}
|
||||
self.schema_name_definitions
|
||||
.insert(T::SCHEMA_NAME.to_string(), T::TYPE.to_string());
|
||||
Ok(())
|
||||
|
|
@ -2800,6 +2868,7 @@ impl CultCache {
|
|||
loaded.insert(entry_id(&entry), entry);
|
||||
}
|
||||
}
|
||||
self.refuse_second_globals(&loaded)?;
|
||||
self.entries = loaded;
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -2883,6 +2952,65 @@ impl CultCache {
|
|||
Ok(values)
|
||||
}
|
||||
|
||||
/// The one record of a `#[cultcache(global)]` type, or `None` before it is written (and for a type that is
|
||||
/// not a global).
|
||||
pub fn get_global<T: DatabaseEntry>(&self) -> Result<Option<T>> {
|
||||
self.require_entry_type::<T>()?;
|
||||
if !T::GLOBAL {
|
||||
return Ok(None);
|
||||
}
|
||||
self.find_record::<T>(|_| true)
|
||||
}
|
||||
|
||||
/// The record whose `#[cultcache(name)]` member is `name`. An empty name is no name, so it finds nothing. Records
|
||||
/// of one type sharing a name find the one with the smallest key (ordinal), whatever order they were written or
|
||||
/// loaded in; removing it finds the next.
|
||||
pub fn get_by_name<T: DatabaseEntry>(&self, name: &str) -> Result<Option<T>> {
|
||||
self.require_entry_type::<T>()?;
|
||||
if name.is_empty() {
|
||||
return Ok(None);
|
||||
}
|
||||
self.find_record::<T>(|document| document.cult_name().as_deref() == Some(name))
|
||||
}
|
||||
|
||||
/// The record whose `#[cultcache(index)]` member `alias` holds `value`. A blank value is not indexed, so it finds
|
||||
/// nothing. Records of one type sharing a value find the one with the smallest key (ordinal), whatever order they
|
||||
/// were written or loaded in; removing it finds the next.
|
||||
pub fn get_by_index<T: DatabaseEntry>(&self, alias: &str, value: &str) -> Result<Option<T>> {
|
||||
self.require_entry_type::<T>()?;
|
||||
if value.trim().is_empty() {
|
||||
return Ok(None);
|
||||
}
|
||||
self.find_record::<T>(|document| {
|
||||
document
|
||||
.cult_indexes()
|
||||
.iter()
|
||||
.any(|(held_alias, held_value)| *held_alias == alias && held_value == value)
|
||||
})
|
||||
}
|
||||
|
||||
/// The first record of `T`, in key order, that `matches`.
|
||||
fn find_record<T: DatabaseEntry>(&self, matches: impl Fn(&T) -> bool) -> Result<Option<T>> {
|
||||
let held = self
|
||||
.entries
|
||||
.range((T::TYPE.to_string(), String::new())..)
|
||||
.take_while(|((held_type, _), _)| held_type == T::TYPE);
|
||||
for (_, entry) in held {
|
||||
let document: T = decode_payload(&entry.payload).with_context(|| {
|
||||
format!(
|
||||
"failed to decode CultCache entry {:?} at key {:?} as {}",
|
||||
T::TYPE,
|
||||
entry.key,
|
||||
T::SCHEMA_NAME
|
||||
)
|
||||
})?;
|
||||
if matches(&document) {
|
||||
return Ok(Some(document));
|
||||
}
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
pub fn soa<T: SoaDocument>(&self) -> Result<CultSoaTable<T>> {
|
||||
let keyed_rows = self.get_all_with_keys::<T>()?;
|
||||
let (keys, documents): (Vec<_>, Vec<_>) = keyed_rows.into_iter().unzip();
|
||||
|
|
@ -2900,6 +3028,7 @@ impl CultCache {
|
|||
|
||||
pub fn put<T: DatabaseEntry>(&mut self, key: impl Into<String>, value: &T) -> Result<T> {
|
||||
let (entry, parsed) = self.prepare_entry(key, value)?;
|
||||
self.refuse_second_global(&self.entries, &entry.r#type, &entry.key)?;
|
||||
if let Some(home) = self.home_index(T::TYPE)? {
|
||||
self.stores[home].store.push(&entry)?;
|
||||
}
|
||||
|
|
@ -3011,6 +3140,7 @@ impl CultCache {
|
|||
for entry in &staged {
|
||||
next.insert(entry_id(entry), entry.clone());
|
||||
}
|
||||
self.refuse_second_globals(&next)?;
|
||||
if let Some(store_index) = store_index {
|
||||
// The home store receives its own records only: those it already
|
||||
// holds in this cache plus the staged ones.
|
||||
|
|
@ -3029,6 +3159,7 @@ impl CultCache {
|
|||
|
||||
pub fn put_envelope<T: DatabaseEntry>(&mut self, entry: CultCacheEnvelope) -> Result<T> {
|
||||
let parsed = self.validate_envelope::<T>(&entry)?;
|
||||
self.refuse_second_global(&self.entries, &entry.r#type, &entry.key)?;
|
||||
if let Some(home) = self.home_index(T::TYPE)? {
|
||||
self.stores[home].store.push(&entry)?;
|
||||
}
|
||||
|
|
@ -3061,6 +3192,7 @@ impl CultCache {
|
|||
));
|
||||
}
|
||||
require_payload_depth(&entry)?;
|
||||
self.refuse_second_global(&self.entries, &entry.r#type, &entry.key)?;
|
||||
if let Some(home) = self.home_index(&entry.r#type)? {
|
||||
self.stores[home].store.push(&entry)?;
|
||||
}
|
||||
|
|
@ -3146,10 +3278,43 @@ impl CultCache {
|
|||
self.home_index(&entry.r#type)?;
|
||||
loaded.insert(entry_id(&entry), entry);
|
||||
}
|
||||
self.refuse_second_globals(&loaded)?;
|
||||
self.entries = loaded;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn refuse_second_global(
|
||||
&self,
|
||||
entries: &BTreeMap<(String, String), CultCacheEnvelope>,
|
||||
type_id: &str,
|
||||
key: &str,
|
||||
) -> Result<()> {
|
||||
if !self.globals.contains(type_id) {
|
||||
return Ok(());
|
||||
}
|
||||
let held_other = entries
|
||||
.range((type_id.to_string(), String::new())..)
|
||||
.take_while(|((held_type, _), _)| held_type == type_id)
|
||||
.map(|((_, held_key), _)| held_key)
|
||||
.find(|held_key| held_key.as_str() != key);
|
||||
match held_other {
|
||||
Some(existing_key) => Err(CultCacheSecondGlobal {
|
||||
schema_name: self.definitions.get(type_id).copied().unwrap_or(type_id).to_string(),
|
||||
existing_key: existing_key.clone(),
|
||||
key: key.to_string(),
|
||||
}
|
||||
.into()),
|
||||
None => Ok(()),
|
||||
}
|
||||
}
|
||||
|
||||
fn refuse_second_globals(&self, entries: &BTreeMap<(String, String), CultCacheEnvelope>) -> Result<()> {
|
||||
for (type_id, key) in entries.keys() {
|
||||
self.refuse_second_global(entries, type_id, key)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn require_entry_type<T: DatabaseEntry>(&self) -> Result<()> {
|
||||
match self.definitions.get(T::TYPE) {
|
||||
Some(schema_name) if *schema_name == T::SCHEMA_NAME => Ok(()),
|
||||
|
|
|
|||
Loading…
Reference in a new issue