// Package itemoccurrences owns canonical ordering and durable domain rules for D&D // item-occurrence artifacts. package itemoccurrences import ( "strconv" "strings" "gitea.maximumdirect.net/eric/notarius/internal/core/source" "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd" "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared" ) const partyHolder = "party" // SupportedKind reports whether kind is one of the durable item occurrence kinds. func SupportedKind(kind dnd.ItemOccurrenceKind) bool { switch kind { case dnd.ItemOccurrenceKindDiscovered, dnd.ItemOccurrenceKindAcquired, dnd.ItemOccurrenceKindLost, dnd.ItemOccurrenceKindConsumed, dnd.ItemOccurrenceKindTransferred: return true default: return false } } // DisplayValue returns the durable display form used for item and holder // comparisons without changing the observed internal spelling. func DisplayValue(value string) string { return strings.TrimSpace(value) } // ComparisonKey returns the shared D&D Unicode- and case-insensitive key for // a trimmed item or holder display value. func ComparisonKey(value string) string { return shared.ComparisonKey(DisplayValue(value)) } // HolderPresent reports whether value is a nonblank optional holder. func HolderPresent(value string) bool { return DisplayValue(value) != "" } // IsPartyHolder reports whether value denotes collective party possession. func IsPartyHolder(value string) bool { return ComparisonKey(value) == partyHolder } // ValidHolderCombination reports whether the optional holder fields satisfy // the durable rules for kind. Blank holders are treated as absent so callers // can preserve invalid extraction candidates for their owning validators. func ValidHolderCombination(kind dnd.ItemOccurrenceKind, from, to string) bool { hasFrom := HolderPresent(from) hasTo := HolderPresent(to) switch kind { case dnd.ItemOccurrenceKindDiscovered: return !hasFrom && !hasTo case dnd.ItemOccurrenceKindAcquired: return !hasFrom && hasTo case dnd.ItemOccurrenceKindLost, dnd.ItemOccurrenceKindConsumed: return hasFrom && !hasTo case dnd.ItemOccurrenceKindTransferred: return hasFrom && hasTo && !IsPartyHolder(from) && !IsPartyHolder(to) && ComparisonKey(from) != ComparisonKey(to) default: return false } } // SourceRefsEqual reports whether two source-reference sequences have the // same representation and values, including nil-versus-empty distinction. func SourceRefsEqual(left, right []source.SourceRef) bool { if (left == nil) != (right == nil) || len(left) != len(right) { return false } for index := range left { if left[index] != right[index] { return false } } return true } // ValidSourceRefs reports whether refs are non-empty and valid for index. func ValidSourceRefs(index source.DocumentIndex, refs []source.SourceRef) bool { if len(refs) == 0 { return false } for _, ref := range refs { if index.ValidateRef(ref) != nil { return false } } return true } // Less defines the canonical occurrence order. Invalid source references remain // comparable through SourceRefOrder's literal fallback so malformed candidates // are still safe to sort and diagnose. func Less(order shared.SourceRefOrder, left, right dnd.ItemOccurrence) bool { left.SourceRefs = order.Canonicalize(left.SourceRefs) right.SourceRefs = order.Canonicalize(right.SourceRefs) return CanonicalLess(order, left, right) } // CanonicalLess defines the canonical occurrence order without modifying or // canonicalizing source references. Callers must provide canonical evidence. func CanonicalLess(order shared.SourceRefOrder, left, right dnd.ItemOccurrence) bool { return CanonicalLessPrepared(order, left, right, PrepareCanonicalOrder(order, left), PrepareCanonicalOrder(order, right)) } type holderOrderKey struct { present bool comparison, display string } // CanonicalOrderKey holds invariant comparisons for an owned occurrence. type CanonicalOrderKey struct { earliest int hasEvidence bool nameComparison, nameDisplay string from, to holderOrderKey } // PrepareCanonicalOrder requires source references already in canonical order. func PrepareCanonicalOrder(order shared.SourceRefOrder, value dnd.ItemOccurrence) CanonicalOrderKey { earliest, hasEvidence := order.EarliestValid(value.SourceRefs) holder := func(value string) holderOrderKey { display := DisplayValue(value) return holderOrderKey{present: display != "", comparison: ComparisonKey(display), display: display} } return CanonicalOrderKey{earliest: earliest, hasEvidence: hasEvidence, nameComparison: ComparisonKey(value.Name), nameDisplay: DisplayValue(value.Name), from: holder(value.From), to: holder(value.To)} } // CanonicalLessPrepared compares values with keys prepared from those same // canonical, unchanged records; arbitrary input should use Less instead. func CanonicalLessPrepared(order shared.SourceRefOrder, left, right dnd.ItemOccurrence, leftKey, rightKey CanonicalOrderKey) bool { if leftKey.hasEvidence != rightKey.hasEvidence { return leftKey.hasEvidence } if leftKey.hasEvidence && leftKey.earliest != rightKey.earliest { return leftKey.earliest < rightKey.earliest } if left.ItemID != right.ItemID { return left.ItemID < right.ItemID } if leftKey.nameComparison != rightKey.nameComparison { return leftKey.nameComparison < rightKey.nameComparison } if leftKey.nameDisplay != rightKey.nameDisplay { return leftKey.nameDisplay < rightKey.nameDisplay } if left.Kind != right.Kind { return left.Kind < right.Kind } if less, decided := holderKeyLess(leftKey.from, rightKey.from); decided { return less } if less, decided := holderKeyLess(leftKey.to, rightKey.to); decided { return less } if less, decided := optionalQuantityLess(left.Quantity, right.Quantity); decided { return less } return sourceRefsLess(order, left.SourceRefs, right.SourceRefs) } func holderKeyLess(left, right holderOrderKey) (bool, bool) { if left.present != right.present { return !left.present, true } if !left.present { return false, false } if left.comparison != right.comparison { return left.comparison < right.comparison, true } if left.display != right.display { return left.display < right.display, true } return false, false } // ExactEqual reports whether occurrences are exact duplicates after their display // fields and evidence have been canonicalized for the supplied source order. func ExactEqual(order shared.SourceRefOrder, left, right dnd.ItemOccurrence) bool { left.SourceRefs = order.Canonicalize(left.SourceRefs) right.SourceRefs = order.Canonicalize(right.SourceRefs) return CanonicalExactEqual(left, right) } // CanonicalExactEqual reports whether canonical occurrences are exact // duplicates without modifying or canonicalizing their source references. func CanonicalExactEqual(left, right dnd.ItemOccurrence) bool { if left.ItemID != right.ItemID || DisplayValue(left.Name) != DisplayValue(right.Name) || left.Kind != right.Kind || DisplayValue(left.From) != DisplayValue(right.From) || DisplayValue(left.To) != DisplayValue(right.To) || (left.Quantity == nil) != (right.Quantity == nil) { return false } if left.Quantity != nil && *left.Quantity != *right.Quantity { return false } return SourceRefsEqual(left.SourceRefs, right.SourceRefs) } // ExactIdentity returns a collision-safe duplicate key after display and // evidence canonicalization. It is intended for callers that have already // decided the occurrence is eligible for duplicate handling. func ExactIdentity(order shared.SourceRefOrder, occurrence dnd.ItemOccurrence) string { occurrence.SourceRefs = order.Canonicalize(occurrence.SourceRefs) return CanonicalExactIdentity(occurrence) } // CanonicalExactIdentity returns a collision-safe duplicate key without // modifying or canonicalizing source references. Callers must provide // canonical evidence. func CanonicalExactIdentity(occurrence dnd.ItemOccurrence) string { var key strings.Builder writeKeyString(&key, occurrence.ItemID) writeKeyString(&key, DisplayValue(occurrence.Name)) writeKeyString(&key, string(occurrence.Kind)) writeKeyString(&key, DisplayValue(occurrence.From)) writeKeyString(&key, DisplayValue(occurrence.To)) if occurrence.Quantity == nil { key.WriteByte('0') } else { key.WriteByte('1') writeKeyInt(&key, *occurrence.Quantity) } writeKeySourceRefs(&key, occurrence.SourceRefs) return key.String() } func writeKeySourceRefs(builder *strings.Builder, refs []source.SourceRef) { if refs == nil { builder.WriteByte('0') return } builder.WriteByte('1') writeKeyInt(builder, len(refs)) for _, ref := range refs { writeKeyString(builder, ref.SourceID) writeKeyInt(builder, ref.StartUnitID) writeKeyInt(builder, ref.EndUnitID) } } func optionalQuantityLess(left, right *int) (bool, bool) { if (left == nil) != (right == nil) { return left == nil, true } if left != nil && *left != *right { return *left < *right, true } return false, false } func sourceRefsLess(order shared.SourceRefOrder, left, right []source.SourceRef) bool { for index := 0; index < len(left) && index < len(right); index++ { if left[index] == right[index] { continue } return order.Less(left[index], right[index]) } return len(left) < len(right) } func writeKeyString(builder *strings.Builder, value string) { builder.WriteString(strconv.Itoa(len(value))) builder.WriteByte(':') builder.WriteString(value) } func writeKeyInt(builder *strings.Builder, value int) { builder.WriteString(strconv.Itoa(value)) builder.WriteByte(';') }