diff --git a/Sources/Leaf/Parser/LeafParser.swift b/Sources/Leaf/Parser/LeafParser.swift index b50e082..8931e62 100644 --- a/Sources/Leaf/Parser/LeafParser.swift +++ b/Sources/Leaf/Parser/LeafParser.swift @@ -156,24 +156,23 @@ extension TemplateByteScanner { case Data(bytes: [.forwardSlash, .forwardSlash]), Data(bytes: [.forwardSlash, .asterisk]): break default: - throw TemplateError.parse(reason: "Invalid tag name", template: makeSource(using: start), source: .capture()) + throw TemplateKitError(identifier: "parse", reason: "Invalid tag name", source: makeSource(using: start)) } } // Extract the tag params. let params: [TemplateSyntax] guard let name = String(data: id, encoding: .utf8) else { - throw TemplateError.parse(reason: "Invalid UTF-8 string", template: makeSource(using: start), source: .capture()) + throw TemplateKitError(identifier: "parse", reason: "Invalid UTF-8 string", source: makeSource(using: start)) } switch name { case "for": try expect(.leftParenthesis) if peek() == .space { - throw TemplateError.parse( + throw TemplateKitError(identifier: "parse", reason: "Whitespace not allowed before key in 'for' tag.", - template: makeSource(using: start), - source: .capture() + source: makeSource(using: start) ) } let key = try extractIdentifier() @@ -182,44 +181,34 @@ extension TemplateByteScanner { try expect(.n) try expect(.space) guard let val = try extractParameter() else { - throw TemplateError.parse(reason: "Parameter required after `in` in for-loop", template: makeSource(using: start), source: .capture()) + throw TemplateKitError(identifier: "parse", reason: "Parameter required after `in` in for-loop", source: makeSource(using: start)) } switch val.type { case .identifier, .tag: break default: - throw TemplateError.parse(reason: "Identifier or tag required", template: makeSource(using: start), source: .capture()) + throw TemplateKitError(identifier: "parse", reason: "Identifier or tag required", source: makeSource(using: start)) } if peek(by: -1) == .space { - throw TemplateError.parse( + throw TemplateKitError(identifier: "parse", reason: "Whitespace not allowed after value in 'for' tag.", - template: makeSource(using: start), - source: .capture() + source: makeSource(using: start) ) } try expect(.rightParenthesis) guard case .identifier(let name) = key.type else { - throw TemplateError.parse(reason: "Invalid key type in for-loop", template: makeSource(using: start), source: .capture()) + throw TemplateKitError(identifier: "parse", reason: "Invalid key type in for-loop", source: makeSource(using: start)) } guard name.path.count == 1 else { - throw TemplateError.parse(reason: "One key required in for-loop", template: makeSource(using: start), source: .capture()) + throw TemplateKitError(identifier: "parse", reason: "One key required in for-loop", source: makeSource(using: start)) } - guard let data = name.path[0].stringValue.data(using: .utf8) else { - throw TemplateError.parse(reason: "Invalid UTF-8 string", template: makeSource(using: start), source: .capture()) - } - - let raw = TemplateSyntax( - type: .raw(TemplateRaw(data: data)), - source: key.source - ) - let keyConstant = TemplateSyntax( - type: .constant(.string([raw])), + type: .constant(.string(name.path[0].stringValue)), source: key.source ) @@ -285,7 +274,7 @@ extension TemplateByteScanner { switch name { case "if": guard params.count == 1 else { - throw TemplateError.parse(reason: "One parameter required for if tag.", template: makeSource(using: start), source: .capture()) + throw TemplateKitError(identifier: "parse", reason: "One parameter required for if tag.", source: makeSource(using: start)) } let cond = try TemplateConditional( @@ -296,13 +285,13 @@ extension TemplateByteScanner { type = .conditional(cond) case "embed": guard params.count == 1 else { - throw TemplateError.parse(reason: "One parameter required for embed tag.", template: makeSource(using: start), source: .capture()) + throw TemplateKitError(identifier: "parse", reason: "One parameter required for embed tag.", source: makeSource(using: start)) } let embed = TemplateEmbed(path: params[0]) type = .embed(embed) case "for": guard params.count == 2 else { - throw TemplateError.parse(reason: "Two parameters required for for-loop.", template: makeSource(using: start), source: .capture()) + throw TemplateKitError(identifier: "parse", reason: "Two parameters required for for-loop.", source: makeSource(using: start)) } let iterator = TemplateIterator(key: params[1], data: params[0], body: body ?? []) type = .iterator(iterator) @@ -405,7 +394,7 @@ extension TemplateByteScanner { try extractSpaces() guard params.count == 1 else { - throw TemplateError.parse(reason: "One parameter required for else tag.", template: makeSource(using: start), source: .capture()) + throw TemplateKitError(identifier: "parse", reason: "One parameter required for else tag.", source: makeSource(using: start)) } return try TemplateConditional( @@ -581,16 +570,16 @@ extension TemplateByteScanner { let bytes = data[start.offset..Home

