<commit_before>//
//  Storage.swift
//  Sweeft
//
//  Created by <NAME> on 10/1/17.
//

import Foundation

public enum Storage {
    case userDefaults
    case keychain
    
    var object: StorageItem {
        switch self {
        case .userDefaults:
            return UserDefaults.standard
        default:
            return Keychain()
        }
        
    }
    
}

protocol StorageItem {
    func object<C: Codable>(forKey defaultName: String) -> C?
    func set<C: Codable>(_ value: C?, forKey defaultName: String)
}

extension UserDefaults: StorageItem {
    
    func object<C>(forKey defaultName: String) -> C? where C: Codable {
        
        guard let data = data(forKey: defaultName) else { return nil }
        let decoder = PropertyListDecoder()
        return try? decoder.decode(C.self, from: data)
    }
    
    func set<C>(_ value: C?, forKey defaultName: String) where C : Decodable, C : Encodable {
        
        guard let value = value else { return set(nil, forKey: defaultName) }
        let encoder = PropertyListEncoder()
        encoder.outputFormat = .xml
        let data = try? encoder.encode(value)<commit_msg>Removing xml output format from User Defaults<commit_after>//
//  Storage.swift
//  Sweeft
//
//  Created by <NAME> on 10/1/17.
//

import Foundation

public enum Storage {
    case userDefaults
    case keychain
    
    var object: StorageItem {
        switch self {
        case .userDefaults:
            return UserDefaults.standard
        default:
            return Keychain()
        }
        
    }
    
}

protocol StorageItem {
    func object<C: Codable>(forKey defaultName: String) -> C?
    func set<C: Codable>(_ value: C?, forKey defaultName: String)
}

extension UserDefaults: StorageItem {
    
    func object<C>(forKey defaultName: String) -> C? where C: Codable {
        
        guard let data = data(forKey: defaultName) else { return nil }
        let decoder = PropertyListDecoder()
        return try? decoder.decode(C.self, from: data)
    }
    
    func set<C>(_ value: C?, forKey defaultName: String) where C : Decodable, C : Encodable {
        
        guard let value = value else { return set(nil, forKey: defaultName) }
        let encoder = PropertyListEncoder()
        let data = try? encoder.encode(value)
{'fraction_non_alphanumeric': 0.09695652173913044, 'fraction_numerical': 0.004347826086956522, 'mean_word_length': 2.231741573033708, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '6802940', 'n_tokens_mistral': 697, 'n_tokens_neox': 664, 'n_words': 207}