0001    // MediaTypeParserCollection.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 MediaType
0026    @_exported import InterchangeData
0027    
0028    public enum MediaTypeSerializerCollectionError
MediaTypeSerializerCollection.swift:58
        throw MediaTypeSerializerCollectionError.MediaTypeNotFound
MediaTypeSerializerCollection.swift:92
            throw MediaTypeSerializerCollectionError.NoSuitableSerializer
: ErrorType { 0029 case NoSuitableSerializer
MediaTypeSerializerCollection.swift:92
            throw MediaTypeSerializerCollectionError.NoSuitableSerializer
0030 case MediaTypeNotFound
MediaTypeSerializerCollection.swift:58
        throw MediaTypeSerializerCollectionError.MediaTypeNotFound
0031 } 0032 0033 public final class MediaTypeSerializerCollection { 0034 public var serializers: [(MediaType, InterchangeDataSerializer)] = [] 0035 0036 public var mediaTypes: [MediaType] { 0037 return serializers.map({$0.0}) 0038 } 0039 0040 public init() {} 0041 0042 public func setPriority(mediaTypes: MediaType...) throws { 0043 for mediaType in mediaTypes.reverse() { 0044 try setTopPriority(mediaType) 0045 } 0046 } 0047 0048 public func setTopPriority(mediaType: MediaType) throws { 0049 for index in 0 ..< serializers.count { 0050 let tuple = serializers[index] 0051 if tuple.0 == mediaType { 0052 serializers.removeAtIndex(index) 0053 serializers.insert(tuple, atIndex: 0) 0054 return 0055 } 0056 } 0057 0058 throw MediaTypeSerializerCollectionError.MediaTypeNotFound 0059 } 0060 0061 public func add(mediaType: MediaType, serializer: InterchangeDataSerializer) { 0062 serializers.append(mediaType, serializer) 0063 } 0064 0065 public func serializersFor(mediaType: MediaType) -> [(MediaType, InterchangeDataSerializer)] { 0066 return serializers.reduce([]) { 0067 if $1.0.matches(mediaType) { 0068 return $0 + [($1.0, $1.1)] 0069 } else { 0070 return $0 0071 } 0072 } 0073 } 0074 0075 public func serialize(data: InterchangeData, mediaTypes: [MediaType]) throws -> (MediaType, Data) { 0076 var lastError: ErrorType? 0077 0078 for acceptedType in mediaTypes { 0079 for (mediaType, serializer) in serializersFor(acceptedType) { 0080 do { 0081 return try (mediaType, serializer.serialize(data)) 0082 } catch { 0083 lastError = error 0084 continue 0085 } 0086 } 0087 } 0088 0089 if let lastError = lastError { 0090 throw lastError 0091 } else { 0092 throw MediaTypeSerializerCollectionError.NoSuitableSerializer 0093 } 0094 } 0095 } 0096