|
| 1 | +/// - キーに指定するラベル |
| 2 | +/// - labels on the key |
| 3 | +public enum CustardKeyLabelStyle: Codable, Equatable, Hashable, Sendable { |
| 4 | + case text(String) |
| 5 | + case systemImage(String) |
| 6 | + case mainAndSub(String, String) |
| 7 | + case mainAndDirections(String, CustardKeyDirectionalLabel) |
| 8 | +} |
| 9 | + |
| 10 | +public struct CustardKeyDirectionalLabel: Codable, Equatable, Hashable, Sendable { |
| 11 | + var left: String? |
| 12 | + var top: String? |
| 13 | + var right: String? |
| 14 | + var bottom: String? |
| 15 | +} |
| 16 | + |
| 17 | +public extension CustardKeyLabelStyle { |
| 18 | + private enum CodingKeys: CodingKey { |
| 19 | + case text |
| 20 | + case system_image |
| 21 | + case type |
| 22 | + case main |
| 23 | + case sub |
| 24 | + case directions |
| 25 | + } |
| 26 | + |
| 27 | + private enum ValueType: String, Codable { |
| 28 | + case text |
| 29 | + case system_image |
| 30 | + case main_and_sub |
| 31 | + case main_and_directions |
| 32 | + } |
| 33 | + |
| 34 | + func encode(to encoder: any Encoder) throws { |
| 35 | + var container = encoder.container(keyedBy: CodingKeys.self) |
| 36 | + switch self { |
| 37 | + case let .text(value): |
| 38 | + try container.encode(value, forKey: .text) |
| 39 | + case let .systemImage(value): |
| 40 | + try container.encode(value, forKey: .system_image) |
| 41 | + case let .mainAndSub(main, sub): |
| 42 | + try container.encode(ValueType.main_and_sub, forKey: .type) |
| 43 | + try container.encode(main, forKey: .main) |
| 44 | + try container.encode(sub, forKey: .sub) |
| 45 | + case let .mainAndDirections(main, directions): |
| 46 | + try container.encode(ValueType.main_and_directions, forKey: .type) |
| 47 | + try container.encode(main, forKey: .main) |
| 48 | + try container.encode(directions, forKey: .directions) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + init(from decoder: any Decoder) throws { |
| 53 | + let container = try decoder.container(keyedBy: CodingKeys.self) |
| 54 | + // "type"が見つかった場合 |
| 55 | + if let type = try? container.decode(ValueType.self, forKey: .type) { |
| 56 | + switch type { |
| 57 | + case .text: |
| 58 | + let value = try container.decode( |
| 59 | + String.self, |
| 60 | + forKey: .text |
| 61 | + ) |
| 62 | + self = .text(value) |
| 63 | + case .system_image: |
| 64 | + let value = try container.decode( |
| 65 | + String.self, |
| 66 | + forKey: .system_image |
| 67 | + ) |
| 68 | + self = .systemImage(value) |
| 69 | + case .main_and_sub: |
| 70 | + let main = try container.decode( |
| 71 | + String.self, |
| 72 | + forKey: .main |
| 73 | + ) |
| 74 | + let sub = try container.decode( |
| 75 | + String.self, |
| 76 | + forKey: .sub |
| 77 | + ) |
| 78 | + self = .mainAndSub(main, sub) |
| 79 | + case .main_and_directions: |
| 80 | + let main = try container.decode( |
| 81 | + String.self, |
| 82 | + forKey: .main |
| 83 | + ) |
| 84 | + let directions = try container.decode( |
| 85 | + CustardKeyDirectionalLabel.self, |
| 86 | + forKey: .directions |
| 87 | + ) |
| 88 | + self = .mainAndDirections(main, directions) |
| 89 | + } |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + // それ以外の場合(old cases) |
| 94 | + guard container.allKeys.count == 1, let key = container.allKeys.first else { |
| 95 | + throw DecodingError.dataCorrupted( |
| 96 | + DecodingError.Context( |
| 97 | + codingPath: container.codingPath, |
| 98 | + debugDescription: "Unabled to decode CustardKeyLabelStyle." |
| 99 | + ) |
| 100 | + ) |
| 101 | + } |
| 102 | + switch key { |
| 103 | + case .text: |
| 104 | + let value = try container.decode( |
| 105 | + String.self, |
| 106 | + forKey: .text |
| 107 | + ) |
| 108 | + self = .text(value) |
| 109 | + case .system_image: |
| 110 | + let value = try container.decode( |
| 111 | + String.self, |
| 112 | + forKey: .system_image |
| 113 | + ) |
| 114 | + self = .systemImage(value) |
| 115 | + default: |
| 116 | + throw DecodingError.dataCorrupted( |
| 117 | + DecodingError.Context( |
| 118 | + codingPath: container.codingPath, |
| 119 | + debugDescription: "Unabled to decode CustardKeyLabelStyle." |
| 120 | + ) |
| 121 | + ) |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments