PartsRepresentable.swift 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright 2023 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import Foundation
  15. /// A protocol describing any data that could be serialized to model-interpretable input data,
  16. /// where the serialization process cannot fail with an error.
  17. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  18. public protocol PartsRepresentable {
  19. var partsValue: [any Part] { get }
  20. }
  21. /// Enables a ``Part`` to be used as a ``PartsRepresentable``.
  22. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  23. public extension Part {
  24. var partsValue: [any Part] {
  25. return [self]
  26. }
  27. }
  28. /// Enable an `Array` of ``PartsRepresentable`` values to be passed in as a single
  29. /// ``PartsRepresentable``.
  30. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  31. extension [PartsRepresentable]: PartsRepresentable {
  32. public var partsValue: [any Part] {
  33. return flatMap { $0.partsValue }
  34. }
  35. }
  36. /// Enables a `String` to be passed in as ``PartsRepresentable``.
  37. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  38. extension String: PartsRepresentable {
  39. public var partsValue: [any Part] {
  40. return [TextPart(self)]
  41. }
  42. }