diff --git a/Source/SWXMLHash+TypeConversion.swift b/Source/SWXMLHash+TypeConversion.swift index b16ff587..99c7dca7 100644 --- a/Source/SWXMLHash+TypeConversion.swift +++ b/Source/SWXMLHash+TypeConversion.swift @@ -98,9 +98,9 @@ public extension XMLIndexer { */ func value(ofAttribute attr: String) throws -> T { switch self { - case .Element(let element): + case .element(let element): return try element.value(ofAttribute: attr) - case .Stream(let opStream): + case .stream(let opStream): return try opStream.findElements().value(ofAttribute: attr) default: throw XMLDeserializationError.NodeIsInvalid(node: self) @@ -116,9 +116,9 @@ public extension XMLIndexer { */ func value(ofAttribute attr: String) -> T? { switch self { - case .Element(let element): + case .element(let element): return element.value(ofAttribute: attr) - case .Stream(let opStream): + case .stream(let opStream): return opStream.findElements().value(ofAttribute: attr) default: return nil @@ -135,11 +135,11 @@ public extension XMLIndexer { */ func value(ofAttribute attr: String) throws -> [T] { switch self { - case .List(let elements): + case .list(let elements): return try elements.map { try $0.value(ofAttribute: attr) } - case .Element(let element): + case .element(let element): return try [element].map { try $0.value(ofAttribute: attr) } - case .Stream(let opStream): + case .stream(let opStream): return try opStream.findElements().value(ofAttribute: attr) default: throw XMLDeserializationError.NodeIsInvalid(node: self) @@ -156,11 +156,11 @@ public extension XMLIndexer { */ func value(ofAttribute attr: String) throws -> [T]? { switch self { - case .List(let elements): + case .list(let elements): return try elements.map { try $0.value(ofAttribute: attr) } - case .Element(let element): + case .element(let element): return try [element].map { try $0.value(ofAttribute: attr) } - case .Stream(let opStream): + case .stream(let opStream): return try opStream.findElements().value(ofAttribute: attr) default: return nil @@ -177,11 +177,11 @@ public extension XMLIndexer { */ func value(ofAttribute attr: String) throws -> [T?] { switch self { - case .List(let elements): + case .list(let elements): return elements.map { $0.value(ofAttribute: attr) } - case .Element(let element): + case .element(let element): return [element].map { $0.value(ofAttribute: attr) } - case .Stream(let opStream): + case .stream(let opStream): return try opStream.findElements().value(ofAttribute: attr) default: throw XMLDeserializationError.NodeIsInvalid(node: self) @@ -198,9 +198,9 @@ public extension XMLIndexer { */ func value() throws -> T { switch self { - case .Element(let element): + case .element(let element): return try T.deserialize(element) - case .Stream(let opStream): + case .stream(let opStream): return try opStream.findElements().value() default: throw XMLDeserializationError.NodeIsInvalid(node: self) @@ -215,9 +215,9 @@ public extension XMLIndexer { */ func value() throws -> T? { switch self { - case .Element(let element): + case .element(let element): return try T.deserialize(element) - case .Stream(let opStream): + case .stream(let opStream): return try opStream.findElements().value() default: return nil @@ -232,11 +232,11 @@ public extension XMLIndexer { */ func value() throws -> [T] { switch self { - case .List(let elements): + case .list(let elements): return try elements.map { try T.deserialize($0) } - case .Element(let element): + case .element(let element): return try [element].map { try T.deserialize($0) } - case .Stream(let opStream): + case .stream(let opStream): return try opStream.findElements().value() default: return [] @@ -251,11 +251,11 @@ public extension XMLIndexer { */ func value() throws -> [T]? { switch self { - case .List(let elements): + case .list(let elements): return try elements.map { try T.deserialize($0) } - case .Element(let element): + case .element(let element): return try [element].map { try T.deserialize($0) } - case .Stream(let opStream): + case .stream(let opStream): return try opStream.findElements().value() default: return nil @@ -270,11 +270,11 @@ public extension XMLIndexer { */ func value() throws -> [T?] { switch self { - case .List(let elements): + case .list(let elements): return try elements.map { try T.deserialize($0) } - case .Element(let element): + case .element(let element): return try [element].map { try T.deserialize($0) } - case .Stream(let opStream): + case .stream(let opStream): return try opStream.findElements().value() default: return [] @@ -291,9 +291,9 @@ public extension XMLIndexer { */ func value() throws -> T { switch self { - case .Element: + case .element: return try T.deserialize(self) - case .Stream(let opStream): + case .stream(let opStream): return try opStream.findElements().value() default: throw XMLDeserializationError.NodeIsInvalid(node: self) @@ -308,9 +308,9 @@ public extension XMLIndexer { */ func value() throws -> T? { switch self { - case .Element: + case .element: return try T.deserialize(self) - case .Stream(let opStream): + case .stream(let opStream): return try opStream.findElements().value() default: return nil @@ -325,11 +325,11 @@ public extension XMLIndexer { */ func value() throws -> [T] where T: XMLIndexerDeserializable { switch self { - case .List(let elements): + case .list(let elements): return try elements.map { try T.deserialize( XMLIndexer($0) ) } - case .Element(let element): + case .element(let element): return try [element].map { try T.deserialize( XMLIndexer($0) ) } - case .Stream(let opStream): + case .stream(let opStream): return try opStream.findElements().value() default: throw XMLDeserializationError.NodeIsInvalid(node: self) @@ -344,11 +344,11 @@ public extension XMLIndexer { */ func value() throws -> [T]? { switch self { - case .List(let elements): + case .list(let elements): return try elements.map { try T.deserialize( XMLIndexer($0) ) } - case .Element(let element): + case .element(let element): return try [element].map { try T.deserialize( XMLIndexer($0) ) } - case .Stream(let opStream): + case .stream(let opStream): return try opStream.findElements().value() default: return nil @@ -363,11 +363,11 @@ public extension XMLIndexer { */ func value() throws -> [T?] { switch self { - case .List(let elements): + case .list(let elements): return try elements.map { try T.deserialize( XMLIndexer($0) ) } - case .Element(let element): + case .element(let element): return try [element].map { try T.deserialize( XMLIndexer($0) ) } - case .Stream(let opStream): + case .stream(let opStream): return try opStream.findElements().value() default: throw XMLDeserializationError.NodeIsInvalid(node: self) diff --git a/Source/SWXMLHash.swift b/Source/SWXMLHash.swift index fece8ae7..533e1ed4 100644 --- a/Source/SWXMLHash.swift +++ b/Source/SWXMLHash.swift @@ -444,27 +444,73 @@ public class IndexOps { /// Error type that is thrown when an indexing or parsing operation fails. public enum IndexingError: Error { - case Attribute(attr: String) - case AttributeValue(attr: String, value: String) - case Key(key: String) - case Index(idx: Int) - case Init(instance: AnyObject) - case Error + case attribute(attr: String) + case attributeValue(attr: String, value: String) + case key(key: String) + case index(idx: Int) + case initialize(instance: AnyObject) + case error + + // unavailable + @available(*, unavailable, renamed: "attribute(attr:)") + public static func Attribute(attr: String) -> IndexingError { + fatalError("unavailable") + } + @available(*, unavailable, renamed: "attributeValue(attr:value:)") + public static func AttributeValue(attr: String, value: String) -> IndexingError { + fatalError("unavailable") + } + @available(*, unavailable, renamed: "key(key:)") + public static func Key(key: String) -> IndexingError { + fatalError("unavailable") + } + @available(*, unavailable, renamed: "index(idx:)") + public static func Index(idx: Int) -> IndexingError { + fatalError("unavailable") + } + @available(*, unavailable, renamed: "initialize(instance:)") + public static func Init(instance: AnyObject) -> IndexingError { + fatalError("unavailable") + } + @available(*, unavailable, renamed: "error") + public static var Error: IndexingError { + fatalError("unavailable") + } } /// Returned from SWXMLHash, allows easy element lookup into XML data. public enum XMLIndexer: Sequence { - case Element(XMLElement) - case List([XMLElement]) - case Stream(IndexOps) - case XMLError(IndexingError) + case element(XMLElement) + case list([XMLElement]) + case stream(IndexOps) + case xmlError(IndexingError) + + // unavailable +#if !swift(>=3.2) // `Sequence.Element` is added on Swift 4.0 including with `-swift-version 3` + @available(*, unavailable, renamed: "element(_:)") + public static func Element(_: XMLElement) -> XMLIndexer { + fatalError("unavailable") + } +#endif + @available(*, unavailable, renamed: "list(_:)") + public static func List(_: [XMLElement]) -> XMLIndexer { + fatalError("unavailable") + } + @available(*, unavailable, renamed: "stream(_:)") + public static func Stream(_: IndexOps) -> XMLIndexer { + fatalError("unavailable") + } + @available(*, unavailable, renamed: "xmlError(_:)") + public static func XMLError(_: IndexingError) -> XMLIndexer { + fatalError("unavailable") + } /// The underlying XMLElement at the currently indexed level of XML. public var element: XMLElement? { switch self { - case .Element(let elem): + case .element(let elem): return elem - case .Stream(let ops): + case .stream(let ops): let list = ops.findElements() return list.element default: @@ -475,15 +521,15 @@ public enum XMLIndexer: Sequence { /// All elements at the currently indexed level public var all: [XMLIndexer] { switch self { - case .List(let list): + case .list(let list): var xmlList = [XMLIndexer]() for elem in list { xmlList.append(XMLIndexer(elem)) } return xmlList - case .Element(let elem): + case .element(let elem): return [XMLIndexer(elem)] - case .Stream(let ops): + case .stream(let ops): let list = ops.findElements() return list.all default: @@ -513,21 +559,21 @@ public enum XMLIndexer: Sequence { */ public func withAttr(_ attr: String, _ value: String) throws -> XMLIndexer { switch self { - case .Stream(let opStream): + case .stream(let opStream): let match = opStream.findElements() return try match.withAttr(attr, value) - case .List(let list): + case .list(let list): if let elem = list.filter({$0.attribute(by: attr)?.text == value}).first { - return .Element(elem) + return .element(elem) } - throw IndexingError.AttributeValue(attr: attr, value: value) - case .Element(let elem): + throw IndexingError.attributeValue(attr: attr, value: value) + case .element(let elem): if elem.attribute(by: attr)?.text == value { - return .Element(elem) + return .element(elem) } - throw IndexingError.AttributeValue(attr: attr, value: value) + throw IndexingError.attributeValue(attr: attr, value: value) default: - throw IndexingError.Attribute(attr: attr) + throw IndexingError.attribute(attr: attr) } } @@ -540,11 +586,11 @@ public enum XMLIndexer: Sequence { public init(_ rawObject: AnyObject) throws { switch rawObject { case let value as XMLElement: - self = .Element(value) + self = .element(value) case let value as LazyXMLParser: - self = .Stream(IndexOps(parser: value)) + self = .stream(IndexOps(parser: value)) default: - throw IndexingError.Init(instance: rawObject) + throw IndexingError.initialize(instance: rawObject) } } @@ -554,11 +600,11 @@ public enum XMLIndexer: Sequence { - parameter _: an instance of XMLElement */ public init(_ elem: XMLElement) { - self = .Element(elem) + self = .element(elem) } init(_ stream: LazyXMLParser) { - self = .Stream(IndexOps(parser: stream)) + self = .stream(IndexOps(parser: stream)) } /** @@ -570,22 +616,22 @@ public enum XMLIndexer: Sequence { */ public func byKey(_ key: String) throws -> XMLIndexer { switch self { - case .Stream(let opStream): + case .stream(let opStream): let op = IndexOp(key) opStream.ops.append(op) - return .Stream(opStream) - case .Element(let elem): + return .stream(opStream) + case .element(let elem): let match = elem.xmlChildren.filter({ $0.name == key }) if !match.isEmpty { if match.count == 1 { - return .Element(match[0]) + return .element(match[0]) } else { - return .List(match) + return .list(match) } } fallthrough default: - throw IndexingError.Key(key: key) + throw IndexingError.key(key: key) } } @@ -599,9 +645,9 @@ public enum XMLIndexer: Sequence { do { return try self.byKey(key) } catch let error as IndexingError { - return .XMLError(error) + return .xmlError(error) } catch { - return .XMLError(IndexingError.Key(key: key)) + return .xmlError(IndexingError.key(key: key)) } } @@ -614,21 +660,21 @@ public enum XMLIndexer: Sequence { */ public func byIndex(_ index: Int) throws -> XMLIndexer { switch self { - case .Stream(let opStream): + case .stream(let opStream): opStream.ops[opStream.ops.count - 1].index = index - return .Stream(opStream) - case .List(let list): + return .stream(opStream) + case .list(let list): if index <= list.count { - return .Element(list[index]) + return .element(list[index]) } - return .XMLError(IndexingError.Index(idx: index)) - case .Element(let elem): + return .xmlError(IndexingError.index(idx: index)) + case .element(let elem): if index == 0 { - return .Element(elem) + return .element(elem) } fallthrough default: - return .XMLError(IndexingError.Index(idx: index)) + return .xmlError(IndexingError.index(idx: index)) } } @@ -642,9 +688,9 @@ public enum XMLIndexer: Sequence { do { return try byIndex(index) } catch let error as IndexingError { - return .XMLError(error) + return .xmlError(error) } catch { - return .XMLError(IndexingError.Index(idx: index)) + return .xmlError(IndexingError.index(idx: index)) } } @@ -679,9 +725,9 @@ extension XMLIndexer: CustomStringConvertible { /// The XML representation of the XMLIndexer at the current level public var description: String { switch self { - case .List(let list): + case .list(let list): return list.map { $0.description }.joined(separator: "") - case .Element(let elem): + case .element(let elem): if elem.name == rootElementName { return elem.children.map { $0.description }.joined(separator: "") } @@ -697,17 +743,17 @@ extension IndexingError: CustomStringConvertible { /// The description for the `IndexingError`. public var description: String { switch self { - case .Attribute(let attr): + case .attribute(let attr): return "XML Attribute Error: Missing attribute [\"\(attr)\"]" - case .AttributeValue(let attr, let value): + case .attributeValue(let attr, let value): return "XML Attribute Error: Missing attribute [\"\(attr)\"] with value [\"\(value)\"]" - case .Key(let key): + case .key(let key): return "XML Element Error: Incorrect key [\"\(key)\"]" - case .Index(let index): + case .index(let index): return "XML Element Error: Incorrect index [\"\(index)\"]" - case .Init(let instance): + case .initialize(let instance): return "XML Indexer Error: initialization with Object [\"\(instance)\"]" - case .Error: + case .error: return "Unknown Error" } } diff --git a/Tests/SWXMLHashTests/XMLParsingTests.swift b/Tests/SWXMLHashTests/XMLParsingTests.swift index f50bd103..2975d534 100644 --- a/Tests/SWXMLHashTests/XMLParsingTests.swift +++ b/Tests/SWXMLHashTests/XMLParsingTests.swift @@ -188,7 +188,7 @@ class XMLParsingTests: XCTestCase { func testShouldStillReturnErrorsWhenAccessingViaSubscripting() { var err: IndexingError? = nil switch xml!["what"]["subelement"][5]["nomatch"] { - case .XMLError(let error): + case .xmlError(let error): err = error default: err = nil