Skip to content

Commit b0a00af

Browse files
committed
feat: use structs instead of enums for value types
1 parent decb0f0 commit b0a00af

File tree

5 files changed

+50
-79
lines changed

5 files changed

+50
-79
lines changed

Sources/HTTPTypesHtmx/HtmxResponseHeaders.swift

+3-15
Original file line numberDiff line numberDiff line change
@@ -223,13 +223,7 @@ extension HTTPFields {
223223
///
224224
/// [https://htmx.org/headers/hx-push-url/](https://htmx.org/headers/hx-push-url/)
225225
public var hxPushUrl: HxPushUrl? {
226-
guard let value = self[.hxPushUrl] else {
227-
return nil
228-
}
229-
switch value {
230-
case "false": return .false
231-
default: return .url(value)
232-
}
226+
self[.hxPushUrl].map(HxPushUrl.init(_:))
233227
}
234228

235229
/// Returns the value of an HX-Redirect response header
@@ -256,13 +250,7 @@ extension HTTPFields {
256250
///
257251
/// [https://htmx.org/headers/hx-replace-url/](https://htmx.org/headers/hx-replace-url/)
258252
public var hxReplaceUrl: HxReplaceUrl? {
259-
guard let value = self[.hxReplaceUrl] else {
260-
return nil
261-
}
262-
switch value {
263-
case "false": return .false
264-
default: return .url(value)
265-
}
253+
self[.hxReplaceUrl].map(HxReplaceUrl.init(_:))
266254
}
267255

268256
/// Returns the value of an HX-Reswap response header
@@ -271,7 +259,7 @@ extension HTTPFields {
271259
///
272260
/// [https://htmx.org/reference/#response_headers](https://htmx.org/reference/#response_headers)
273261
public var hxReswap: HxSwap? {
274-
return self[.hxReswap].flatMap(HxSwap.init(rawValue:))
262+
return self[.hxReswap].map(HxSwap.init(_:))
275263
}
276264

277265
/// Returns the value of an HX-Retarget response header
+12-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
/// Possible values for the HX-Push-URL header
2-
public enum HxPushUrl: Equatable, CustomStringConvertible {
2+
public struct HxPushUrl: Equatable, CustomStringConvertible {
3+
public let description: String
4+
5+
init(_ description: String) {
6+
self.description = description
7+
}
8+
}
9+
10+
extension HxPushUrl {
311
/// prevents the browser’s history from being updated.
4-
case `false`
12+
public static let `false` = HxPushUrl("false")
513

614
/// A URL to be pushed into the location bar. This may be relative or absolute, as per [`history.pushState()`](https://developer.mozilla.org/en-US/docs/Web/API/History/pushState).
7-
case url(String)
8-
9-
public var description: String {
10-
switch self {
11-
case .false: return "false"
12-
case .url(let raw): return raw
13-
}
15+
public static func url(_ string: String) -> HxPushUrl {
16+
return HxPushUrl(string)
1417
}
1518
}
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
/// Possible values for the HX-Replace-URL header
2-
public enum HxReplaceUrl: Equatable, CustomStringConvertible {
3-
/// false, which prevents the browser’s current URL from being updated.
4-
case `false`
2+
public struct HxReplaceUrl: Equatable, CustomStringConvertible {
3+
public let description: String
54

6-
/// A URL to replace the current URL in the location bar. This may be relative or absolute, as per [`history.replaceState()`](https://developer.mozilla.org/en-US/docs/Web/API/History/replaceState), but must have the same origin as the current URL.
7-
case url(String)
5+
init(_ description: String) {
6+
self.description = description
7+
}
8+
}
89

9-
public var description: String {
10-
switch self {
11-
case .false: return "false"
12-
case .url(let raw): return raw
13-
}
10+
extension HxReplaceUrl {
11+
/// false, prevents the browser’s current URL from being updated.
12+
public static let `false` = HxReplaceUrl("false")
13+
14+
/// A URL to replace the current URL in the location bar. This may be relative or absolute, as per [`history.replaceState()`](https://developer.mozilla.org/en-US/docs/Web/API/History/replaceState), but must have the same origin as the current URL.
15+
public static func url(_ string: String) -> HxReplaceUrl {
16+
return HxReplaceUrl(string)
1417
}
1518
}
+20-43
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,40 @@
11
/// Possible values for HX-Reswap header
22
///
33
/// [https://htmx.org/attributes/hx-swap/](https://htmx.org/attributes/hx-swap/)
4-
public enum HxSwap: RawRepresentable, Equatable, CustomStringConvertible {
5-
public typealias RawValue = String
4+
public struct HxSwap: Equatable, CustomStringConvertible, ExpressibleByStringLiteral {
5+
public let description: String
66

7+
public init(_ description: String) {
8+
self.description = description
9+
}
10+
11+
public init(stringLiteral value: StringLiteralType) {
12+
self.description = value
13+
}
14+
}
15+
16+
extension HxSwap {
717
/// Replace the inner html of the target element
8-
case innerHTML
18+
public static let innerHTML = HxSwap("innerHTML")
919

1020
/// Replace the entire target element with the response
11-
case outerHTML
21+
public static let outerHTML = HxSwap("outerHTML")
1222

1323
/// Insert the response before the target element
14-
case beforebegin
24+
public static let beforebegin = HxSwap("beforebegin")
1525

1626
/// Insert the response before the first child of the target element
17-
case afterbegin
27+
public static let afterbegin = HxSwap("afterbegin")
1828

1929
/// Insert the response after the last child of the target element
20-
case beforeend
30+
public static let beforeend = HxSwap("beforeend")
2131

2232
/// Insert the response after the target element
23-
case afterend
33+
public static let afterend = HxSwap("afterend")
2434

2535
/// Deletes the target element regardless of the response
26-
case delete
36+
public static let delete = HxSwap("delete")
2737

2838
/// Does not append content from response (out of band items will still be processed).
29-
case none
30-
31-
/// Fallback if you need a value other than the ones avaiable
32-
case raw(String)
33-
34-
public var rawValue: String {
35-
switch self {
36-
case .innerHTML: return "innerHTML"
37-
case .outerHTML: return "outerHTML"
38-
case .beforebegin: return "beforebegin"
39-
case .afterbegin: return "afterbegin"
40-
case .beforeend: return "beforeend"
41-
case .afterend: return "afterend"
42-
case .delete: return "delete"
43-
case .none: return "none"
44-
case .raw(let string): return string
45-
}
46-
}
47-
48-
public var description: String { rawValue }
49-
50-
public init(rawValue: String) {
51-
switch rawValue {
52-
case "innerHTML": self = .innerHTML
53-
case "outerHTML": self = .outerHTML
54-
case "beforebegin": self = .beforebegin
55-
case "afterbegin": self = .afterbegin
56-
case "beforeend": self = .beforeend
57-
case "afterend": self = .afterend
58-
case "delete": self = .delete
59-
case "none": self = .none
60-
default: self = .raw(rawValue)
61-
}
62-
}
39+
public static let none = HxSwap("none")
6340
}

Tests/HTTPTypesHtmxTests/HtmxResponseHeadersTests.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ final class ResponseHeadersTests: XCTestCase {
3636
XCTAssertEqual(HTTPField.hxReswap(.delete).description, "HX-Reswap: delete")
3737
XCTAssertEqual(HTTPField.hxReswap(.none).description, "HX-Reswap: none")
3838
XCTAssertEqual(
39-
HTTPField.hxReswap(.raw("raw this can be anything")).description,
39+
HTTPField.hxReswap("raw this can be anything").description,
4040
"HX-Reswap: raw this can be anything"
4141
)
4242
XCTAssertEqual(HTTPField.hxRetarget("cssSelector").description, "HX-Retarget: cssSelector")
@@ -114,7 +114,7 @@ final class ResponseHeadersTests: XCTestCase {
114114
(HTTPField.hxReswap(.afterend), .afterend),
115115
(HTTPField.hxReswap(.delete), .delete),
116116
(HTTPField.hxReswap(.none), .none),
117-
(HTTPField.hxReswap(.raw("any value")), .raw("any value")),
117+
(HTTPField.hxReswap("any value"), "any value"),
118118
]
119119
for (header, value) in headersHxReswap {
120120
XCTAssertEqual(HTTPFields([header]).hxReswap, value)

0 commit comments

Comments
 (0)