Implemented the TemplateService service in the library target.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import Foundation
|
||||
|
||||
public struct FileService: FileServicing {
|
||||
|
||||
public struct FileService {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
private let fileManager: FileManager
|
||||
@@ -12,6 +12,12 @@ public struct FileService: FileServicing {
|
||||
self.fileManager = fileManager
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - FileServicing
|
||||
|
||||
extension FileService: FileServicing {
|
||||
|
||||
// MARK: Computed
|
||||
|
||||
public var currentFolder: URL {
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
import Foundation
|
||||
import Mustache
|
||||
|
||||
public struct TemplateService {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
private let mustacheRenderer: MustacheLibrary
|
||||
|
||||
// MARK: Initialisers
|
||||
|
||||
public init(
|
||||
bundle: Bundleable? = nil,
|
||||
templateFolder: String
|
||||
) async throws (TemplateServiceError) {
|
||||
guard let pathResources = (bundle ?? Bundle.module).resourcePath else {
|
||||
throw .resourcePathNotFound
|
||||
}
|
||||
|
||||
let pathTemplates = pathResources + "/" + templateFolder
|
||||
|
||||
do {
|
||||
self.mustacheRenderer = try await MustacheLibrary(directory: pathTemplates)
|
||||
} catch {
|
||||
throw .serviceNotInitialized
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - TemplateServicing
|
||||
|
||||
extension TemplateService: TemplateServicing {
|
||||
|
||||
// MARK: Functions
|
||||
|
||||
public func render(_ object: Any, on template: String) async throws (TemplateServiceError) -> String {
|
||||
guard mustacheRenderer.getTemplate(named: template) != nil else {
|
||||
throw .templateNotFound
|
||||
}
|
||||
|
||||
guard let content = mustacheRenderer.render(object, withTemplate: template) else {
|
||||
throw .contentNotRendered
|
||||
}
|
||||
|
||||
return content
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user