Skip to content

Commit 838bbca

Browse files
committed
Add IndexFile and Bestiary index
1 parent 56a5fd2 commit 838bbca

3 files changed

Lines changed: 50 additions & 0 deletions

File tree

Sources/FifthEdition/Bestiary.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99

1010
import MemberwiseInit
1111

12+
@MemberwiseInit(.public)
13+
public struct Bestiary: Equatable, Codable, Sendable {
14+
public static let entryBasePath: String = "data/bestiary"
15+
public static let indexEntryPath: String = "\(entryBasePath)/index.json"
16+
17+
public var monster: [Creature] = []
18+
}
19+
1220
@MemberwiseInit(.public)
1321
public struct Creature: Equatable, Sendable {
1422
public enum AbilityScore: Equatable, Sendable {

Sources/FifthEdition/Util.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ extension Alignment: Hashable {
2727
static let any: Self = .all
2828
}
2929

30+
@MemberwiseInit(.public)
31+
public struct IndexFile: Equatable, Sendable {
32+
public var entries: [String: String] = [:]
33+
}
34+
3035
public enum Page: Equatable, Hashable, Sendable {
3136
case number(Int)
3237
case numeral(String)
@@ -173,6 +178,18 @@ extension Alignment: Codable {
173178
}
174179
}
175180

181+
extension IndexFile: Codable {
182+
public init(from decoder: any Decoder) throws {
183+
let container = try decoder.singleValueContainer()
184+
entries = try container.decode([String: String].self)
185+
}
186+
187+
public func encode(to encoder: any Encoder) throws {
188+
var container = encoder.singleValueContainer()
189+
try container.encode(entries)
190+
}
191+
}
192+
176193
extension Page: Codable {
177194
public init(from decoder: any Decoder) throws {
178195
let container = try decoder.singleValueContainer()

Tests/FifthEditionTests/UtilTests.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,31 @@ struct AlignmentCodableTests {
7171

7272
}
7373

74+
struct IndexFileCodableTests {
75+
76+
@Test("Index file")
77+
func index() throws {
78+
try testCodable(
79+
json: """
80+
{
81+
"XDMG" : "foo-xdmg.json",
82+
"XMM" : "foo-xmm.json",
83+
"XPHB" : "foo-xphb.json"
84+
}
85+
""",
86+
value: IndexFile(
87+
entries: [
88+
"XDMG": "foo-xdmg.json",
89+
"XMM": "foo-xmm.json",
90+
"XPHB": "foo-xphb.json",
91+
],
92+
)
93+
)
94+
95+
}
96+
97+
}
98+
7499
struct PageCodableTests {
75100

76101
@Test("Numeric page")

0 commit comments

Comments
 (0)