29 lines
736 B
Swift
29 lines
736 B
Swift
//
|
|
// Copyright 2022 Signal Messenger, LLC
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
//
|
|
|
|
import Foundation
|
|
|
|
extension CaseIterable where Self: Equatable {
|
|
|
|
public func previous() -> Self {
|
|
let all = Self.allCases
|
|
var idx = all.firstIndex(of: self)!
|
|
if idx == all.startIndex {
|
|
let lastIndex = all.index(all.endIndex, offsetBy: -1)
|
|
return all[lastIndex]
|
|
} else {
|
|
all.formIndex(&idx, offsetBy: -1)
|
|
return all[idx]
|
|
}
|
|
}
|
|
|
|
public func next() -> Self {
|
|
let all = Self.allCases
|
|
let idx = all.firstIndex(of: self)!
|
|
let next = all.index(after: idx)
|
|
return all[next == all.endIndex ? all.startIndex : next]
|
|
}
|
|
|
|
}
|