Use enum instead of pair as result type for Template
This commit is contained in:
@@ -8,10 +8,6 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
public protocol Error : Printable {
|
||||
|
||||
}
|
||||
|
||||
struct NodeError : Error {
|
||||
let token:Token
|
||||
let message:String
|
||||
|
||||
33
Stencil/Result.swift
Normal file
33
Stencil/Result.swift
Normal file
@@ -0,0 +1,33 @@
|
||||
//
|
||||
// Result.swift
|
||||
// Stencil
|
||||
//
|
||||
// Created by Marius Rackwitz on 26/10/14.
|
||||
// Copyright (c) 2014 Cocode. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
public protocol Error : Printable {
|
||||
|
||||
}
|
||||
|
||||
public func ==(lhs:Error, rhs:Error) -> Bool {
|
||||
return lhs.description == rhs.description
|
||||
}
|
||||
|
||||
public enum Result : Equatable {
|
||||
case Success(string: String)
|
||||
case Error(error: Stencil.Error)
|
||||
}
|
||||
|
||||
public func ==(lhs:Result, rhs:Result) -> Bool {
|
||||
switch (lhs, rhs) {
|
||||
case (.Success(let lhsValue), .Success(let rhsValue)):
|
||||
return lhsValue == rhsValue
|
||||
case (.Error(let lhsValue), .Error(let rhsValue)):
|
||||
return lhsValue == rhsValue
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
@@ -44,15 +44,19 @@ public class Template {
|
||||
parser = TokenParser(tokens: tokens)
|
||||
}
|
||||
|
||||
public func render(context:Context) -> (string:String?, error:Error?) {
|
||||
public func render(context:Context) -> Result {
|
||||
let (nodes, error) = parser.parse()
|
||||
|
||||
if let error = error {
|
||||
return (nil, error)
|
||||
return .Error(error: error)
|
||||
} else if let nodes = nodes {
|
||||
return renderNodes(nodes, context)
|
||||
let result = renderNodes(nodes, context)
|
||||
if let string = result.0 {
|
||||
return .Success(string: string)
|
||||
} else {
|
||||
return .Error(error: result.1!)
|
||||
}
|
||||
}
|
||||
|
||||
return (nil, nil)
|
||||
return .Success(string: "")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user