Commit ae41d0e459

ae41d0e459e2b95c0c449bfe414a0a5015123d54

parent: 44618d4ea0

Verified · cmc

cmc <hello@cleberg.net> · 2026-10-19 22:00 UTC

Decoder: reject edge arrays of the wrong length and malformed face orders

edges_assignment, edges_foldAngle and edges_krz:firstCreasedAtStep must
match edges_vertices; faceOrders and krz:faceOrders_midFold entries must
be [f, g, s] with faces in range and s in -1...1.

Ref #2

Layout: unified · split

Packages/FoldKit/Sources/FoldFormat/FoldFile.swift +29
@@ -6,6 +6,11 @@ public enum FoldError: Error, Equatable, Sendable {
66 case missingStepRecord(step: Int)
77 case topologyInFrame(frame: Int)
88 case missingFrameInheritance(frame: Int)
9 /// An `edges_` array, named by its FOLD key, whose length differs from `edges_vertices`.
10 case edgeArrayLengthMismatch(frame: Int, key: String)
11 /// Entry `entry` of `faceOrders` or `krz:faceOrders_midFold`, named by `key`, is not
12 /// `[f, g, s]` with both faces in range and `s` in −1...1.
13 case malformedFaceOrder(frame: Int, key: String, entry: Int)
914}
1015
1116/// A FOLD file whose frames share one topology. `frames[0]` is the key frame, FOLD's top level;
@@ -71,9 +76,13 @@ public struct FoldFile: Codable, Sendable {
7176 facesVertices = try c.decode([[Int]].self, forKey: .facesVertices)
7277 edgesFirstCreasedAtStep = try c.decodeIfPresent([Int?].self, forKey: .edgesFirstCreasedAtStep)
7378 ?? Array(repeating: nil, count: edgesVertices.count)
79 if edgesFirstCreasedAtStep.count != edgesVertices.count {
80 throw FoldError.edgeArrayLengthMismatch(frame: 0, key: CodingKeys.edgesFirstCreasedAtStep.rawValue)
81 }
7482
7583 let keyFrame = try FoldFrame(from: decoder, inheriting: nil)
7684 frames = [keyFrame]
85 try checkShape(keyFrame, 0)
7786 guard c.contains(.fileFrames) else { return }
7887 var list = try c.nestedUnkeyedContainer(forKey: .fileFrames)
7988 while !list.isAtEnd {
@@ -88,11 +97,31 @@ public struct FoldFile: Codable, Sendable {
8897 throw FoldError.missingFrameInheritance(frame: n)
8998 }
9099 let frame = try FoldFrame(from: frameDecoder, inheriting: keyFrame)
100 try checkShape(frame, n)
91101 guard frame.step != nil else { throw FoldError.missingStepRecord(step: n) }
92102 frames.append(frame)
93103 }
94104 }
95105
106 /// Rejects frame `n`'s edge arrays that disagree with `edges_vertices` and its malformed
107 /// face orders.
108 private func checkShape(_ frame: FoldFrame, _ n: Int) throws {
109 let edgeArrays: [(FoldFrame.CodingKeys, Int)] = [(.edgesAssignment, frame.edgesAssignment.count),
110 (.edgesFoldAngle, frame.edgesFoldAngle.count)]
111 for (key, count) in edgeArrays where count != edgesVertices.count {
112 throw FoldError.edgeArrayLengthMismatch(frame: n, key: key.rawValue)
113 }
114 let faces = facesVertices.indices
115 let orders: [(FoldFrame.CodingKeys, [[Int]])] = [(.faceOrders, frame.faceOrders),
116 (.faceOrdersMidFold, frame.faceOrdersMidFold ?? [])]
117 for (key, list) in orders {
118 for (i, t) in list.enumerated()
119 where t.count != 3 || !faces.contains(t[0]) || !faces.contains(t[1]) || !(-1...1).contains(t[2]) {
120 throw FoldError.malformedFaceOrder(frame: n, key: key.rawValue, entry: i)
121 }
122 }
123 }
124
96125 public func encode(to encoder: any Encoder) throws {
97126 var c = encoder.container(keyedBy: CodingKeys.self)
98127 try c.encode(fileSpec, forKey: .fileSpec)
Packages/FoldKit/Tests/FoldFormatTests/DecodingTests.swift +41
@@ -94,3 +94,44 @@ func rejectsCutAndJoinAssignments(_ code: String) {
9494 try JSONDecoder().decode(EdgeAssignment.self, from: Data("\"\(code)\"".utf8))
9595 }
9696}
97
98/// half.fold with `change` applied to its JSON object.
99private func decodeHalf(_ change: (inout [String: Any]) -> Void) throws -> FoldFile {
100 var json = try JSONSerialization.jsonObject(with: Data(contentsOf: fixture("half.fold"))) as! [String: Any]
101 change(&json)
102 return try JSONDecoder().decode(FoldFile.self, from: JSONSerialization.data(withJSONObject: json))
103}
104
105@Test func rejectsAnEdgeArrayOfTheWrongLength() {
106 #expect(throws: FoldError.edgeArrayLengthMismatch(frame: 0, key: "edges_krz:firstCreasedAtStep")) {
107 try decodeHalf { $0["edges_krz:firstCreasedAtStep"] = [NSNull(), 1] }
108 }
109 #expect(throws: FoldError.edgeArrayLengthMismatch(frame: 0, key: "edges_assignment")) {
110 try decodeHalf { $0["edges_assignment"] = ["B", "B", "B", "B", "B", "B"] }
111 }
112 #expect(throws: FoldError.edgeArrayLengthMismatch(frame: 2, key: "edges_foldAngle")) {
113 try decodeHalf { json in
114 var frames = json["file_frames"] as! [[String: Any]]
115 frames[1]["edges_foldAngle"] = [0, 0, 0, 0, 0, 0, 180, 0]
116 json["file_frames"] = frames
117 }
118 }
119}
120
121@Test(arguments: [[0, 1], [0, 2, 1], [-1, 1, 1], [0, 1, 2], [0, 1, 1, 0]])
122func rejectsAMalformedFaceOrder(_ triple: [Int]) {
123 #expect(throws: FoldError.malformedFaceOrder(frame: 1, key: "faceOrders", entry: 1)) {
124 try decodeHalf { json in
125 var frames = json["file_frames"] as! [[String: Any]]
126 frames[0]["faceOrders"] = [[0, 1, 1], triple]
127 json["file_frames"] = frames
128 }
129 }
130 #expect(throws: FoldError.malformedFaceOrder(frame: 1, key: "krz:faceOrders_midFold", entry: 0)) {
131 try decodeHalf { json in
132 var frames = json["file_frames"] as! [[String: Any]]
133 frames[0]["krz:faceOrders_midFold"] = [triple]
134 json["file_frames"] = frames
135 }
136 }
137}