blob: 56b0e6c38f963c60aab4543319b525a93c577373 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
// SPDX-License-Identifier: MIT
// Copyright © 2018 WireGuard LLC. All Rights Reserved.
enum ActivationType {
case activateManually
case useOnDemandForAnyInternetActivity
case useOnDemandOnlyOverWifi
case useOnDemandOnlyOverCellular
}
extension ActivationType: Codable {
// We use separate coding keys in case we might have a enum with associated values in the future
enum CodingKeys: CodingKey {
case activateManually
case useOnDemandForAnyInternetActivity
case useOnDemandOnlyOverWifi
case useOnDemandOnlyOverCellular
}
// Decoding error
enum DecodingError: Error {
case invalidInput
}
// Encoding
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
switch self {
case .activateManually:
try container.encode(true, forKey: CodingKeys.activateManually)
case .useOnDemandForAnyInternetActivity:
try container.encode(true, forKey: CodingKeys.useOnDemandForAnyInternetActivity)
case .useOnDemandOnlyOverWifi:
try container.encode(true, forKey: CodingKeys.useOnDemandOnlyOverWifi)
case .useOnDemandOnlyOverCellular:
try container.encode(true, forKey: CodingKeys.useOnDemandOnlyOverCellular)
}
}
// Decoding
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
if let isValid = try? container.decode(Bool.self, forKey: CodingKeys.activateManually), isValid {
self = .activateManually
return
}
if let isValid = try? container.decode(Bool.self, forKey: CodingKeys.useOnDemandForAnyInternetActivity), isValid {
self = .useOnDemandForAnyInternetActivity
return
}
if let isValid = try? container.decode(Bool.self, forKey: CodingKeys.useOnDemandOnlyOverWifi), isValid {
self = .useOnDemandOnlyOverWifi
return
}
if let isValid = try? container.decode(Bool.self, forKey: CodingKeys.useOnDemandOnlyOverCellular), isValid {
self = .useOnDemandOnlyOverCellular
return
}
throw DecodingError.invalidInput
}
}
|