SourceGenerator.swift 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright 2024, gRPC Authors 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. /// Creates a ``SourceFile`` containing the generated code for the RPCs represented in a ``CodeGenerationRequest`` object.
  17. public struct SourceGenerator: Sendable {
  18. /// The options regarding the access level, indentation for the generated code
  19. /// and whether to generate server and client code.
  20. public var configuration: Configuration
  21. public init(configuration: Configuration) {
  22. self.configuration = configuration
  23. }
  24. /// User options for the CodeGeneration.
  25. public struct Configuration: Sendable {
  26. /// The access level the generated code will have.
  27. public var accessLevel: AccessLevel
  28. /// The indentation of the generated code as the number of spaces.
  29. public var indentation: Int
  30. /// Whether or not client code should be generated.
  31. public var client: Bool
  32. /// Whether or not server code should be generated.
  33. public var server: Bool
  34. /// The possible access levels for the generated code.
  35. public struct AccessLevel: Sendable, Hashable {
  36. internal var level: Level
  37. internal enum Level {
  38. case `internal`
  39. case `public`
  40. case `package`
  41. }
  42. /// The generated code will have `internal` access level.
  43. public static var `internal`: Self { Self(level: .`internal`) }
  44. /// The generated code will have `public` access level.
  45. public static var `public`: Self { Self(level: .`public`) }
  46. /// The generated code will have `package` access level.
  47. public static var `package`: Self { Self(level: .`package`) }
  48. }
  49. }
  50. /// The function that transforms a ``CodeGenerationRequest`` object into a ``SourceFile`` object containing
  51. /// the generated code, in accordance to the configurations set by the user for the ``SourceGenerator``.
  52. public func generate(
  53. _ serviceRepresentation: CodeGenerationRequest
  54. ) throws -> SourceFile {
  55. let translator = IDLToStructuredSwiftTranslator()
  56. let textRenderer = TextBasedRenderer(indentation: self.configuration.indentation)
  57. let structuredSwiftRepresentation = try translator.translate(
  58. codeGenerationRequest: serviceRepresentation,
  59. accessLevel: self.configuration.accessLevel,
  60. client: configuration.client,
  61. server: configuration.server
  62. )
  63. let sourceFile = try textRenderer.render(structured: structuredSwiftRepresentation)
  64. return sourceFile
  65. }
  66. }