diff options
author | Roopesh Chander <roop@roopc.net> | 2018-11-10 18:15:43 +0530 |
---|---|---|
committer | Roopesh Chander <roop@roopc.net> | 2018-11-10 19:17:24 +0530 |
commit | 0f03ffc920c2603777fce7b35d395bbf74520be4 (patch) | |
tree | ad08b9b7995bda1ca65f859d3208c92e11e027f8 /WireGuard/Shared | |
parent | Model: TunnelConfiguration: Add explicit conformance to Decodable (diff) | |
download | wireguard-apple-0f03ffc920c2603777fce7b35d395bbf74520be4.tar.xz wireguard-apple-0f03ffc920c2603777fce7b35d395bbf74520be4.zip |
Model: ActivityType enum to represent VPN-on-demand options
Signed-off-by: Roopesh Chander <roop@roopc.net>
Diffstat (limited to 'WireGuard/Shared')
-rw-r--r-- | WireGuard/Shared/Model/ActivationType.swift | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/WireGuard/Shared/Model/ActivationType.swift b/WireGuard/Shared/Model/ActivationType.swift new file mode 100644 index 0000000..56b0e6c --- /dev/null +++ b/WireGuard/Shared/Model/ActivationType.swift @@ -0,0 +1,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 + } +} |