JSONBridge.swift 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import Foundation
  2. enum JSONError: Error {
  3. case invalidString
  4. case invalidDate(String)
  5. case decodingFailed(Error)
  6. case encodingFailed
  7. }
  8. enum JSONBridge {
  9. static func pumpSettings(from: JSON) throws -> PumpSettings {
  10. try JSONBridge.from(string: from.rawJSON)
  11. }
  12. static func bgTargets(from: JSON) throws -> BGTargets {
  13. try JSONBridge.from(string: from.rawJSON)
  14. }
  15. static func basalProfile(from: JSON) throws -> [BasalProfileEntry] {
  16. try JSONBridge.from(string: from.rawJSON)
  17. }
  18. static func trioCustomOrefVariables(from: JSON) throws -> TrioCustomOrefVariables {
  19. try JSONBridge.from(string: from.rawJSON)
  20. }
  21. static func insulinSensitivities(from: JSON) throws -> InsulinSensitivities {
  22. try JSONBridge.from(string: from.rawJSON)
  23. }
  24. static func carbRatios(from: JSON) throws -> CarbRatios {
  25. try JSONBridge.from(string: from.rawJSON)
  26. }
  27. static func tempTargets(from: JSON) throws -> [TempTarget] {
  28. try JSONBridge.from(string: from.rawJSON)
  29. }
  30. static func profile(from: JSON) throws -> Profile {
  31. try JSONBridge.from(string: from.rawJSON)
  32. }
  33. static func autosens(from: JSON) throws -> Autosens? {
  34. try JSONBridge.from(string: from.rawJSON)
  35. }
  36. static func from<T: Decodable>(string: String) throws -> T {
  37. guard let data = string.data(using: .utf8) else {
  38. throw JSONError.invalidString
  39. }
  40. return try JSONCoding.decoder.decode(T.self, from: data)
  41. }
  42. }