0001    // URLEncodedFormInterchangeDataSerializer.swift
0002    //
0003    // The MIT License (MIT)
0004    //
0005    // Copyright (c) 2015 Zewo
0006    //
0007    // Permission is hereby granted, free of charge, to any person obtaining a copy
0008    // of this software and associated documentation files (the "Software"), to deal
0009    // in the Software without restriction, including without limitation the rights
0010    // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0011    // copies of the Software, and to permit persons to whom the Software is
0012    // furnished to do so, subject to the following conditions:
0013    //
0014    // The above copyright notice and this permission notice shall be included in all
0015    // copies or substantial portions of the Software.
0016    //
0017    // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0018    // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0019    // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0020    // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0021    // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0022    // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
0023    // SOFTWARE.
0024    
0025    @_exported import InterchangeData
0026    
0027    enum URLEncodedFormInterchangeDataSerializeError: ErrorType {
0028        case InvalidInterchangeData
0029    }
0030    
0031    public struct URLEncodedFormInterchangeDataSerializer: InterchangeDataSerializer {
0032        public init() {}
0033    
0034        public func serialize(interchangeData: InterchangeData) throws -> Data {
0035            return try serializeToString(interchangeData).data
0036        }
0037    
0038        public func serializeToString(interchangeData: InterchangeData) throws -> String {
0039            switch interchangeData {
0040            case .NullValue: throw URLEncodedFormSerializeError.InvalidInterchangeData
0041            case .BooleanValue: throw URLEncodedFormSerializeError.InvalidInterchangeData
0042            case .NumberValue: throw URLEncodedFormSerializeError.InvalidInterchangeData
0043            case .StringValue: throw URLEncodedFormSerializeError.InvalidInterchangeData
0044            case .ArrayValue: throw URLEncodedFormSerializeError.InvalidInterchangeData
0045            case .ObjectValue(let object): return try serializeObject(object)
0046            }
0047        }
0048    
0049        func serializeObject(object: [String: InterchangeData]) throws -> String {
0050            var string = ""
0051    
0052            for (index, (key, value)) in object.enumerate() {
0053                switch value {
0054                case .NullValue: throw URLEncodedFormSerializeError.InvalidInterchangeData
0055                case .BooleanValue: throw URLEncodedFormSerializeError.InvalidInterchangeData
0056                case .NumberValue: throw URLEncodedFormSerializeError.InvalidInterchangeData
0057                case .StringValue(let s):
0058                    if index != 0 {
0059                        string += "&"
0060                    }
0061    
0062                    string += "\(key)=\(s)"
0063                case .ArrayValue: throw URLEncodedFormSerializeError.InvalidInterchangeData
0064                case .ObjectValue: throw URLEncodedFormSerializeError.InvalidInterchangeData
0065                }
0066            }
0067    
0068            return string
0069        }
0070    }
0071