2
0

QEMUArgumentBuilder.swift 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //
  2. // Copyright © 2022 osy. All rights reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. import Foundation
  17. @resultBuilder
  18. struct QEMUArgumentBuilder {
  19. static func buildBlock(_ components: [QEMUArgumentFragment]...) -> [QEMUArgumentFragment] {
  20. let merged = components.flatMap { $0 }
  21. var combined: [QEMUArgumentFragment] = []
  22. var current = QEMUArgumentFragment()
  23. for fragment in merged {
  24. current.merge(fragment)
  25. if current.isFinal {
  26. combined.append(current)
  27. current = QEMUArgumentFragment()
  28. }
  29. }
  30. if !current.string.isEmpty {
  31. combined.append(current)
  32. }
  33. return combined
  34. }
  35. static func buildExpression(_ fragment: QEMUArgumentFragment) -> [QEMUArgumentFragment] {
  36. [fragment]
  37. }
  38. static func buildExpression(_ fragments: [QEMUArgumentFragment]) -> [QEMUArgumentFragment] {
  39. fragments
  40. }
  41. static func buildExpression(_ arguments: [QEMUArgument]) -> [QEMUArgumentFragment] {
  42. arguments.map { QEMUArgumentFragment(from: $0) }
  43. }
  44. static func buildExpression(_ string: String) -> [QEMUArgumentFragment] {
  45. [.init(string)]
  46. }
  47. static func buildExpression(_ constant: any QEMUConstant) -> [QEMUArgumentFragment] {
  48. [.init(constant.rawValue)]
  49. }
  50. static func buildExpression(_ assignment: ()) -> [QEMUArgumentFragment] {
  51. []
  52. }
  53. static func buildExpression(_ url: URL) -> [QEMUArgumentFragment] {
  54. var arg = QEMUArgumentFragment(url.path)
  55. arg.fileUrls = [url]
  56. arg.seperator = ""
  57. return [arg]
  58. }
  59. static func buildExpression<I: FixedWidthInteger>(_ int: I) -> [QEMUArgumentFragment] {
  60. [.init("\(int)")]
  61. }
  62. static func buildEither(first component: [QEMUArgumentFragment]) -> [QEMUArgumentFragment] {
  63. component
  64. }
  65. static func buildEither(second component: [QEMUArgumentFragment]) -> [QEMUArgumentFragment] {
  66. component
  67. }
  68. static func buildArray(_ components: [[QEMUArgumentFragment]]) -> [QEMUArgumentFragment] {
  69. components.flatMap { $0 }
  70. }
  71. static func buildOptional(_ component: [QEMUArgumentFragment]?) -> [QEMUArgumentFragment] {
  72. component ?? []
  73. }
  74. static func buildFinalResult(_ component: [QEMUArgumentFragment]) -> [QEMUArgument] {
  75. component.map { QEMUArgument(from: $0) }
  76. }
  77. }