CornerSide.swift 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //
  2. // CornerSide.swift
  3. // IBAnimatable
  4. //
  5. // Created by Miroslav Valkovic-Madjer on 17/11/16.
  6. // Copyright © 2016 IBAnimatable. All rights reserved.
  7. //
  8. import Foundation
  9. public enum CornerSide: String {
  10. case topLeft = "topleft"
  11. case topRight = "topright"
  12. case bottomLeft = "bottomleft"
  13. case bottomRight = "bottomright"
  14. }
  15. #if swift(>=4.2)
  16. extension CornerSide: CaseIterable {}
  17. #endif
  18. public struct CornerSides: OptionSet {
  19. public let rawValue: Int
  20. public static let unknown = CornerSides(rawValue: 0)
  21. public static let topLeft = CornerSides(rawValue: 1)
  22. public static let topRight = CornerSides(rawValue: 1 << 1)
  23. public static let bottomLeft = CornerSides(rawValue: 1 << 2)
  24. public static let bottomRight = CornerSides(rawValue: 1 << 3)
  25. public static let allSides: CornerSides = [.topLeft, .topRight, .bottomLeft, .bottomRight]
  26. public init(rawValue: Int) {
  27. self.rawValue = rawValue
  28. }
  29. init(rawValue: String?) {
  30. guard let rawValue = rawValue, !rawValue.isEmpty else {
  31. self = .allSides
  32. return
  33. }
  34. let sideElements = rawValue.lowercased().split(separator: ",")
  35. .map(String.init)
  36. .map { CornerSide(rawValue: $0.trimmingCharacters(in: CharacterSet.whitespaces)) }
  37. .map { CornerSides(side: $0) }
  38. guard !sideElements.contains(.unknown) else {
  39. self = .allSides
  40. return
  41. }
  42. self = CornerSides(sideElements)
  43. }
  44. init(side: CornerSide?) {
  45. guard let side = side else {
  46. self = .unknown
  47. return
  48. }
  49. switch side {
  50. case .topLeft: self = .topLeft
  51. case .topRight: self = .topRight
  52. case .bottomLeft: self = .bottomLeft
  53. case .bottomRight: self = .bottomRight
  54. }
  55. }
  56. }