this repo has no description
at main 50 lines 1.7 kB view raw
1import Foundation 2 3public struct CreditEvent: Identifiable, Equatable, Codable, Sendable { 4 public var id: UUID 5 public let date: Date 6 public let service: String 7 public let creditsUsed: Double 8 9 public init(id: UUID = UUID(), date: Date, service: String, creditsUsed: Double) { 10 self.id = id 11 self.date = date 12 self.service = service 13 self.creditsUsed = creditsUsed 14 } 15 16 private enum CodingKeys: String, CodingKey { 17 case id 18 case date 19 case service 20 case creditsUsed 21 } 22 23 public init(from decoder: Decoder) throws { 24 let container = try decoder.container(keyedBy: CodingKeys.self) 25 self.id = try container.decodeIfPresent(UUID.self, forKey: .id) ?? UUID() 26 self.date = try container.decode(Date.self, forKey: .date) 27 self.service = try container.decode(String.self, forKey: .service) 28 self.creditsUsed = try container.decode(Double.self, forKey: .creditsUsed) 29 } 30 31 public func encode(to encoder: Encoder) throws { 32 var container = encoder.container(keyedBy: CodingKeys.self) 33 try container.encode(self.id, forKey: .id) 34 try container.encode(self.date, forKey: .date) 35 try container.encode(self.service, forKey: .service) 36 try container.encode(self.creditsUsed, forKey: .creditsUsed) 37 } 38} 39 40public struct CreditsSnapshot: Equatable, Codable, Sendable { 41 public let remaining: Double 42 public let events: [CreditEvent] 43 public let updatedAt: Date 44 45 public init(remaining: Double, events: [CreditEvent], updatedAt: Date) { 46 self.remaining = remaining 47 self.events = events 48 self.updatedAt = updatedAt 49 } 50}