 // MARK: - EntityEncoder
 
 struct EntityEncoder: EncoderType {
-    static func encode(_ value: Entity) -> Any {
+    static func encode(_ entity: Entity) -> Any {
         var dictionary = [String: Any]()
 
-        dictionary["html_tag"] = value.tag as AnyObject?
-        dictionary["position"] = [value.range.lowerBound, value.range.upperBound]
-        dictionary["attributes"] = value.attributes as AnyObject?
+        dictionary["html_tag"] = entity.tag
+        dictionary["position"] = [entity.range.lowerBound, entity.range.upperBound]
+        dictionary["attributes"] = entity.attributes
 
-        return dictionary as AnyObject
+        return dictionary
     }
 }

// 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 {
            return nil
        }
        return array[0]..<array[1]
    }
}

    }
}

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)
    }
}

