//! Immutable item templates and strict stack validation. Template component //! patches are interpreted against the current item prototype when used. //! Component schemas supply typed size queries and contained-item validation. use crate::{ component_patch::{Patch, Prototype}, item_stack_refs::{ItemStacks, StackRef}, item_stack_storage::ItemStackData, }; use std::{fmt, rc::Rc}; pub trait ValidationRules { type ContentsError; fn has_max_damage(&self, stack: &ItemStackData) -> bool; /// The component default is one, including on an empty stack. fn max_stack_size(&self, stack: &ItemStackData) -> i32; /// Validate container sizes, then bundle sizes and weight, then charged /// projectile sizes. Only the immediate contained sizes are checked. fn validate_contents(&self, stack: &ItemStackData) -> Result<(), Self::ContentsError>; } pub type Creation = (StackRef, Option>); #[derive(Clone, Debug, PartialEq, Eq)] pub enum ValidationError { StackableDamageable, Contents(E), TooManyItems { count: i32, maximum: i32 }, } pub fn validate_strict< I: Copy + Eq, K: Clone + Eq, V: Clone + PartialEq, R: ValidationRules, >( stack: &ItemStackData, rules: &R, ) -> Result<(), ValidationError> { if rules.has_max_damage(stack) && rules.max_stack_size(stack) > 1 { return Err(ValidationError::StackableDamageable); } rules .validate_contents(stack) .map_err(ValidationError::Contents)?; let count = stack.count(); let maximum = rules.max_stack_size(stack); if count > maximum { return Err(ValidationError::TooManyItems { count, maximum }); } Ok(()) } /// A rejected patch restores the original raw patch, including redundant entries. /// The error is returned for the caller's diagnostic logger. pub fn apply_validated< I: Copy + Eq, K: Clone + Eq, V: Clone + PartialEq, R: ValidationRules, >( stack: &mut ItemStackData, patch: &Patch, rules: &R, ) -> Result<(), ValidationError> { let previous = stack.components.as_patch(); stack.components.apply_patch(patch); let result = validate_strict(stack, rules); if result.is_err() { stack.components.restore_patch(&previous); } result } #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum TemplateError { EmptyItem, EmptyStack, } impl fmt::Display for TemplateError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str(match self { Self::EmptyItem => "Item must be non-empty", Self::EmptyStack => "Stack must be non-empty", }) } } impl std::error::Error for TemplateError {} #[derive(Debug)] struct Data { item: I, air: I, count: i32, patch: Patch, } #[derive(Debug)] pub struct ItemTemplate(Rc>); impl Clone for ItemTemplate { fn clone(&self) -> Self { Self(self.0.clone()) } } impl ItemTemplate { /// The native constructor rejects zero, but permits negative counts. pub fn new(item: I, air: I, count: i32, patch: Patch) -> Result { if item == air || count == 0 { return Err(TemplateError::EmptyItem); } Ok(Self(Rc::new(Data { item, air, count, patch, }))) } pub fn from_stack(stack: &ItemStackData, air: I) -> Result { Self::new(stack.item(), air, stack.count(), stack.visible_patch()) } pub fn from_non_empty_stack( stack: &ItemStackData, air: I, ) -> Result { if stack.is_empty() { return Err(TemplateError::EmptyStack); } Self::from_stack(stack, air) } pub fn from_non_empty_stack_with_count( stack: &ItemStackData, air: I, count: i32, ) -> Result { if stack.is_empty() || count <= 0 { return Err(TemplateError::EmptyStack); } Self::new(stack.item(), air, count, stack.visible_patch()) } pub fn same_reference(&self, other: &Self) -> bool { Rc::ptr_eq(&self.0, &other.0) } pub fn item(&self) -> I { self.0.item } pub fn count(&self) -> i32 { self.0.count } pub fn patch(&self) -> &Patch { &self.0.patch } pub fn with_count(&self, count: i32) -> Result { if count == self.count() { return Ok(self.clone()); } Self::new(self.item(), self.0.air, count, self.0.patch.clone()) } pub fn component<'a>(&'a self, prototype: &'a Prototype, key: &K) -> Option<&'a V> { match self.0.patch.entries().iter().find(|(k, _)| k == key) { Some((_, value)) => value.as_ref(), None => prototype.get(key), } } /// Invalid creations return the store's canonical EMPTY. The optional error /// lets the caller log the rejected template without changing game behavior. pub fn create>( &self, stacks: &ItemStacks, prototype: Prototype, rules: &R, ) -> Creation { self.apply(stacks, prototype, self.count(), &Patch::new([]), rules) } /// The supplied patch is the base; this template's patch takes precedence. pub fn apply>( &self, stacks: &ItemStacks, prototype: Prototype, count: i32, base: &Patch, rules: &R, ) -> Creation { let stack = stacks.create(self.item(), count, prototype); { let mut data = stack.write(); data.components.apply_patch(base); data.components.apply_patch(self.patch()); } let error = validate_strict(&stack.read(), rules).err(); if error.is_some() { (stacks.empty(), error) } else { (stack, None) } } } impl PartialEq for ItemTemplate { fn eq(&self, other: &Self) -> bool { self.0.item == other.0.item && self.0.count == other.0.count && self.0.patch == other.0.patch } }