<commit_before>//
//  Codable.swift
//  BSON
//
//  Created by <NAME> on 13/06/2017.
//

import Foundation

// MARK: - Codable Conformance

fileprivate struct DocumentCodingKey : CodingKey {
    let intValue: Int? = nil
    var stringValue: String
    
    init?(intValue: Int) {
        return nil
    }
    
    init?(stringValue: String) {
        self.stringValue = stringValue
    }
    
    init(_ string: String) {
        self.stringValue = string
    }
}

extension ObjectId : Codable {
    public func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()
        
        if let container = container as? _BSONSingleValueEncodingContainer {
            container.encoder.target.primitive = self
            return
        }
        
        try container.encode(self.hexString)
    }
    
    public init(from decoder: Decoder) throws {
        let singleValueContainer = try decoder.singleValueContainer()
        
        if let container = singleValueContainer as? _BSONSingleValueDecodingContainer {
            guard let id = container.decoder.target.primitive as? ObjectId else {
                throw DecodingError.typeMismatch(ObjectId.self, DecodingError.Context(codingPath: container.decoder.codingPath, debugDescription: "Expected ObjectId but got \(String(describing: container.decoder.target.primitive ?? nil))"))
            }
            
            self = id
            return
        }
        
        let string = try singleValueContainer.decode(String.self)
        self = try ObjectId(string)
    }
}

extension Document : Codable {
    public func encode(to encoder: Encoder) throws {
        throw EncodingError.invalidValue(self, EncodingError.Context(codingPath: encoder.codingPath, debugDescription: "Document can only be encoded by BSONEncoder."))
    }
    
    public init(from decoder: Decoder) throws {
        throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Document can only be decoded by BSONDecoder."))
    }
}

// MARK: - Helpers

// These primitives cannot be added to the document using a subscript
fileprivate protocol _BSONFakePrimitive {}
extension Dictionary : _BSONFakePrimitive {}
extension Array : _BSONFakePrimitive {}

// MARK: - Encoding

public class BSONEncoder {
    
    public init() {}
    
    public func encode<T : Encodable>(_ value: T) throws -> Document {
        let encoder = _BSONEncoder()
        try value.encode(to: encoder)
        <commit_msg>Make BSONEncoder.encode non-generic<commit_after>//
//  Codable.swift
//  BSON
//
//  Created by <NAME> on 13/06/2017.
//

import Foundation

// MARK: - Codable Conformance

fileprivate struct DocumentCodingKey : CodingKey {
    let intValue: Int? = nil
    var stringValue: String
    
    init?(intValue: Int) {
        return nil
    }
    
    init?(stringValue: String) {
        self.stringValue = stringValue
    }
    
    init(_ string: String) {
        self.stringValue = string
    }
}

extension ObjectId : Codable {
    public func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()
        
        if let container = container as? _BSONSingleValueEncodingContainer {
            container.encoder.target.primitive = self
            return
        }
        
        try container.encode(self.hexString)
    }
    
    public init(from decoder: Decoder) throws {
        let singleValueContainer = try decoder.singleValueContainer()
        
        if let container = singleValueContainer as? _BSONSingleValueDecodingContainer {
            guard let id = container.decoder.target.primitive as? ObjectId else {
                throw DecodingError.typeMismatch(ObjectId.self, DecodingError.Context(codingPath: container.decoder.codingPath, debugDescription: "Expected ObjectId but got \(String(describing: container.decoder.target.primitive ?? nil))"))
            }
            
            self = id
            return
        }
        
        let string = try singleValueContainer.decode(String.self)
        self = try ObjectId(string)
    }
}

extension Document : Codable {
    public func encode(to encoder: Encoder) throws {
        throw EncodingError.invalidValue(self, EncodingError.Context(codingPath: encoder.codingPath, debugDescription: "Document can only be encoded by BSONEncoder."))
    }
    
    public init(from decoder: Decoder) throws {
        throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Document can only be decoded by BSONDecoder."))
    }
}

// MARK: - Helpers

// These primitives cannot be added to the document using a subscript
fileprivate protocol _BSONFakePrimitive {}
extension Dictionary : _BSONFakePrimitive {}
extension Array : _BSONFakePrimitive {}

// MARK: - Encoding

public class BSONEncoder {
    
    public init() {}
    
    public func encode(_ value: Encodable) throws -> Document {
        let encoder = _BSONEncoder()
        try value.encode(to: encoder)
        
{'fraction_non_alphanumeric': 0.08325093929207039, 'fraction_numerical': 0.0031639311844967374, 'mean_word_length': 2.6, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 8, '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': '6687532', 'n_tokens_mistral': 1418, 'n_tokens_neox': 1357, 'n_words': 371}