forked from pointfreeco/swift-parsing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Routing.swift
166 lines (146 loc) · 3.6 KB
/
Routing.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import Benchmark
import Foundation
import Parsing
/*
This benchmark demonstrates how you can build a URL request router that can transform an input
request into a more well-structured data type, such as an enum. We build a router that can
recognize one of 5 routes for a website.
*/
let routingSuite = BenchmarkSuite(name: "Routing") { suite in
enum Route: Equatable {
case home
case contactUs
case episodes
case episode(id: Int)
case episodeComments(id: Int)
}
let router = Method("GET")
.skip(End())
.map { Route.home }
.orElse(
Method("GET")
.skip(Path(StartsWith("contact-us".utf8)))
.skip(End())
.map { Route.contactUs }
)
.orElse(
Method("GET")
.skip(Path(StartsWith("episodes".utf8)))
.skip(End())
.map { Route.episodes }
)
.orElse(
Method("GET")
.skip(Path(StartsWith("episodes".utf8)))
.take(Path(Int.parser()))
.skip(End())
.map(Route.episode(id:))
)
.orElse(
Method("GET")
.skip(Path(StartsWith("episodes".utf8)))
.take(Path(Int.parser()))
.skip(Path(StartsWith("comments".utf8)))
.skip(End())
.map(Route.episodeComments(id:))
)
let requests = [
RequestData(
method: "GET"
),
RequestData(
method: "GET",
pathComponents: ["contact-us"[...].utf8]
),
RequestData(
method: "GET",
pathComponents: ["episodes"[...].utf8]
),
RequestData(
method: "GET",
pathComponents: ["episodes"[...].utf8, "1"[...].utf8]
),
RequestData(
method: "GET",
pathComponents: ["episodes"[...].utf8, "1"[...].utf8, "comments"[...].utf8]
),
]
var output: [Route]!
var expectedOutput = [
Route.home,
.contactUs,
.episodes,
.episode(id: 1),
.episodeComments(id: 1),
]
suite.benchmark(
name: "Parser",
run: {
output = requests.map {
var input = $0
return router.parse(&input)!
}
},
tearDown: {
precondition(output == expectedOutput)
}
)
}
private struct RequestData {
var body: Data?
var headers: [(String, Substring.UTF8View)] = []
var method: String?
var pathComponents: ArraySlice<Substring.UTF8View> = []
var queryItems: [(String, Substring.UTF8View)] = []
}
private struct Method: Parser {
typealias Input = RequestData
typealias Output = Void
let method: String
init(_ method: String) {
self.method = method
}
func parse(_ input: inout RequestData) -> Void? {
guard input.method?.lowercased() == self.method.lowercased()
else { return nil }
input.method = nil
return ()
}
}
private struct Path<Component>: Parser
where
Component: Parser,
Component.Input == Substring.UTF8View
{
typealias Input = RequestData
typealias Output = Component.Output
let component: Component
init(_ component: Component) {
self.component = component
}
func parse(_ input: inout Input) -> Output? {
guard !input.pathComponents.isEmpty
else { return nil }
let original = input
let output = self.component.parse(&input.pathComponents[input.pathComponents.startIndex])
guard input.pathComponents[input.pathComponents.startIndex].isEmpty
else {
input = original
return nil
}
input.pathComponents.removeFirst()
return output
}
}
private struct End: Parser {
typealias Input = RequestData
typealias Output = Void
func parse(_ input: inout Input) -> Output? {
guard
input.method == nil,
input.pathComponents.isEmpty
else { return nil }
input = .init()
return ()
}
}