metatext/Shared/Model/Unknowable.swift

18 lines
456 B
Swift
Raw Normal View History

2020-08-07 01:41:59 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
import Foundation
protocol Unknowable: RawRepresentable, CaseIterable where RawValue: Equatable {
2020-08-07 03:57:52 +00:00
static var unknownCase: Self { get }
2020-08-07 01:41:59 +00:00
}
extension Unknowable {
init(rawValue: RawValue) {
2020-08-07 03:57:52 +00:00
self = Self.allCases.first { $0.rawValue == rawValue } ?? Self.unknownCase
2020-08-07 01:41:59 +00:00
}
}
2020-08-07 10:14:14 +00:00
extension Unknowable {
static var allCasesExceptUnknown: [Self] { allCases.filter { $0 != unknownCase } }
}