diff --git a/Package.swift b/Package.swift index 3321e2f..3234553 100644 --- a/Package.swift +++ b/Package.swift @@ -3,7 +3,7 @@ import PackageDescription let package = Package( name: "Leaf", dependencies: [ - .Package(url: "https://github.com/vapor/core.git", majorVersion: 1), - .Package(url: "https://github.com/vapor/node.git", majorVersion: 1) + .Package(url: "https://github.com/vapor/core.git", Version(2,0,0, prereleaseIdentifiers: ["alpha"])), + .Package(url: "https://github.com/vapor/node.git", Version(2,0,0, prereleaseIdentifiers: ["alpha"])) ] ) diff --git a/Sources/Leaf/Buffer/Buffer+Leaf.swift b/Sources/Leaf/Buffer/Buffer+Leaf.swift index 8d3bcb2..74b7956 100644 --- a/Sources/Leaf/Buffer/Buffer+Leaf.swift +++ b/Sources/Leaf/Buffer/Buffer+Leaf.swift @@ -71,7 +71,7 @@ extension BufferProtocol where Element == Byte { // check if body moveForward() - guard current == .space, next == .openCurly else { + guard current == .space, next == .leftCurlyBracket else { return TagTemplate(name: name, parameters: parameters, body: Leaf?.none) } moveForward() // queue up `{` @@ -91,20 +91,20 @@ extension BufferProtocol where Element == Byte { mutating func extractInstructionName() throws -> String { moveForward() // drop initial token from name. a secondary token implies chain - let name = extractUntil { $0 == .openParenthesis } - guard current == .openParenthesis else { + let name = extractUntil { $0 == .leftParenthesis } + guard current == .leftParenthesis else { throw ParseError.expectedOpenParenthesis } return name.string } mutating func extractInstructionParameters() throws -> [Parameter] { - return try extractSection(opensWith: .openParenthesis, closesWith: .closedParenthesis) + return try extractSection(opensWith: .leftParenthesis, closesWith: .rightParenthesis) .extractParameters() } mutating func extractBody() throws -> String { - return try extractSection(opensWith: .openCurly, closesWith: .closedCurly) + return try extractSection(opensWith: .leftCurlyBracket, closesWith: .rightCurlyBracket) .trimmed(.whitespace) .string } diff --git a/Sources/Leaf/Byte+Leaf.swift b/Sources/Leaf/Byte+Leaf.swift index 38744d4..41f557a 100644 --- a/Sources/Leaf/Byte+Leaf.swift +++ b/Sources/Leaf/Byte+Leaf.swift @@ -1,14 +1,3 @@ -// TODO: => Core -extension Byte { - public static let openParenthesis = "(".bytes.first! - public static let closedParenthesis = ")".bytes.first! - - public static let openCurly = "{".bytes.first! - public static let closedCurly = "}".bytes.first! - - public static let quotationMark = "\"".bytes.first! -} - extension Sequence where Iterator.Element == Byte { public static var whitespace: Bytes { return [ .space, .newLine, .carriageReturn, .horizontalTab] diff --git a/Sources/Leaf/HTMLEscape.swift b/Sources/Leaf/HTMLEscape.swift index a74e9fc..9215279 100644 --- a/Sources/Leaf/HTMLEscape.swift +++ b/Sources/Leaf/HTMLEscape.swift @@ -27,17 +27,17 @@ extension String { .replacingOccurrences(of: "<", with: "<") .replacingOccurrences(of: ">", with: ">") */ - return bytes + return makeBytes() .split(separator: .ampersand, omittingEmptySubsequences: false) - .joined(separator: "&".bytes) - .split(separator: .quotationMark, omittingEmptySubsequences: false) - .joined(separator: """.bytes) + .joined(separator: "&".makeBytes()) + .split(separator: .quote, omittingEmptySubsequences: false) + .joined(separator: """.makeBytes()) .split(separator: .apostrophe, omittingEmptySubsequences: false) - .joined(separator: "'".bytes) + .joined(separator: "'".makeBytes()) .split(separator: .lessThan, omittingEmptySubsequences: false) - .joined(separator: "<".bytes) + .joined(separator: "<".makeBytes()) .split(separator: .greaterThan, omittingEmptySubsequences: false) - .joined(separator: ">".bytes) + .joined(separator: ">".makeBytes()) .string } } diff --git a/Sources/Leaf/Node+Rendered.swift b/Sources/Leaf/Node+Rendered.swift index c60eb62..8d25d2b 100644 --- a/Sources/Leaf/Node+Rendered.swift +++ b/Sources/Leaf/Node+Rendered.swift @@ -4,14 +4,16 @@ extension Node { case .array(_), .object(_), .null: return [] case let .bool(bool): - return bool.description.bytes + return bool.description.makeBytes() case let .number(number): - return number.description.bytes + return number.description.makeBytes() case let .string(str): // defaults to escaping, use #raw(var) to unescape. - return str.htmlEscaped().bytes + return str.htmlEscaped().makeBytes() case let .bytes(bytes): return bytes + case let .date(date): + return Date.outgoingDateFormatter.string(from: date).makeBytes() } } } diff --git a/Sources/Leaf/Parameter.swift b/Sources/Leaf/Parameter.swift index f1c75ef..a60ac86 100644 --- a/Sources/Leaf/Parameter.swift +++ b/Sources/Leaf/Parameter.swift @@ -47,7 +47,7 @@ extension Parameter { internal init(_ bytes: S) throws where S.Iterator.Element == Byte { let bytes = bytes.array.trimmed(.whitespace) guard !bytes.isEmpty else { throw Error.nonEmptyArgumentRequired } - if bytes.count > 1, bytes.first == .quotationMark, bytes.last == .quotationMark { + if bytes.count > 1, bytes.first == .quote, bytes.last == .quote { self = .constant(value: bytes.dropFirst().dropLast().string) } else { let path = bytes.split( diff --git a/Sources/Leaf/Stem+Render.swift b/Sources/Leaf/Stem+Render.swift index 5c891c2..47b0d7a 100644 --- a/Sources/Leaf/Stem+Render.swift +++ b/Sources/Leaf/Stem+Render.swift @@ -70,7 +70,7 @@ extension Stem { context: Context, value: Node?, tagTemplate: TagTemplate) throws -> Bytes { - // return "World".bytes + // return "World".makeBytes() if let subLeaf = tagTemplate.body { if let val = value { context.push(["self": val]) } return try tag.render(stem: self, context: context, value: value, leaf: subLeaf) diff --git a/Sources/Leaf/Stem+Spawn.swift b/Sources/Leaf/Stem+Spawn.swift index ec56ac5..fd65d61 100644 --- a/Sources/Leaf/Stem+Spawn.swift +++ b/Sources/Leaf/Stem+Spawn.swift @@ -1,6 +1,6 @@ extension Stem { public func spawnLeaf(raw: String) throws -> Leaf { - return try spawnLeaf(raw: raw.bytes) + return try spawnLeaf(raw: raw.makeBytes()) } public func spawnLeaf(raw: Bytes) throws -> Leaf { diff --git a/Sources/Leaf/Tag/Models/Equal.swift b/Sources/Leaf/Tag/Models/Equal.swift index d9a3a15..28266e5 100644 --- a/Sources/Leaf/Tag/Models/Equal.swift +++ b/Sources/Leaf/Tag/Models/Equal.swift @@ -54,5 +54,9 @@ fileprivate func fuzzyEquals(_ lhs: Node?, _ rhs: Node?) -> Bool { return true case let .string(string): return string == rhs.string + case let .date(date): + // FIXME: Add fuzzy date access and equality? + guard case let .date(right) = rhs else { return false } + return date == right } } diff --git a/Sources/Leaf/Tag/Models/Raw.swift b/Sources/Leaf/Tag/Models/Raw.swift index 9bf0caf..165bae6 100644 --- a/Sources/Leaf/Tag/Models/Raw.swift +++ b/Sources/Leaf/Tag/Models/Raw.swift @@ -2,13 +2,13 @@ final class Raw: Tag { let name = "raw" func compileBody(stem: Stem, raw: String) throws -> Leaf { - let component = Leaf.Component.raw(raw.bytes) + let component = Leaf.Component.raw(raw.makeBytes()) return Leaf(raw: raw, components: [component]) } func run(stem: Stem, context: Context, tagTemplate: TagTemplate, arguments: [Argument]) throws -> Node? { guard let string = arguments.first?.value?.string else { return nil } - let unescaped = string.bytes + let unescaped = string.makeBytes() return .bytes(unescaped) } diff --git a/Sources/Leaf/Tag/TagTemplate.swift b/Sources/Leaf/Tag/TagTemplate.swift index 1d9ee66..3d6fb08 100644 --- a/Sources/Leaf/Tag/TagTemplate.swift +++ b/Sources/Leaf/Tag/TagTemplate.swift @@ -9,9 +9,9 @@ public final class TagTemplate { internal init(name: String, parameters: [Parameter], body: Leaf?) { // we strip leading token, if another one is there, // that means we've found a chain element, ie: @@else { - if name.bytes.first == TOKEN { + if name.makeBytes().first == TOKEN { self.isChain = true - self.name = name.bytes.dropFirst().string + self.name = name.makeBytes().dropFirst().string } else { self.isChain = false self.name = name diff --git a/Tests/LeafTests/BufferTests.swift b/Tests/LeafTests/BufferTests.swift index a2b5913..88a1ab2 100644 --- a/Tests/LeafTests/BufferTests.swift +++ b/Tests/LeafTests/BufferTests.swift @@ -9,7 +9,7 @@ class BufferTests: XCTestCase { ] func testSectionOpenerThrow() throws { - var buffer = Buffer("No opener".bytes) + var buffer = Buffer("No opener".makeBytes()) do { _ = try buffer.extractSection(opensWith: .period, closesWith: .period) XCTFail() @@ -17,7 +17,7 @@ class BufferTests: XCTestCase { } func testSectionCloserThrow() throws { - var buffer = Buffer(". No closer".bytes) + var buffer = Buffer(". No closer".makeBytes()) do { _ = try buffer.extractSection(opensWith: .period, closesWith: .period) XCTFail() diff --git a/Tests/LeafTests/NodeRenderTests.swift b/Tests/LeafTests/NodeRenderTests.swift index c68487c..87d4009 100644 --- a/Tests/LeafTests/NodeRenderTests.swift +++ b/Tests/LeafTests/NodeRenderTests.swift @@ -9,17 +9,17 @@ class NodeRenderTests: XCTestCase { func testRender() throws { var node = Node("Hello") - XCTAssert(try node.rendered() == "Hello".bytes) + XCTAssert(try node.rendered() == "Hello".makeBytes()) - node = .bytes("SomeBytes".bytes) - XCTAssert(try node.rendered() == "SomeBytes".bytes) + node = .bytes("SomeBytes".makeBytes()) + XCTAssert(try node.rendered() == "SomeBytes".makeBytes()) node = .number(19972) - XCTAssert(try node.rendered() == "19972".bytes) + XCTAssert(try node.rendered() == "19972".makeBytes()) node = .number(-98172) - XCTAssert(try node.rendered() == "-98172".bytes) + XCTAssert(try node.rendered() == "-98172".makeBytes()) node = .number(73.655) - XCTAssert(try node.rendered() == "73.655".bytes) + XCTAssert(try node.rendered() == "73.655".makeBytes()) node = .object([:]) XCTAssert(try node.rendered() == []) @@ -29,8 +29,8 @@ class NodeRenderTests: XCTestCase { XCTAssert(try node.rendered() == []) node = .bool(true) - XCTAssert(try node.rendered() == "true".bytes) + XCTAssert(try node.rendered() == "true".makeBytes()) node = .bool(false) - XCTAssert(try node.rendered() == "false".bytes) + XCTAssert(try node.rendered() == "false".makeBytes()) } } diff --git a/Tests/LeafTests/PerformanceTests.swift b/Tests/LeafTests/PerformanceTests.swift index e35112b..739a90b 100644 --- a/Tests/LeafTests/PerformanceTests.swift +++ b/Tests/LeafTests/PerformanceTests.swift @@ -12,7 +12,7 @@ class PerformanceTests: XCTestCase { func testLeaf() throws { let raw = "Hello, #(name)!" - let expectation = "Hello, World!".bytes + let expectation = "Hello, World!".makeBytes() let template = try stem.spawnLeaf(raw: raw) let ctxt = Context(["name": "World"]) measure { @@ -44,7 +44,7 @@ class PerformanceTests: XCTestCase { func testLeafLong() throws { let raw = [String](repeating: "Hello, #(name)!", count: 1000).joined(separator: ", ") - let expectation = [String](repeating: "Hello, World!", count: 1000).joined(separator: ", ").bytes + let expectation = [String](repeating: "Hello, World!", count: 1000).joined(separator: ", ").makeBytes() let template = try stem.spawnLeaf(raw: raw) _ = template.description let ctxt = Context(["name": "World"]) diff --git a/Tests/LeafTests/TagTemplateTests.swift b/Tests/LeafTests/TagTemplateTests.swift index 638972b..8cc280b 100644 --- a/Tests/LeafTests/TagTemplateTests.swift +++ b/Tests/LeafTests/TagTemplateTests.swift @@ -51,7 +51,7 @@ class TagTemplateTests: XCTestCase { } func testEquatableComponents() throws { - let lhs = Leaf.Component.raw("raw".bytes) + let lhs = Leaf.Component.raw("raw".makeBytes()) let rhs = Leaf.Component.chain([]) XCTAssertNotEqual(lhs, rhs) }