1import Foundation
2
3/// A row/column coordinate inside a puzzle grid. Also used as the dictionary
4/// key for `GridState`, `MovesValue.cells` and `Puzzle.decorations`.
5struct GridPosition: Hashable, Sendable, Codable {
6 let row: Int
7 let col: Int
8}
9
10extension GridPosition {
11 /// `true` when both coordinates survive the trapping `Int16` conversions
12 /// every Core Data cell/journal sink performs. The remote codecs drop
13 /// entries that fail this, so a forged record can't persist a value that
14 /// crashes cache replay on every subsequent fetch.
15 var isInt16Representable: Bool {
16 row >= 0 && col >= 0 && row <= Int(Int16.max) && col <= Int(Int16.max)
17 }
18
19 /// `isInt16Representable` plus grid-shape bounds, for sinks that know the
20 /// game's dimensions. Zero dimensions (a row that predates the recorded
21 /// grid size) skip the shape check rather than rejecting everything.
22 func isPersistable(gridWidth: Int16, gridHeight: Int16) -> Bool {
23 guard isInt16Representable else { return false }
24 guard gridWidth > 0, gridHeight > 0 else { return true }
25 return row < Int(gridHeight) && col < Int(gridWidth)
26 }
27}