Skip to content

Commit 350eee7

Browse files
author
Clément Le Provost
committed
[refact] Restore true and false cases in TypoTolerance
As per [SE-0071](https://github.com/apple/swift-evolution/blob/master/proposals/0071-member-keywords.md), most keywords can be used as enumeration cases, provided that they are escaped with back quotes in the declaration. They can then be used at the call site without escaping. => I restored the `true` and `false` cases for `TypoTolerance`, as it is more convenient to use `.true` than `.bool(true)`.
1 parent 84e13f5 commit 350eee7

File tree

2 files changed

+15
-41
lines changed

2 files changed

+15
-41
lines changed

Source/Query.swift

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -259,43 +259,17 @@ public class Query : NSObject, NSCopying {
259259
// NOTE: Objective-C bridge moved away to `_objc_Query`
260260

261261
/// Values applicable to the `typoTolerance` parameter.
262-
public enum TypoTolerance: Equatable {
263-
/// Activate or de-activate the typo tolerance entirely.
264-
case bool(Bool)
262+
public enum TypoTolerance: String {
263+
/// Activate typo tolerance entirely.
264+
case `true` = "true"
265+
/// De-activate typo tolerance entirely.
266+
case `false` = "false"
265267
/// Keep only results with the lowest number of typo. For example if one result match without typos, then
266268
/// all results with typos will be hidden.
267-
case min
269+
case min = "min"
268270
/// If there is a match without typo, then all results with 2 typos or more will be removed. This
269271
/// option is useful if you want to avoid as much as possible false positive.
270-
case strict
271-
272-
// NOTE: Associated values disable the ability to convert to/from raw values, so we emulate it ourselves.
273-
public var rawValue: String {
274-
switch self {
275-
case let .bool(value): return String(value)
276-
case .min: return "min"
277-
case .strict: return "strict"
278-
}
279-
}
280-
static func from(rawValue: String) -> TypoTolerance? {
281-
switch rawValue {
282-
case "true": return .bool(true)
283-
case "false": return .bool(false)
284-
case "min": return .min
285-
case "strict": return .strict
286-
default: return nil
287-
}
288-
}
289-
290-
// NOTE: Associated values disable automatic conformance to `Equatable`, so we have to implement it ourselves.
291-
static public func ==(lhs: TypoTolerance, rhs: TypoTolerance) -> Bool {
292-
switch (lhs, rhs) {
293-
case (let .bool(lhsValue), let .bool(rhsValue)): return lhsValue == rhsValue
294-
case (.min, .min): return true
295-
case (.strict, .strict): return true
296-
default: return false
297-
}
298-
}
272+
case strict = "strict"
299273
}
300274
/// This setting has four different options:
301275
/// - `true`: activate the typo-tolerance.
@@ -307,7 +281,7 @@ public class Query : NSObject, NSCopying {
307281
public var typoTolerance: TypoTolerance? {
308282
get {
309283
if let value = self["typoTolerance"] {
310-
return TypoTolerance.from(rawValue: value)
284+
return TypoTolerance(rawValue: value)
311285
} else {
312286
return nil
313287
}
@@ -1103,7 +1077,7 @@ public class _objc_Query: Query {
11031077
@objc(typoTolerance)
11041078
public var _typoTolerance: String? {
11051079
get { return typoTolerance?.rawValue }
1106-
set { typoTolerance = newValue == nil ? nil : TypoTolerance.from(rawValue: newValue!) }
1080+
set { typoTolerance = newValue == nil ? nil : TypoTolerance(rawValue: newValue!) }
11071081
}
11081082

11091083
@objc(minWordSizefor1Typo)

Tests/QueryTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -331,17 +331,17 @@ class QueryTests: XCTestCase {
331331
let query1 = Query()
332332
XCTAssertNil(query1.typoTolerance)
333333

334-
query1.typoTolerance = .bool(true)
335-
XCTAssertEqual(query1.typoTolerance, .bool(true))
334+
query1.typoTolerance = .true
335+
XCTAssertEqual(query1.typoTolerance, .true)
336336
XCTAssertEqual(query1["typoTolerance"], "true")
337337
var query2 = Query.parse(query1.build())
338-
XCTAssertEqual(query2.typoTolerance, .bool(true))
338+
XCTAssertEqual(query2.typoTolerance, .true)
339339

340-
query1.typoTolerance = .bool(false)
341-
XCTAssertEqual(query1.typoTolerance, .bool(false))
340+
query1.typoTolerance = .false
341+
XCTAssertEqual(query1.typoTolerance, .false)
342342
XCTAssertEqual(query1["typoTolerance"], "false")
343343
query2 = Query.parse(query1.build())
344-
XCTAssertEqual(query2.typoTolerance, .bool(false))
344+
XCTAssertEqual(query2.typoTolerance, .false)
345345

346346
query1.typoTolerance = .min
347347
XCTAssertEqual(query1.typoTolerance, .min)

0 commit comments

Comments
 (0)