summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/TunnelConfigurationCoder.swift
blob: 97500eccbb2e971818a5feafa81eb6e1b9c7f925 (plain)
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
//
//  TunnelConfigurationCoder.swift
//  MullvadVPN
//
//  Created by pronebird on 02/10/2019.
//  Copyright © 2019 Mullvad VPN AB. All rights reserved.
//

import Foundation

/// Tunnel configuration encoding and decoding helper
enum TunnelConfigurationCoder {}

extension TunnelConfigurationCoder {

    enum Error: Swift.Error {
        case encode(Swift.Error)
        case decode(Swift.Error)
    }

    static func decode(data: Data) -> Result<TunnelConfiguration, Error> {
        return Result { try JSONDecoder().decode(TunnelConfiguration.self, from: data) }
            .mapError { Error.decode($0) }
    }

    static func encode(tunnelConfig: TunnelConfiguration) -> Result<Data, Error> {
        return Result { try JSONEncoder().encode(tunnelConfig) }
            .mapError { Error.encode($0) }
    }
}