bar

""" - let data = try TemplateDataEncoder().encode(["foo": "bar"]) + let data = try TemplateDataEncoder().testEncode(["foo": "bar"]) try XCTAssertEqual(renderer.testRender(home, data), expected) } @@ -410,7 +410,7 @@ class LeafTests: XCTestCase { ziz: index=1 last=false first=false vapor: index=2 last=true first=false """ - let data = try TemplateDataEncoder().encode([ + let data = try TemplateDataEncoder().testEncode([ "names": ["tanner", "ziz", "vapor"] ]) try XCTAssertEqual(renderer.testRender(template, data), expected) diff --git a/Tests/LeafTests/TemplateDataEncoderTests.swift b/Tests/LeafTests/TemplateDataEncoderTests.swift index beaa215..f8f82aa 100644 --- a/Tests/LeafTests/TemplateDataEncoderTests.swift +++ b/Tests/LeafTests/TemplateDataEncoderTests.swift @@ -7,17 +7,17 @@ import XCTest class TemplateDataEncoderTests: XCTestCase { func testString() { let data = "hello" - try XCTAssertEqual(TemplateDataEncoder().encode(data), .string(data)) + try XCTAssertEqual(TemplateDataEncoder().testEncode(data), .string(data)) } func testDouble() { let data: Double = 3.14 - try XCTAssertEqual(TemplateDataEncoder().encode(data), .double(data)) + try XCTAssertEqual(TemplateDataEncoder().testEncode(data), .double(data)) } func testDictionary() { let data: [String: String] = ["string": "hello", "foo": "3.14"] - try XCTAssertEqual(TemplateDataEncoder().encode(data), .dictionary([ + try XCTAssertEqual(TemplateDataEncoder().testEncode(data), .dictionary([ "string": .string("hello"), "foo": .string("3.14") ])) @@ -28,7 +28,7 @@ class TemplateDataEncoderTests: XCTestCase { "a": ["string": "hello", "foo": "3.14"], "b": ["greeting": "hey", "foo": "3.15"] ] - try XCTAssertEqual(TemplateDataEncoder().encode(data), .dictionary([ + try XCTAssertEqual(TemplateDataEncoder().testEncode(data), .dictionary([ "a": .dictionary([ "string": .string("hello"), "foo": .string("3.14") @@ -42,21 +42,21 @@ class TemplateDataEncoderTests: XCTestCase { func testArray() { let data: [String] = ["string", "hello", "foo", "3.14"] - try XCTAssertEqual(TemplateDataEncoder().encode(data), .array([ + try XCTAssertEqual(TemplateDataEncoder().testEncode(data), .array([ .string("string"), .string("hello"), .string("foo"), .string("3.14") ])) } func testNestedArray() { let data: [[String]] = [["string"], ["hello", "foo"], ["3.14"]] - try XCTAssertEqual(TemplateDataEncoder().encode(data), .array([ + try XCTAssertEqual(TemplateDataEncoder().testEncode(data), .array([ .array([.string("string")]), .array([.string("hello"), .string("foo")]), .array([.string("3.14")]) ])) } func testEncodable() { struct Hello: Encodable { var hello = "hello" } - try XCTAssertEqual(TemplateDataEncoder().encode(Hello()), .dictionary([ + try XCTAssertEqual(TemplateDataEncoder().testEncode(Hello()), .dictionary([ "hello": .string("hello"), ])) } @@ -71,7 +71,7 @@ class TemplateDataEncoderTests: XCTestCase { var fib: [Int] = [0, 1, 1, 2, 3, 5, 8, 13] } - try XCTAssertEqual(TemplateDataEncoder().encode(Test()), .dictionary([ + try XCTAssertEqual(TemplateDataEncoder().testEncode(Test()), .dictionary([ "string": .string("hello"), "double": .double(3.14), "int": .int(42), @@ -95,7 +95,7 @@ class TemplateDataEncoderTests: XCTestCase { } let sub = Test() - try XCTAssertEqual(TemplateDataEncoder().encode(Test(sub: sub)), .dictionary([ + try XCTAssertEqual(TemplateDataEncoder().testEncode(Test(sub: sub)), .dictionary([ "string": .string("hello"), "double": .double(3.14), "int": .int(42), @@ -122,3 +122,10 @@ class TemplateDataEncoderTests: XCTestCase { ("testNestedEncodable", testNestedEncodable), ] } + + +extension TemplateDataEncoder { + func testEncode(_ encodable: E) throws -> TemplateData where E: Encodable { + return try encode(encodable, on: EmbeddedEventLoop()).wait() + } +}