<commit_before>//
//  ResponseSerialization.swift
//  Flamingo
//
//  Created by <NAME> on 9/12/17.
//

import Foundation

public protocol ResponseSerialization {
    
    associatedtype Serialized
    
    func serialize(request: URLRequest?, response: HTTPURLResponse?, data: Data?, error: Swift.Error?) -> Result<Serialized>
    
}

public struct DataResponseSerializer: ResponseSerialization {
    
    public typealias Serialized = Data
    
    public init() { }
    
    public func serialize(request: URLRequest?, response: HTTPURLResponse?, data: Data?, error: Swift.Error?) -> Result<Data> {
        if let data  = data {
            return .success(data)
        } else if let error = error {
            return .error(error)
        } else {
            return .error(Error.unableToRetrieveDataAndError)
        }
    }
    
}

public struct StringResponseSerializer: ResponseSerialization {
    
    public typealias Serialized = String
    
    let encoding: String.Encoding
    
    public init(encoding: String.Encoding = .utf8) {
        self.encoding = encoding
    }
    
    public func serialize(request: URLRequest?, response: HTTPURLResponse?, data: Data?, error: Swift.Error?) -> Result<String> {
        if let data = data, let resultString = String(data: data, encoding: encoding) {
            return .success(resultString)
        } else if let error = error {
            return .error(error)
        } else {
            return .error(Error.unableToRetrieveDataAndError)
        }
    }
    
}

public struct CodableJSONSerializer<Serialized: Decodable>: ResponseSerialization {
    
    let serializer: JSONSerializer
    
    public init(serializer: JSONSerializer) {
        self.serializer = serializer
    }
    
    public init(dateDecodingStrategy: JSONDecoder.DateDecodingStrategy = .deferredToDate, dataDecodingStrategy: JSONDecoder.DataDecodingStrategy = .base64, nonConformingFloatDecodingStrategy: JSONDecoder.NonConformingFloatDecodingStrategy = .throw) {
        let serializer = JSONSerializer(dateDecodingStrategy: dateDecodingStrategy, dataDecodingStrategy: dataDecodingStrategy, nonConformingFloatDecodingStrategy: nonConformingFloatDecodingStrategy)

        self.init(serializer: serializer)
    }
    
    public func serialize(request: URLRequest?, response: HTTPURLResponse?, data: Data?, error: Swift.Error?) -> Result<Serialized> {
        guard let data = data else {
            return .error(error ?? Error.unableToRetrieveDataAndError)
        }
        
        return self.serializer.deserialize(data: data)
    }
    
}<commit_msg>Add init with coder<commit_after>//
//  ResponseSerialization.swift
//  Flamingo
//
//  Created by <NAME> on 9/12/17.
//

import Foundation

public protocol ResponseSerialization {
    
    associatedtype Serialized
    
    func serialize(request: URLRequest?, response: HTTPURLResponse?, data: Data?, error: Swift.Error?) -> Result<Serialized>
    
}

public struct DataResponseSerializer: ResponseSerialization {
    
    public typealias Serialized = Data
    
    public init() { }
    
    public func serialize(request: URLRequest?, response: HTTPURLResponse?, data: Data?, error: Swift.Error?) -> Result<Data> {
        if let data  = data {
            return .success(data)
        } else if let error = error {
            return .error(error)
        } else {
            return .error(Error.unableToRetrieveDataAndError)
        }
    }
    
}

public struct StringResponseSerializer: ResponseSerialization {
    
    public typealias Serialized = String
    
    let encoding: String.Encoding
    
    public init(encoding: String.Encoding = .utf8) {
        self.encoding = encoding
    }
    
    public func serialize(request: URLRequest?, response: HTTPURLResponse?, data: Data?, error: Swift.Error?) -> Result<String> {
        if let data = data, let resultString = String(data: data, encoding: encoding) {
            return .success(resultString)
        } else if let error = error {
            return .error(error)
        } else {
            return .error(Error.unableToRetrieveDataAndError)
        }
    }
    
}

public struct CodableJSONSerializer<Serialized: Decodable>: ResponseSerialization {
    
    let serializer: JSONSerializer
    
    public init(serializer: JSONSerializer) {
        self.serializer = serializer
    }

    public init(decoder: JSONDecoder) {
        self.serializer = JSONSerializer(decoder: decoder)
    }
    
    public init(dateDecodingStrategy: JSONDecoder.DateDecodingStrategy = .deferredToDate, dataDecodingStrategy: JSONDecoder.DataDecodingStrategy = .base64, nonConformingFloatDecodingStrategy: JSONDecoder.NonConformingFloatDecodingStrategy = .throw) {
        let serializer = JSONSerializer(dateDecodingStrategy: dateDecodingStrategy, dataDecodingStrategy: dataDecodingStrategy, nonConformingFloatDecodingStrategy: nonConformingFloatDecodingStrategy)

        self.init(serializer: serializer)
    }
    
    public func serialize(request: URLRequest?, response: HTTPURLResponse?, data: Data?, error: Swift.Error?) -> Result<Serialized> {
        guard let data = data else {
            return .error(error ?? Error.unableToRetrieveDataAndError)
        }
        
        return self.serializer.deserialize(data: data)
    }
    
}
{'fraction_non_alphanumeric': 0.08720271800679502, 'fraction_numerical': 0.003020007550018875, 'mean_word_length': 2.9280948851000743, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 23, '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': '7300259', 'n_tokens_mistral': 1425, 'n_tokens_neox': 1409, 'n_words': 343}