SourceGenerator.swift 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. public init(accessLevel: AccessLevel, client: Bool, server: Bool, indentation: Int = 4) {
  35. self.accessLevel = accessLevel
  36. self.indentation = indentation
  37. self.client = client
  38. self.server = server
  39. }
  40. /// The possible access levels for the generated code.
  41. public struct AccessLevel: Sendable, Hashable {
  42. internal var level: Level
  43. internal enum Level {
  44. case `internal`
  45. case `public`
  46. case `package`
  47. }
  48. /// The generated code will have `internal` access level.
  49. public static var `internal`: Self { Self(level: .`internal`) }
  50. /// The generated code will have `public` access level.
  51. public static var `public`: Self { Self(level: .`public`) }
  52. /// The generated code will have `package` access level.
  53. public static var `package`: Self { Self(level: .`package`) }
  54. }
  55. }
  56. /// The function that transforms a ``CodeGenerationRequest`` object into a ``SourceFile`` object containing
  57. /// the generated code, in accordance to the configurations set by the user for the ``SourceGenerator``.
  58. public func generate(
  59. _ serviceRepresentation: CodeGenerationRequest
  60. ) throws -> SourceFile {
  61. let translator = IDLToStructuredSwiftTranslator()
  62. let textRenderer = TextBasedRenderer(indentation: self.configuration.indentation)
  63. let structuredSwiftRepresentation = try translator.translate(
  64. codeGenerationRequest: serviceRepresentation,
  65. accessLevel: self.configuration.accessLevel,
  66. client: configuration.client,
  67. server: configuration.server
  68. )
  69. let sourceFile = try textRenderer.render(structured: structuredSwiftRepresentation)
  70. return sourceFile
  71. }
  72. }