From aa30dcbddf8e898099dc922920a03720d30684cd Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Thu, 18 Mar 2021 15:25:46 +0000 Subject: [PATCH] Don't log files that dont load throw error instead remove swift-log dependency --- Package.swift | 8 ++------ Sources/HummingbirdMustache/Library+FileSystem.swift | 11 ++++++----- Sources/HummingbirdMustache/Library.swift | 8 ++++++-- Tests/HummingbirdMustacheTests/LibraryTests.swift | 2 +- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/Package.swift b/Package.swift index a2dae55..692780d 100644 --- a/Package.swift +++ b/Package.swift @@ -8,13 +8,9 @@ let package = Package( products: [ .library(name: "HummingbirdMustache", targets: ["HummingbirdMustache"]), ], - dependencies: [ - .package(url: "https://github.com/apple/swift-log.git", from: "1.4.0"), - ], + dependencies: [], targets: [ - .target(name: "HummingbirdMustache", dependencies: [ - .product(name: "Logging", package: "swift-log"), - ]), + .target(name: "HummingbirdMustache", dependencies: []), .testTarget(name: "HummingbirdMustacheTests", dependencies: ["HummingbirdMustache"]), ] ) diff --git a/Sources/HummingbirdMustache/Library+FileSystem.swift b/Sources/HummingbirdMustache/Library+FileSystem.swift index 1234fbe..45500ef 100644 --- a/Sources/HummingbirdMustache/Library+FileSystem.swift +++ b/Sources/HummingbirdMustache/Library+FileSystem.swift @@ -3,7 +3,7 @@ import Logging extension HBMustacheLibrary { /// Load templates from a folder - func loadTemplates(from directory: String, withExtension extension: String = "mustache", logger: Logger?) { + func loadTemplates(from directory: String, withExtension extension: String = "mustache") throws { var directory = directory if !directory.hasSuffix("/") { directory += "/" @@ -15,11 +15,12 @@ extension HBMustacheLibrary { guard path.hasSuffix(extWithDot) else { continue } guard let data = fs.contents(atPath: directory + path) else { continue } let string = String(decoding: data, as: Unicode.UTF8.self) - guard let template = try? HBMustacheTemplate(string: string) else { - logger?.error("Failed to load \(path)") - continue + let template: HBMustacheTemplate + do { + template = try HBMustacheTemplate(string: string) + } catch { + throw Error.failedToLoad(path, error) } - logger?.debug("Loading \(path)") // drop ".mustache" from path to get name let name = String(path.dropLast(extWithDot.count)) register(template, named: name) diff --git a/Sources/HummingbirdMustache/Library.swift b/Sources/HummingbirdMustache/Library.swift index 4b7a6c5..08e1a6e 100644 --- a/Sources/HummingbirdMustache/Library.swift +++ b/Sources/HummingbirdMustache/Library.swift @@ -18,9 +18,9 @@ public final class HBMustacheLibrary { /// the folder is recursive and templates in subfolders will be registered with the name `subfolder/template`. /// - Parameter directory: Directory to look for mustache templates /// - Parameter extension: Extension of files to look for - public init(directory: String, withExtension extension: String = "mustache", logger: Logger? = nil) { + public init(directory: String, withExtension extension: String = "mustache") throws { templates = [:] - loadTemplates(from: directory, withExtension: `extension`, logger: logger) + try loadTemplates(from: directory, withExtension: `extension`) } /// Register template under name @@ -49,5 +49,9 @@ public final class HBMustacheLibrary { return template.render(object) } + public enum Error: Swift.Error { + case failedToLoad(String, Swift.Error) + } + private var templates: [String: HBMustacheTemplate] } diff --git a/Tests/HummingbirdMustacheTests/LibraryTests.swift b/Tests/HummingbirdMustacheTests/LibraryTests.swift index 75de125..3d591f6 100644 --- a/Tests/HummingbirdMustacheTests/LibraryTests.swift +++ b/Tests/HummingbirdMustacheTests/LibraryTests.swift @@ -11,7 +11,7 @@ final class LibraryTests: XCTestCase { try data.write(to: URL(fileURLWithPath: "templates/test.mustache")) defer { XCTAssertNoThrow(try fs.removeItem(atPath: "templates/test.mustache")) } - let library = HBMustacheLibrary(directory: "./templates") + let library = try HBMustacheLibrary(directory: "./templates") let object = ["value": ["value1", "value2"]] XCTAssertEqual(library.render(object, withTemplate: "test"), "value1value2") }