Operators.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Operators.swift
  2. // Eureka ( https://github.com/xmartlabs/Eureka )
  3. //
  4. // Copyright (c) 2016 Xmartlabs ( http://xmartlabs.com )
  5. //
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. import Foundation
  25. // MARK: Operators
  26. precedencegroup FormPrecedence {
  27. associativity: left
  28. higherThan: LogicalConjunctionPrecedence
  29. }
  30. precedencegroup SectionPrecedence {
  31. associativity: left
  32. higherThan: FormPrecedence
  33. }
  34. infix operator +++ : FormPrecedence
  35. /**
  36. Appends a section to a form
  37. - parameter left: the form
  38. - parameter right: the section to be appended
  39. - returns: the updated form
  40. */
  41. @discardableResult
  42. public func +++ (left: Form, right: Section) -> Form {
  43. left.append(right)
  44. return left
  45. }
  46. /**
  47. Appends a row to the last section of a form
  48. - parameter left: the form
  49. - parameter right: the row
  50. */
  51. @discardableResult
  52. public func +++ (left: Form, right: BaseRow) -> Form {
  53. let section = Section()
  54. let _ = left +++ section <<< right
  55. return left
  56. }
  57. /**
  58. Creates a form with two sections
  59. - parameter left: the first section
  60. - parameter right: the second section
  61. - returns: the created form
  62. */
  63. @discardableResult
  64. public func +++ (left: Section, right: Section) -> Form {
  65. let form = Form()
  66. let _ = form +++ left +++ right
  67. return form
  68. }
  69. /**
  70. Appends the row wrapped in a new section
  71. - parameter left: a section of the form
  72. - parameter right: a row to be appended
  73. - returns: the form
  74. */
  75. @discardableResult
  76. public func +++ (left: Section, right: BaseRow) -> Form {
  77. let section = Section()
  78. section <<< right
  79. return left +++ section
  80. }
  81. /**
  82. Creates a form with two sections, each containing one row.
  83. - parameter left: The row for the first section
  84. - parameter right: The row for the second section
  85. - returns: the created form
  86. */
  87. @discardableResult
  88. public func +++ (left: BaseRow, right: BaseRow) -> Form {
  89. let form = Section() <<< left +++ Section() <<< right
  90. return form
  91. }
  92. infix operator <<< : SectionPrecedence
  93. /**
  94. Appends a row to a section.
  95. - parameter left: the section
  96. - parameter right: the row to be appended
  97. - returns: the section
  98. */
  99. @discardableResult
  100. public func <<< (left: Section, right: BaseRow) -> Section {
  101. left.append(right)
  102. return left
  103. }
  104. /**
  105. Creates a section with two rows
  106. - parameter left: The first row
  107. - parameter right: The second row
  108. - returns: the created section
  109. */
  110. @discardableResult
  111. public func <<< (left: BaseRow, right: BaseRow) -> Section {
  112. let section = Section()
  113. section <<< left <<< right
  114. return section
  115. }
  116. /**
  117. Appends a collection of rows to a section
  118. - parameter lhs: the section
  119. - parameter rhs: the rows to be appended
  120. */
  121. public func += <C: Collection>(lhs: inout Section, rhs: C) where C.Iterator.Element == BaseRow {
  122. lhs.append(contentsOf: rhs)
  123. }
  124. /**
  125. Appends a collection of section to a form
  126. - parameter lhs: the form
  127. - parameter rhs: the sections to be appended
  128. */
  129. public func += <C: Collection>(lhs: inout Form, rhs: C) where C.Iterator.Element == Section {
  130. lhs.append(contentsOf: rhs)
  131. }