blob: 786891db228cf7d9b098bdc458c92a81ea987ede (
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
import Foundation
/// Represents a parsed `hutch://` deep link.
enum DeepLink: Equatable {
case home
case work
/// hutch://git/<owner>/<repo>
case repository(owner: String, repo: String)
/// hutch://todo/<owner>/<tracker>/<ticketId>
case ticket(owner: String, tracker: String, ticketId: Int)
/// hutch://builds/<jobId>
case build(jobId: Int)
/// hutch://builds (tab-level)
case buildsTab
/// hutch://repositories (tab-level)
case repositoriesTab
/// hutch://trackers (tab-level)
case trackersTab
/// hutch://status
case systemStatus
/// hutch://lookup
case lookup
/// Attempt to parse a URL into a DeepLink.
/// Expected format: hutch://<path>
init?(url: URL) {
guard url.scheme == "hutch" else { return nil }
// url.host gives the first path component for opaque URLs;
// use standardized path components from the full string.
let components = url.pathComponents(fromScheme: "hutch")
switch components.first {
case "home", nil:
self = .home
case "work", "inbox":
self = .work
case "git" where components.count >= 3:
let owner = components[1]
let repo = components[2]
self = .repository(owner: owner, repo: repo)
case "todo" where components.count >= 4:
let owner = components[1]
let tracker = components[2]
guard let ticketId = Int(components[3]) else { return nil }
self = .ticket(owner: owner, tracker: tracker, ticketId: ticketId)
case "builds" where components.count >= 2:
guard let jobId = Int(components[1]) else { return nil }
self = .build(jobId: jobId)
case "builds":
self = .buildsTab
case "repositories":
self = .repositoriesTab
case "trackers":
self = .trackersTab
case "status":
self = .systemStatus
case "lookup":
self = .lookup
default:
return nil
}
}
}
private extension URL {
/// Parse path components from a custom-scheme URL.
/// For `hutch://git/~user/repo`, returns `["git", "~user", "repo"]`.
func pathComponents(fromScheme scheme: String) -> [String] {
// Remove scheme prefix and split by "/"
var str = absoluteString
if str.hasPrefix("\(scheme)://") {
str = String(str.dropFirst("\(scheme)://".count))
}
return str.split(separator: "/").map(String.init)
}
}
|