Remove custom Result type and throw errors
This commit is contained in:
@@ -1,44 +1,37 @@
|
||||
import Foundation
|
||||
import PathKit
|
||||
|
||||
extension String : Error {
|
||||
public var description:String {
|
||||
return self
|
||||
}
|
||||
}
|
||||
|
||||
public class IncludeNode : Node {
|
||||
public class IncludeNode : NodeType {
|
||||
public let templateName:String
|
||||
|
||||
public class func parse(parser:TokenParser, token:Token) -> TokenParser.Result {
|
||||
public class func parse(parser:TokenParser, token:Token) throws -> NodeType {
|
||||
let bits = token.contents.componentsSeparatedByString("\"")
|
||||
|
||||
if bits.count != 3 {
|
||||
return .Error(error:NodeError(token: token, message: "Tag takes one argument, the template file to be included"))
|
||||
throw TemplateSyntaxError("'include' tag takes one argument, the template file to be included")
|
||||
}
|
||||
|
||||
return .Success(node:IncludeNode(templateName: bits[1]))
|
||||
return IncludeNode(templateName: bits[1])
|
||||
}
|
||||
|
||||
public init(templateName:String) {
|
||||
self.templateName = templateName
|
||||
}
|
||||
|
||||
public func render(context: Context) -> Result {
|
||||
public func render(context: Context) throws -> String {
|
||||
if let loader = context["loader"] as? TemplateLoader {
|
||||
if let template = loader.loadTemplate(templateName) {
|
||||
return template.render(context)
|
||||
return try template.render(context)
|
||||
}
|
||||
|
||||
let paths:String = loader.paths.map { path in
|
||||
return path.description
|
||||
}.joinWithSeparator(", ")
|
||||
let error = "Template '\(templateName)' not found in \(paths)"
|
||||
return .Error(error)
|
||||
}.joinWithSeparator(", ")
|
||||
throw TemplateSyntaxError("'\(templateName)' template not found in \(paths)")
|
||||
}
|
||||
|
||||
let error = "Template loader not in context"
|
||||
return .Error(error)
|
||||
throw TemplateSyntaxError("Template loader not in context")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -24,36 +24,32 @@ func any<Element>(elements:[Element], closure:(Element -> Bool)) -> Element? {
|
||||
return nil
|
||||
}
|
||||
|
||||
class ExtendsNode : Node {
|
||||
class ExtendsNode : NodeType {
|
||||
let templateName:String
|
||||
let blocks:[String:BlockNode]
|
||||
|
||||
class func parse(parser:TokenParser, token:Token) -> TokenParser.Result {
|
||||
class func parse(parser:TokenParser, token:Token) throws -> NodeType {
|
||||
let bits = token.contents.componentsSeparatedByString("\"")
|
||||
|
||||
if bits.count != 3 {
|
||||
return .Error(error:NodeError(token: token, message: "Tag takes one argument, the template file to be extended"))
|
||||
throw TemplateSyntaxError("'extends' takes one argument, the template file to be extended")
|
||||
}
|
||||
|
||||
switch parser.parse() {
|
||||
case .Success(let nodes):
|
||||
if (any(nodes) { ($0 as? ExtendsNode) != nil }) != nil {
|
||||
return .Error(error:"'extends' cannot appear more than once in the same template")
|
||||
}
|
||||
|
||||
let blockNodes = nodes.filter { node in node is BlockNode }
|
||||
|
||||
let nodes = blockNodes.reduce([String:BlockNode](), combine: { (accumulator, node:Node) -> [String:BlockNode] in
|
||||
let node = (node as! BlockNode)
|
||||
var dict = accumulator
|
||||
dict[node.name] = node
|
||||
return dict
|
||||
})
|
||||
|
||||
return .Success(node:ExtendsNode(templateName: bits[1], blocks: nodes))
|
||||
case .Error(let error):
|
||||
return .Error(error:error)
|
||||
let parsedNodes = try parser.parse()
|
||||
if (any(parsedNodes) { ($0 as? ExtendsNode) != nil }) != nil {
|
||||
throw TemplateSyntaxError("'extends' cannot appear more than once in the same template")
|
||||
}
|
||||
|
||||
let blockNodes = parsedNodes.filter { node in node is BlockNode }
|
||||
|
||||
let nodes = blockNodes.reduce([String:BlockNode](), combine: { (accumulator, node:NodeType) -> [String:BlockNode] in
|
||||
let node = (node as! BlockNode)
|
||||
var dict = accumulator
|
||||
dict[node.name] = node
|
||||
return dict
|
||||
})
|
||||
|
||||
return ExtendsNode(templateName: bits[1], blocks: nodes)
|
||||
}
|
||||
|
||||
init(templateName:String, blocks:[String:BlockNode]) {
|
||||
@@ -61,12 +57,12 @@ class ExtendsNode : Node {
|
||||
self.blocks = blocks
|
||||
}
|
||||
|
||||
func render(context: Context) -> Result {
|
||||
func render(context: Context) throws -> String {
|
||||
if let loader = context["loader"] as? TemplateLoader {
|
||||
if let template = loader.loadTemplate(templateName) {
|
||||
let blockContext = BlockContext(blocks: blocks)
|
||||
context.push([BlockContext.contextKey: blockContext])
|
||||
let result = template.render(context)
|
||||
let result = try template.render(context)
|
||||
context.pop()
|
||||
return result
|
||||
}
|
||||
@@ -74,51 +70,39 @@ class ExtendsNode : Node {
|
||||
let paths:String = loader.paths.map { path in
|
||||
return path.description
|
||||
}.joinWithSeparator(", ")
|
||||
let error = "Template '\(templateName)' not found in \(paths)"
|
||||
return .Error(error)
|
||||
throw TemplateSyntaxError("'\(templateName)' template not found in \(paths)")
|
||||
}
|
||||
|
||||
let error = "Template loader not in context"
|
||||
return .Error(error)
|
||||
throw TemplateSyntaxError("Template loader not in context")
|
||||
}
|
||||
}
|
||||
|
||||
class BlockNode : Node {
|
||||
class BlockNode : NodeType {
|
||||
let name:String
|
||||
let nodes:[Node]
|
||||
let nodes:[NodeType]
|
||||
|
||||
class func parse(parser:TokenParser, token:Token) -> TokenParser.Result {
|
||||
class func parse(parser:TokenParser, token:Token) throws -> NodeType {
|
||||
let bits = token.components()
|
||||
|
||||
if bits.count != 2 {
|
||||
return .Error(error:NodeError(token: token, message: "Tag takes one argument, the template file to be included"))
|
||||
throw TemplateSyntaxError("'block' tag takes one argument, the template file to be included")
|
||||
}
|
||||
|
||||
let blockName = bits[1]
|
||||
var nodes = [Node]()
|
||||
|
||||
switch parser.parse(until(["endblock"])) {
|
||||
case .Success(let blockNodes):
|
||||
nodes = blockNodes
|
||||
case .Error(let error):
|
||||
return .Error(error: error)
|
||||
}
|
||||
|
||||
return .Success(node:BlockNode(name:blockName, nodes:nodes))
|
||||
let nodes = try parser.parse(until(["endblock"]))
|
||||
return BlockNode(name:blockName, nodes:nodes)
|
||||
}
|
||||
|
||||
init(name:String, nodes:[Node]) {
|
||||
init(name:String, nodes:[NodeType]) {
|
||||
self.name = name
|
||||
self.nodes = nodes
|
||||
}
|
||||
|
||||
func render(context: Context) -> Result {
|
||||
if let blockContext = context[BlockContext.contextKey] as? BlockContext {
|
||||
if let node = blockContext.pop(name) {
|
||||
return node.render(context)
|
||||
}
|
||||
func render(context: Context) throws -> String {
|
||||
if let blockContext = context[BlockContext.contextKey] as? BlockContext, node = blockContext.pop(name) {
|
||||
return try node.render(context)
|
||||
}
|
||||
|
||||
return renderNodes(nodes, context: context)
|
||||
return try renderNodes(nodes, context)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user