<commit_before>//
//  Response.swift
//  Endpoint
//
//  Created by <NAME> on 13/10/2016.
//  Copyright © 2016 Tailored Apps. All rights reserved.
//

import Foundation

public protocol ResponseParser {
    associatedtype OutputType = Self
    
    static func parse(responseData: Data?, encoding: String.Encoding) throws -> OutputType?
}

extension ResponseParser {
    public static func parseJSON(responseData: Data?) throws -> Any {
        return try JSONSerialization.jsonObject(with: responseData ?? Data(), options: .allowFragments)
    }
}

extension Data: ResponseParser {
    public static func parse(responseData: Data?, encoding: String.Encoding) throws -> Data? {
        return responseData
    }
}

extension String: ResponseParser {
    public static func parse(responseData: Data?, encoding: String.Encoding) throws -> String? {
        guard let data = responseData else {
            return nil
        }
        
        if let string = String(data: data, encoding: encoding) {
            return string
        } else {
            throw APIError.parsingError(description: "String could not be parsed with encoding: \(encoding)")
        }
    }
}

extension Dictionary: ResponseParser {
    public static func parse(responseData: Data?, encoding: String.Encoding) throws -> Dictionary? {
        guard let dict = try parseJSON(responseData: responseData) as? Dictionary else {
            throw APIError.parsingError(description: "JSON structure is not an Object")
        }
        
        return dict
    }
}

extension Array: ResponseParser {
    public static func parse(responseData: Data?, encoding: String.Encoding) throws -> Array? {
        guard let array = try parseJSON(responseData: responseData) as? Array else {
            throw APIError.parsingError(description: "JSON structure is not an Array")
        }
        
        return array
    }
}

public struct Result<Value: ResponseParser> {
    public internal(set) var value: Value?
    public internal(set) var error: Error?
    
    public internal(set) var response: HTTPURLResponse?
    
    public var isSuccess: Bool {
        return !isError
    }
    <commit_msg>fixed response value type<commit_after>//
//  Response.swift
//  Endpoint
//
//  Created by <NAME> on 13/10/2016.
//  Copyright © 2016 Tailored Apps. All rights reserved.
//

import Foundation

public protocol ResponseParser {
    associatedtype OutputType = Self
    
    static func parse(responseData: Data?, encoding: String.Encoding) throws -> OutputType?
}

extension ResponseParser {
    public static func parseJSON(responseData: Data?) throws -> Any {
        return try JSONSerialization.jsonObject(with: responseData ?? Data(), options: .allowFragments)
    }
}

extension Data: ResponseParser {
    public static func parse(responseData: Data?, encoding: String.Encoding) throws -> Data? {
        return responseData
    }
}

extension String: ResponseParser {
    public static func parse(responseData: Data?, encoding: String.Encoding) throws -> String? {
        guard let data = responseData else {
            return nil
        }
        
        if let string = String(data: data, encoding: encoding) {
            return string
        } else {
            throw APIError.parsingError(description: "String could not be parsed with encoding: \(encoding)")
        }
    }
}

extension Dictionary: ResponseParser {
    public static func parse(responseData: Data?, encoding: String.Encoding) throws -> Dictionary? {
        guard let dict = try parseJSON(responseData: responseData) as? Dictionary else {
            throw APIError.parsingError(description: "JSON structure is not an Object")
        }
        
        return dict
    }
}

extension Array: ResponseParser {
    public static func parse(responseData: Data?, encoding: String.Encoding) throws -> Array? {
        guard let array = try parseJSON(responseData: responseData) as? Array else {
            throw APIError.parsingError(description: "JSON structure is not an Array")
        }
        
        return array
    }
}

public struct Result<Value> {
    public internal(set) var value: Value?
    public internal(set) var error: Error?
    
    public internal(set) var response: HTTPURLResponse?
    
    public var isSuccess: Bool {
        return !isError
    }
    
{'fraction_non_alphanumeric': 0.08718778908418132, 'fraction_numerical': 0.005550416281221091, 'mean_word_length': 2.7445887445887447, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '627404', 'n_tokens_mistral': 1123, 'n_tokens_neox': 1096, 'n_words': 378}