CodeGenerator.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. @available(*, deprecated, renamed: "CodeGenerator")
  17. public typealias SourceGenerator = CodeGenerator
  18. /// Generates ``SourceFile`` objects containing generated code for the RPCs represented
  19. /// in a ``CodeGenerationRequest`` object.
  20. public struct CodeGenerator: Sendable {
  21. /// The options regarding the access level, indentation for the generated code
  22. /// and whether to generate server and client code.
  23. public var config: Config
  24. public init(config: Config) {
  25. self.config = config
  26. }
  27. /// User options for the CodeGeneration.
  28. public struct Config: Sendable {
  29. /// The access level the generated code will have.
  30. public var accessLevel: AccessLevel
  31. /// Whether imports have explicit access levels.
  32. public var accessLevelOnImports: Bool
  33. /// The indentation of the generated code as the number of spaces.
  34. public var indentation: Int
  35. /// Whether or not client code should be generated.
  36. public var client: Bool
  37. /// Whether or not server code should be generated.
  38. public var server: Bool
  39. /// Creates a new configuration.
  40. ///
  41. /// - Parameters:
  42. /// - accessLevel: The access level the generated code will have.
  43. /// - accessLevelOnImports: Whether imports have explicit access levels.
  44. /// - client: Whether or not client code should be generated.
  45. /// - server: Whether or not server code should be generated.
  46. /// - indentation: The indentation of the generated code as the number of spaces.
  47. public init(
  48. accessLevel: AccessLevel,
  49. accessLevelOnImports: Bool,
  50. client: Bool,
  51. server: Bool,
  52. indentation: Int = 4
  53. ) {
  54. self.accessLevel = accessLevel
  55. self.accessLevelOnImports = accessLevelOnImports
  56. self.indentation = indentation
  57. self.client = client
  58. self.server = server
  59. }
  60. /// The possible access levels for the generated code.
  61. public struct AccessLevel: Sendable, Hashable {
  62. package var level: Level
  63. package enum Level {
  64. case `internal`
  65. case `public`
  66. case `package`
  67. }
  68. /// The generated code will have `internal` access level.
  69. public static var `internal`: Self { Self(level: .`internal`) }
  70. /// The generated code will have `public` access level.
  71. public static var `public`: Self { Self(level: .`public`) }
  72. /// The generated code will have `package` access level.
  73. public static var `package`: Self { Self(level: .`package`) }
  74. }
  75. }
  76. /// Transforms a ``CodeGenerationRequest`` object into a ``SourceFile`` object containing
  77. /// the generated code.
  78. public func generate(
  79. _ request: CodeGenerationRequest
  80. ) throws -> SourceFile {
  81. let translator = IDLToStructuredSwiftTranslator()
  82. let textRenderer = TextBasedRenderer(indentation: self.config.indentation)
  83. let structuredSwiftRepresentation = try translator.translate(
  84. codeGenerationRequest: request,
  85. accessLevel: self.config.accessLevel,
  86. accessLevelOnImports: self.config.accessLevelOnImports,
  87. client: self.config.client,
  88. server: self.config.server
  89. )
  90. let sourceFile = try textRenderer.render(structured: structuredSwiftRepresentation)
  91. return sourceFile
  92. }
  93. }