0001    //
0002    //  RawRepresentableDecodable.swift
0003    //  Decodable
0004    //
0005    //  Created by Daniel Garbień on 06/11/15.
0006    //  Copyright © 2015 anviking. All rights reserved.
0007    //
0008    
0009    /**
0010     * Extends all RawRepresentables (enums) which are also Decodable with decode implementation.
0011     *
0012     * I could not find a way to implicitly declare RawRepresentable conforming to Decodable, what would make all enums Decodable automatically.
0013     * Because of that for an enum to be compatible with Decodable operators it must be declared as implementing Decodable protocol.
0014     */
0015    public extension RawRepresentable where RawValue: Decodable, Self: Decodable {
0016        
0017        static func decode(json: AnyObject) throws -> Self {
0018            let rawValue = try RawValue.decode(json)
0019            guard let rawRepresentable = Self(rawValue: rawValue) else {
0020                throw RawRepresentableInitializationError(type: self, rawValue: rawValue, object: json)
0021            }
0022            return rawRepresentable
0023        }
0024    }
0025