Docs.swift 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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(gRPCSwift 2.0, *)
  17. package enum Docs {
  18. package static func suffix(_ header: String, withDocs footer: String) -> String {
  19. if footer.isEmpty {
  20. return header
  21. } else {
  22. let docs = """
  23. ///
  24. \(Self.inlineDocsAsNote(footer))
  25. """
  26. return header + "\n" + docs
  27. }
  28. }
  29. package static func interposeDocs(
  30. _ docs: String,
  31. between header: String,
  32. and footer: String
  33. ) -> String {
  34. let middle: String
  35. if docs.isEmpty {
  36. middle = """
  37. ///
  38. """
  39. } else {
  40. middle = """
  41. ///
  42. \(Self.inlineDocsAsNote(docs))
  43. ///
  44. """
  45. }
  46. return header + "\n" + middle + "\n" + footer
  47. }
  48. private static func inlineDocsAsNote(_ docs: String) -> String {
  49. let header = """
  50. /// > Source IDL Documentation:
  51. /// >
  52. """
  53. let body = docs.split(separator: "\n").map { line in
  54. var line = "/// > " + line.dropFirst(4)
  55. line.trimPrefix(while: { $0.isWhitespace })
  56. return String(line.drop(while: { $0.isWhitespace }))
  57. }.joined(separator: "\n")
  58. return header + "\n" + body
  59. }
  60. }