SourceGenerator.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. /// Whether imports have explicit access levels.
  29. public var accessLevelOnImports: Bool
  30. /// The indentation of the generated code as the number of spaces.
  31. public var indentation: Int
  32. /// Whether or not client code should be generated.
  33. public var client: Bool
  34. /// Whether or not server code should be generated.
  35. public var server: Bool
  36. /// Creates a new configuration.
  37. ///
  38. /// - Parameters:
  39. /// - accessLevel: The access level the generated code will have.
  40. /// - accessLevelOnImports: Whether imports have explicit access levels.
  41. /// - client: Whether or not client code should be generated.
  42. /// - server: Whether or not server code should be generated.
  43. /// - indentation: The indentation of the generated code as the number of spaces.
  44. public init(
  45. accessLevel: AccessLevel,
  46. accessLevelOnImports: Bool,
  47. client: Bool,
  48. server: Bool,
  49. indentation: Int = 4
  50. ) {
  51. self.accessLevel = accessLevel
  52. self.accessLevelOnImports = accessLevelOnImports
  53. self.indentation = indentation
  54. self.client = client
  55. self.server = server
  56. }
  57. /// The possible access levels for the generated code.
  58. public struct AccessLevel: Sendable, Hashable {
  59. internal var level: Level
  60. internal enum Level {
  61. case `internal`
  62. case `public`
  63. case `package`
  64. }
  65. /// The generated code will have `internal` access level.
  66. public static var `internal`: Self { Self(level: .`internal`) }
  67. /// The generated code will have `public` access level.
  68. public static var `public`: Self { Self(level: .`public`) }
  69. /// The generated code will have `package` access level.
  70. public static var `package`: Self { Self(level: .`package`) }
  71. }
  72. }
  73. /// The function that transforms a ``CodeGenerationRequest`` object into a ``SourceFile`` object containing
  74. /// the generated code, in accordance to the configurations set by the user for the ``SourceGenerator``.
  75. public func generate(
  76. _ request: CodeGenerationRequest
  77. ) throws -> SourceFile {
  78. let translator = IDLToStructuredSwiftTranslator()
  79. let textRenderer = TextBasedRenderer(indentation: self.configuration.indentation)
  80. let structuredSwiftRepresentation = try translator.translate(
  81. codeGenerationRequest: request,
  82. accessLevel: self.configuration.accessLevel,
  83. accessLevelOnImports: self.configuration.accessLevelOnImports,
  84. client: self.configuration.client,
  85. server: self.configuration.server
  86. )
  87. let sourceFile = try textRenderer.render(structured: structuredSwiftRepresentation)
  88. return sourceFile
  89. }
  90. }