menubar-pet/Sources/PetKit/PetState.swift
37 lines · 1298 bytes
1import Foundation
2
3// The models for what `pet-life` prints, in a library so the contract can be
4// tested. Field names match its JSON exactly, and that JSON is pinned to exact
5// bytes by a test on the Rust side — this is the other half of the same
6// contract. The wire format between the daemon and the collector has drifted
7// twice in this project; both times a pinned fixture caught it.
8
9/// What `pet-life` prints. Field names match its JSON exactly; that contract is
10/// pinned to exact bytes by a test on the Rust side.
11public struct Grave: Decodable {
12 public init(name: String, generation: Int, lived_days: Double) {
13 self.name = name; self.generation = generation; self.lived_days = lived_days
14 }
15
16 public let name: String
17 public let generation: Int
18 public let lived_days: Double
19}
20
21public struct PetState: Decodable {
22 public let alive: Bool
23 public let name: String?
24 public let generation: Int?
25 public let stage: String
26 public let face: String
27 public let age_days: Double
28 public let quiet_days: Double
29 public let cemetery: [Grave]
30}
31
32public extension PetState {
33 /// Decode what `pet-life` printed.
34 static func decode(_ data: Data) throws -> PetState {
35 try JSONDecoder().decode(PetState.self, from: data)
36 }
37}