<commit_before>import Foundation
import Argo
import Curry

/**
  Result type returned from parsing JSON
*/
public enum Result<T> {
    case Success(T)
    case Failure(String)
}


class JsonParseService {
    
    /**
      Parse the result from a fetch mails request
    */
    func parseMails(json: AnyObject?) -> Result<Mails> {
        if let json = json {
            let decocedResponse = Mails.decode(JSON.parse(json))
            switch decocedResponse {
            case let .Success(response):
                return Result.Success(response)
            case let .Failure(error):
                switch error {
                case let .MissingKey(key):
                    return Result.Failure("Error parsing JSON: Missing key " + key)
                case let .TypeMismatch(expected, actual):
                    return Result.Failure("Error parsing JSON: expected " + expected + ", found " + actual)
                }
            }
        }
        return Result.Failure("Missing input")
    }
    
}

// MARK: - MailsResponse
extension Mails: Decodable {
    static func decode(json: JSON) -> Decoded<Mails> {
        return curry(Mails.init)
            <^> json <|| "mails"
            <*> json <|  "stats"
    }
}

// MARK: - Mail
extension Mail: Decodable {
    static func decode(json: JSON) -> Decoded<Mail> {<commit_msg>added func parseMail<commit_after>import Foundation
import Argo
import Curry

/**
  Result type returned from parsing JSON
*/
public enum Result<T> {
    case Success(T)
    case Failure(String)
}


class JsonParseService {
    
    /**
      Parse the result from a fetch mails request
    */
    func parseMails(json: AnyObject?) -> Result<Mails> {
        if let json = json {
            let decocedResponse = Mails.decode(JSON.parse(json))
            switch decocedResponse {
            case let .Success(response):
                return Result.Success(response)
            case let .Failure(error):
                switch error {
                case let .MissingKey(key):
                    return Result.Failure("Error parsing JSON: Missing key " + key)
                case let .TypeMismatch(expected, actual):
                    return Result.Failure("Error parsing JSON: expected " + expected + ", found " + actual)
                }
            }
        }
        return Result.Failure("Missing input")
    }
    
    func parseMail(json: AnyObject?) -> Result<Mail> {
        if let json = json {
            let decodedResponse = Mail.decode(JSON.parse(json))
            switch decodedResponse {
            case let .Success(response):
                return Result.Success(response)
            case let .Failure(error):
                switch error {
                case let .MissingKey(key):
                    return Result.Failure("Error parsing JSON: Missing key " + key)
                case let .TypeMismatch(expected, actual):
                    return Result.Failure("Error parsing JSON: expected " + expected + ", found " + actual)
                }
            }
        }
        return Result.Failure("Missing input")
    }
    
}

// MARK: - MailsResponse
extension Mails: Decodable {
    static func decode(json: JSON) -> Decoded<Mails> {
        return curry(Mails.init)
            <^> json <|| "mails"
            <*> json <|  "stats"
    }
}

// MARK: - Mail
extension Mail: Decodable {
    static func decode(json: JSON) -> Decoded<Mail> {
{'fraction_non_alphanumeric': 0.10090405365995918, 'fraction_numerical': 0.0, 'mean_word_length': 1.793159609120521, 'pattern_counts': {'":': 0, '<': 20, '<?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': '789991', 'n_tokens_mistral': 938, 'n_tokens_neox': 889, 'n_words': 256}