-
-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathEditableProfile.swift
More file actions
95 lines (79 loc) · 2.58 KB
/
Copy pathEditableProfile.swift
File metadata and controls
95 lines (79 loc) · 2.58 KB
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
// SPDX-FileCopyrightText: 2026 Davide De Rosa
//
// SPDX-License-Identifier: GPL-3.0
import CommonLibrary
public struct EditableProfile: MutableProfileType {
public let version: Int? = nil
public var id: UUID
public var name: String
public var modules: [any ModuleBuilder]
public var activeModulesIds: Set<UUID>
public var behavior: ProfileBehavior?
public var userInfo: JSON?
public init(
id: UUID = UUID(),
name: String = "",
modules: [any ModuleBuilder] = [],
activeModulesIds: Set<UUID> = [],
behavior: ProfileBehavior? = nil,
userInfo: JSON? = nil
) {
self.id = id
self.name = name
self.modules = modules
self.activeModulesIds = activeModulesIds
self.behavior = behavior
self.userInfo = userInfo
}
public func builder() throws -> Profile.Builder {
var builder = Profile.Builder(id: id)
builder.modules = try modules.compactMap {
do {
return try $0.build()
} catch {
throw ABI.AppError.malformedModule($0, error: error)
}
}
builder.activeModulesIds = activeModulesIds
let trimmedName = name.trimmingCharacters(in: .whitespaces)
guard !trimmedName.isEmpty else {
throw ABI.AppError.emptyProfileName
}
builder.name = trimmedName
builder.behavior = behavior
builder.userInfo = userInfo
// some modules may require an active connection module (VPN)
// for example, IP and HTTP Proxy modules require a VPN in NE
if !builder.hasConnection,
let requiringConnection = builder.activeModules.first(where: \.requiresConnection) {
throw ABI.AppError.moduleRequiresConnection(requiringConnection)
}
return builder
}
}
private extension Profile.Builder {
var hasConnection: Bool {
if activeConnectionModule != nil { return true }
if let providerModule = firstModule(ofType: ProviderModule.self, ifActive: true) {
return providerModule.buildsConnection
}
return false
}
}
extension Profile {
public func editable() -> EditableProfile {
EditableProfile(
id: id,
name: name,
modules: modulesBuilders(),
activeModulesIds: activeModulesIds,
behavior: behavior,
userInfo: userInfo
)
}
public func modulesBuilders() -> [any ModuleBuilder] {
modules.compactMap {
$0.moduleBuilder()
}
}
}