AlgorithmError.swift 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //
  2. // AlgorithmError.swift
  3. // G7SensorKit
  4. //
  5. // Created by Pete Schwamb on 11/11/22.
  6. //
  7. import Foundation
  8. enum AlgorithmError: Error {
  9. case unreliableState(AlgorithmState)
  10. }
  11. extension AlgorithmError: LocalizedError {
  12. var errorDescription: String? {
  13. switch self {
  14. case .unreliableState:
  15. return LocalizedString("Glucose data is unavailable", comment: "Error description for unreliable state")
  16. }
  17. }
  18. var failureReason: String? {
  19. switch self {
  20. case .unreliableState(let state):
  21. return state.localizedDescription
  22. }
  23. }
  24. }
  25. extension AlgorithmState {
  26. public var localizedDescription: String {
  27. switch self {
  28. case .known(let state):
  29. switch state {
  30. case .ok:
  31. return LocalizedString("Sensor is OK", comment: "The description of sensor algorithm state when sensor is ok.")
  32. case .stopped:
  33. return LocalizedString("Sensor is stopped", comment: "The description of sensor algorithm state when sensor is stopped.")
  34. case .warmup, .questionMarks:
  35. return LocalizedString("Sensor is warming up", comment: "The description of sensor algorithm state when sensor is warming up.")
  36. case .expired:
  37. return LocalizedString("Sensor expired", comment: "The description of sensor algorithm state when sensor is expired.")
  38. case .sensorFailed:
  39. return LocalizedString("Sensor failed", comment: "The description of sensor algorithm state when sensor failed.")
  40. }
  41. case .unknown(let rawValue):
  42. return String(format: LocalizedString("Sensor is in unknown state %1$d", comment: "The description of sensor algorithm state when raw value is unknown. (1: missing data details)"), rawValue)
  43. }
  44. }
  45. }