 // MARK: - PositionDecoder
 
 private struct PositionDecoder: DecoderType {
-    static func decode(_ JSON: Alexander.JSON) -> Range<Int>? {
-        guard let array = JSON.object as? [Int], array.count == 2 else {
+    static func decode(_ json: JSON) -> Range<Int>? {
+        guard
+            let array = json.object as? [Int],
+            array.count == 2
+        else {
             return nil
         }
+
         return array[0]..<array[1]
     }
 }

// MARK: - Entity

struct Entity {
    var tag: String
    var range: Range<Int>
    fileprivate var attributes: [String: AnyObject]?
}

extension Entity {
    var href: URL? {
        if tag == "a" {
            return (attributes?["href"] as? String).flatMap({ URL(string: $0) })

    }
}

extension Entity: Equatable {
    static func == (lhs: Entity, rhs: Entity) -> Bool {
        return lhs.tag == rhs.tag
            && lhs.range == rhs.range
            && lhs.href == rhs.href
    }
}


// MARK: - EntityDecoder

struct EntityDecoder: DecoderType {
    static func decode(_ json: JSON) -> Entity? {
        guard
            let tag = json["html_tag"]?.stringValue,
            let range = json["position"]?.decode(PositionDecoder.self)
        else {
            return nil
        }

        let attributes = json["attributes"]?.dictionaryValue

        return Entity(tag: tag, range: range, attributes: attributes)
    }
}

