Skip to content

Commit

Permalink
refactor: break out IndexingError to own file
Browse files Browse the repository at this point in the history
  • Loading branch information
drmohundro committed May 3, 2022
1 parent d7aba5a commit c2fae03
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 67 deletions.
12 changes: 11 additions & 1 deletion SWXMLHash.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@
63ED3C2A2821AF4D006A8A08 /* XMLHashOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63ED3C282821AF4D006A8A08 /* XMLHashOptions.swift */; };
63ED3C2B2821AF4D006A8A08 /* XMLHashOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63ED3C282821AF4D006A8A08 /* XMLHashOptions.swift */; };
63ED3C2C2821AF4D006A8A08 /* XMLHashOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63ED3C282821AF4D006A8A08 /* XMLHashOptions.swift */; };
63ED3C2E2821B100006A8A08 /* IndexingError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63ED3C2D2821B100006A8A08 /* IndexingError.swift */; };
63ED3C2F2821B100006A8A08 /* IndexingError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63ED3C2D2821B100006A8A08 /* IndexingError.swift */; };
63ED3C302821B100006A8A08 /* IndexingError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63ED3C2D2821B100006A8A08 /* IndexingError.swift */; };
63ED3C312821B100006A8A08 /* IndexingError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63ED3C2D2821B100006A8A08 /* IndexingError.swift */; };
6C42BED1205183A100137D31 /* shim.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C42BECC2051834B00137D31 /* shim.swift */; };
6C42BED2205183A100137D31 /* shim.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C42BECC2051834B00137D31 /* shim.swift */; };
6C42BED3205183A100137D31 /* shim.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C42BECC2051834B00137D31 /* shim.swift */; };
Expand Down Expand Up @@ -181,6 +185,7 @@
63ED3C062821A723006A8A08 /* Bool+XMLDeserialization.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Bool+XMLDeserialization.swift"; sourceTree = "<group>"; };
63ED3C072821A723006A8A08 /* Int+XMLDeserialization.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Int+XMLDeserialization.swift"; sourceTree = "<group>"; };
63ED3C282821AF4D006A8A08 /* XMLHashOptions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = XMLHashOptions.swift; sourceTree = "<group>"; };
63ED3C2D2821B100006A8A08 /* IndexingError.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = IndexingError.swift; sourceTree = "<group>"; };
6C0CE0F01D7440F8005F1248 /* LinuxShims.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LinuxShims.swift; sourceTree = "<group>"; };
6C42BECC2051834B00137D31 /* shim.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = shim.swift; sourceTree = "<group>"; };
6C42BED4205183AF00137D31 /* shim.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = shim.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -300,10 +305,10 @@
CD6083F1196CA106000B4F8D /* Source */ = {
isa = PBXGroup;
children = (
63ED3C282821AF4D006A8A08 /* XMLHashOptions.swift */,
63ED3C062821A723006A8A08 /* Bool+XMLDeserialization.swift */,
63ED3C032821A723006A8A08 /* Double+XMLDeserialization.swift */,
6317F1A5282179E200F6C364 /* Float+XMLDeserialization.swift */,
63ED3C2D2821B100006A8A08 /* IndexingError.swift */,
63ED3C072821A723006A8A08 /* Int+XMLDeserialization.swift */,
6C42BED4205183AF00137D31 /* shim.swift */,
63ED3C002821A723006A8A08 /* String+XMLDeserialization.swift */,
Expand All @@ -314,6 +319,7 @@
6317F1A02821756400F6C364 /* XMLDeserializationError.swift */,
63ED3C042821A723006A8A08 /* XMLElementDeserializable.swift */,
CD60840B196CA11D000B4F8D /* XMLHash.swift */,
63ED3C282821AF4D006A8A08 /* XMLHashOptions.swift */,
54B83CC41C849D9B00D588B5 /* XMLIndexer+XMLIndexerDeserializable.swift */,
63ED3C052821A723006A8A08 /* XMLIndexerDeserializable.swift */,
);
Expand Down Expand Up @@ -670,6 +676,7 @@
buildActionMask = 2147483647;
files = (
63ED3C1C2821A723006A8A08 /* XMLIndexerDeserializable.swift in Sources */,
63ED3C2E2821B100006A8A08 /* IndexingError.swift in Sources */,
63ED3C102821A723006A8A08 /* XMLAttributeDeserializable.swift in Sources */,
63ED3C142821A723006A8A08 /* Double+XMLDeserialization.swift in Sources */,
63ED3C202821A723006A8A08 /* Bool+XMLDeserialization.swift in Sources */,
Expand Down Expand Up @@ -711,6 +718,7 @@
buildActionMask = 2147483647;
files = (
63ED3C1D2821A723006A8A08 /* XMLIndexerDeserializable.swift in Sources */,
63ED3C2F2821B100006A8A08 /* IndexingError.swift in Sources */,
63ED3C112821A723006A8A08 /* XMLAttributeDeserializable.swift in Sources */,
63ED3C152821A723006A8A08 /* Double+XMLDeserialization.swift in Sources */,
63ED3C212821A723006A8A08 /* Bool+XMLDeserialization.swift in Sources */,
Expand Down Expand Up @@ -752,6 +760,7 @@
buildActionMask = 2147483647;
files = (
63ED3C1E2821A723006A8A08 /* XMLIndexerDeserializable.swift in Sources */,
63ED3C302821B100006A8A08 /* IndexingError.swift in Sources */,
63ED3C122821A723006A8A08 /* XMLAttributeDeserializable.swift in Sources */,
63ED3C162821A723006A8A08 /* Double+XMLDeserialization.swift in Sources */,
63ED3C222821A723006A8A08 /* Bool+XMLDeserialization.swift in Sources */,
Expand Down Expand Up @@ -793,6 +802,7 @@
buildActionMask = 2147483647;
files = (
63ED3C1F2821A723006A8A08 /* XMLIndexerDeserializable.swift in Sources */,
63ED3C312821B100006A8A08 /* IndexingError.swift in Sources */,
63ED3C132821A723006A8A08 /* XMLAttributeDeserializable.swift in Sources */,
63ED3C172821A723006A8A08 /* Double+XMLDeserialization.swift in Sources */,
63ED3C232821A723006A8A08 /* Bool+XMLDeserialization.swift in Sources */,
Expand Down
115 changes: 115 additions & 0 deletions Source/IndexingError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
//
// IndexingError.swift
// SWXMLHash
//
// Copyright (c) 2022 David Mohundro
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

import Foundation

/// 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 initialize(instance: AnyObject)
case encoding
case error

// swiftlint:disable identifier_name
// 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")
}
// swiftlint:enable identifier_name
}

extension IndexingError: CustomStringConvertible {
/// The description for the `IndexingError`.
public var description: String {
switch self {
case .attribute(let attr):
return "XML Attribute Error: Missing attribute [\"\(attr)\"]"
case let .attributeValue(attr, value):
return "XML Attribute Error: Missing attribute [\"\(attr)\"] with value [\"\(value)\"]"
case .key(let key):
return "XML Element Error: Incorrect key [\"\(key)\"]"
case .index(let index):
return "XML Element Error: Incorrect index [\"\(index)\"]"
case .initialize(let instance):
return "XML Indexer Error: initialization with Object [\"\(instance)\"]"
case .encoding:
return "String Encoding Error"
case .error:
return "Unknown Error"
}
}
}

extension IndexingError: LocalizedError {
/// The description for the `IndexingError`.
public var errorDescription: String? {
switch self {
case .attribute(let attr):
return "XML Attribute Error: Missing attribute [\"\(attr)\"]"
case let .attributeValue(attr, value):
return "XML Attribute Error: Missing attribute [\"\(attr)\"] with value [\"\(value)\"]"
case .key(let key):
return "XML Element Error: Incorrect key [\"\(key)\"]"
case .index(let index):
return "XML Element Error: Incorrect index [\"\(index)\"]"
case .initialize(let instance):
return "XML Indexer Error: initialization with Object [\"\(instance)\"]"
case .encoding:
return "String Encoding Error"
case .error:
return "Unknown Error"
}
}
}

66 changes: 0 additions & 66 deletions Source/XMLHash.swift
Original file line number Diff line number Diff line change
Expand Up @@ -474,50 +474,6 @@ public struct ParsingError: Error {
public let column: Int
}

/// 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 initialize(instance: AnyObject)
case encoding
case error

// swiftlint:disable identifier_name
// 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")
}
// swiftlint:enable identifier_name
}

/// Returned from XMLHash, allows easy element lookup into XML data.
public enum XMLIndexer {
case element(XMLElement)
Expand Down Expand Up @@ -805,28 +761,6 @@ extension XMLIndexer: CustomStringConvertible {
}
}

extension IndexingError: CustomStringConvertible {
/// The description for the `IndexingError`.
public var description: String {
switch self {
case .attribute(let attr):
return "XML Attribute Error: Missing attribute [\"\(attr)\"]"
case let .attributeValue(attr, value):
return "XML Attribute Error: Missing attribute [\"\(attr)\"] with value [\"\(value)\"]"
case .key(let key):
return "XML Element Error: Incorrect key [\"\(key)\"]"
case .index(let index):
return "XML Element Error: Incorrect index [\"\(index)\"]"
case .initialize(let instance):
return "XML Indexer Error: initialization with Object [\"\(instance)\"]"
case .encoding:
return "String Encoding Error"
case .error:
return "Unknown Error"
}
}
}

/// Models content for an XML doc, whether it is text or XML
public protocol XMLContent: CustomStringConvertible { }

Expand Down

0 comments on commit c2fae03

Please sign in to comment.