CarbRatios.swift 835 B

1234567891011121314151617181920212223242526272829303132333435
  1. import Foundation
  2. struct CarbRatios: JSON {
  3. let units: CarbUnit
  4. let schedule: [CarbRatioEntry]
  5. }
  6. struct CarbRatioEntry: JSON {
  7. let start: String
  8. let offset: Int
  9. let ratio: Decimal
  10. }
  11. enum CarbUnit: String, JSON {
  12. case grams
  13. case exchanges
  14. }
  15. extension CarbRatioEntry {
  16. private enum CodingKeys: String, CodingKey {
  17. case start
  18. case offset
  19. case ratio
  20. }
  21. init(from decoder: Decoder) throws {
  22. let container = try decoder.container(keyedBy: CodingKeys.self)
  23. let start = try container.decode(String.self, forKey: .start)
  24. let offset = try container.decode(Int.self, forKey: .offset)
  25. let ratio = try container.decode(Double.self, forKey: .ratio).decimal ?? .zero
  26. self = CarbRatioEntry(start: start, offset: offset, ratio: ratio)
  27. }
  28. }