-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathRelayLocation.swift
118 lines (97 loc) · 3.39 KB
/
RelayLocation.swift
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//
// RelayLocation.swift
// MullvadTypes
//
// Created by pronebird on 21/10/2022.
// Copyright © 2022 Mullvad VPN AB. All rights reserved.
//
import Foundation
public enum RelayLocation: Codable, Hashable, CustomDebugStringConvertible {
case country(String)
case city(String, String)
case hostname(String, String, String)
public init?(dashSeparatedString: String) {
let components = dashSeparatedString.split(separator: "-", maxSplits: 2).map(String.init)
switch components.count {
case 1:
self = .country(components[0])
case 2:
self = .city(components[0], components[1])
case 3:
self = .hostname(components[0], components[1], components[2])
default:
return nil
}
}
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let components = try container.decode([String].self)
switch components.count {
case 1:
self = .country(components[0])
case 2:
self = .city(components[0], components[1])
case 3:
self = .hostname(components[0], components[1], components[2])
default:
throw DecodingError.dataCorruptedError(
in: container,
debugDescription: "Invalid enum representation"
)
}
}
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case let .country(code):
try container.encode([code])
case let .city(countryCode, cityCode):
try container.encode([countryCode, cityCode])
case let .hostname(countryCode, cityCode, hostname):
try container.encode([countryCode, cityCode, hostname])
}
}
/// A list of `RelayLocation` items preceding the given one in the relay tree
public var ancestors: [RelayLocation] {
switch self {
case let .hostname(country, city, _):
return [.country(country), .city(country, city)]
case let .city(country, _):
return [.country(country)]
case .country:
return []
}
}
public var debugDescription: String {
var output = "RelayLocation."
switch self {
case let .country(country):
output += "country(\(String(reflecting: country)))"
case let .city(country, city):
output += "city(\(String(reflecting: country)), \(String(reflecting: city)))"
case let .hostname(country, city, host):
output += "hostname(\(String(reflecting: country)), " +
"\(String(reflecting: city)), " +
"\(String(reflecting: host)))"
}
return output
}
public var stringRepresentation: String {
switch self {
case let .country(country):
return country
case let .city(country, city):
return "\(country)-\(city)"
case let .hostname(country, city, host):
return "\(country)-\(city)-\(host)"
}
}
}
public struct RelayLocations: Codable, Equatable {
public let locations: [RelayLocation]
public let customListId: UUID?
public init(locations: [RelayLocation], customListId: UUID? = nil) {
self.locations = locations
self.customListId = customListId
}
}