forked from IBAnimatable/IBAnimatable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CornerSide.swift
executable file
·70 lines (56 loc) · 1.66 KB
/
CornerSide.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
//
// CornerSide.swift
// IBAnimatable
//
// Created by Miroslav Valkovic-Madjer on 17/11/16.
// Copyright © 2016 IBAnimatable. All rights reserved.
//
import Foundation
public enum CornerSide: String {
case topLeft = "topleft"
case topRight = "topright"
case bottomLeft = "bottomleft"
case bottomRight = "bottomright"
}
#if swift(>=4.2)
extension CornerSide: CaseIterable {}
#endif
public struct CornerSides: OptionSet {
public let rawValue: Int
public static let unknown = CornerSides(rawValue: 0)
public static let topLeft = CornerSides(rawValue: 1)
public static let topRight = CornerSides(rawValue: 1 << 1)
public static let bottomLeft = CornerSides(rawValue: 1 << 2)
public static let bottomRight = CornerSides(rawValue: 1 << 3)
public static let allSides: CornerSides = [.topLeft, .topRight, .bottomLeft, .bottomRight]
public init(rawValue: Int) {
self.rawValue = rawValue
}
init(rawValue: String?) {
guard let rawValue = rawValue, !rawValue.isEmpty else {
self = .allSides
return
}
let sideElements = rawValue.lowercased().split(separator: ",")
.map(String.init)
.map { CornerSide(rawValue: $0.trimmingCharacters(in: CharacterSet.whitespaces)) }
.map { CornerSides(side: $0) }
guard !sideElements.contains(.unknown) else {
self = .allSides
return
}
self = CornerSides(sideElements)
}
init(side: CornerSide?) {
guard let side = side else {
self = .unknown
return
}
switch side {
case .topLeft: self = .topLeft
case .topRight: self = .topRight
case .bottomLeft: self = .bottomLeft
case .bottomRight: self = .bottomRight
}
}
}