Commit ae41d0e459
Verified · cmc
Layout: unified · split
Packages/FoldKit/Sources/FoldFormat/FoldFile.swift +29
| @@ -6,6 +6,11 @@ public enum FoldError: Error, Equatable, Sendable { | ||
| 6 | 6 | case missingStepRecord(step: Int) |
| 7 | 7 | case topologyInFrame(frame: Int) |
| 8 | 8 | 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) | |
| 9 | 14 | } |
| 10 | 15 | |
| 11 | 16 | /// 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 { | ||
| 71 | 76 | facesVertices = try c.decode([[Int]].self, forKey: .facesVertices) |
| 72 | 77 | edgesFirstCreasedAtStep = try c.decodeIfPresent([Int?].self, forKey: .edgesFirstCreasedAtStep) |
| 73 | 78 | ?? Array(repeating: nil, count: edgesVertices.count) |
| 79 | if edgesFirstCreasedAtStep.count != edgesVertices.count { | |
| 80 | throw FoldError.edgeArrayLengthMismatch(frame: 0, key: CodingKeys.edgesFirstCreasedAtStep.rawValue) | |
| 81 | } | |
| 74 | 82 | |
| 75 | 83 | let keyFrame = try FoldFrame(from: decoder, inheriting: nil) |
| 76 | 84 | frames = [keyFrame] |
| 85 | try checkShape(keyFrame, 0) | |
| 77 | 86 | guard c.contains(.fileFrames) else { return } |
| 78 | 87 | var list = try c.nestedUnkeyedContainer(forKey: .fileFrames) |
| 79 | 88 | while !list.isAtEnd { |
| @@ -88,11 +97,31 @@ public struct FoldFile: Codable, Sendable { | ||
| 88 | 97 | throw FoldError.missingFrameInheritance(frame: n) |
| 89 | 98 | } |
| 90 | 99 | let frame = try FoldFrame(from: frameDecoder, inheriting: keyFrame) |
| 100 | try checkShape(frame, n) | |
| 91 | 101 | guard frame.step != nil else { throw FoldError.missingStepRecord(step: n) } |
| 92 | 102 | frames.append(frame) |
| 93 | 103 | } |
| 94 | 104 | } |
| 95 | 105 | |
| 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 | ||
| 96 | 125 | public func encode(to encoder: any Encoder) throws { |
| 97 | 126 | var c = encoder.container(keyedBy: CodingKeys.self) |
| 98 | 127 | try c.encode(fileSpec, forKey: .fileSpec) |
Packages/FoldKit/Tests/FoldFormatTests/DecodingTests.swift +41
| @@ -94,3 +94,44 @@ func rejectsCutAndJoinAssignments(_ code: String) { | ||
| 94 | 94 | try JSONDecoder().decode(EdgeAssignment.self, from: Data("\"\(code)\"".utf8)) |
| 95 | 95 | } |
| 96 | 96 | } |
| 97 | ||
| 98 | /// half.fold with `change` applied to its JSON object. | |
| 99 | private 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]]) | |
| 122 | func 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 | } | |