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 MediaTypeParserCollectionError: ErrorType { 0029 case NoSuitableParser
MediaTypeParserCollection.swift:58 throw MediaTypeParserCollectionError.MediaTypeNotFoundMediaTypeParserCollection.swift:90 throw MediaTypeParserCollectionError.NoSuitableParser0030 case MediaTypeNotFound
MediaTypeParserCollection.swift:90 throw MediaTypeParserCollectionError.NoSuitableParser0031 } 0032 0033 public final class MediaTypeParserCollection { 0034 public var parsers: [(MediaType, InterchangeDataParser)] = [] 0035 0036 public var mediaTypes: [MediaType] { 0037 return parsers.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 ..< parsers.count { 0050 let tuple = parsers[index] 0051 if tuple.0 == mediaType { 0052 parsers.removeAtIndex(index) 0053 parsers.insert(tuple, atIndex: 0) 0054 return 0055 } 0056 } 0057 0058 throw MediaTypeParserCollectionError.MediaTypeNotFound 0059 } 0060 0061 public func add(mediaType: MediaType, parser: InterchangeDataParser) { 0062 parsers.append(mediaType, parser) 0063 } 0064 0065 public func parsersFor(mediaType: MediaType) -> [(MediaType, InterchangeDataParser)] { 0066 return parsers.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 parse(data: Data, mediaType: MediaType) throws -> (MediaType, InterchangeData) { 0076 var lastError: ErrorType? 0077 0078 for (mediaType, parser) in parsersFor(mediaType) { 0079 do { 0080 return try (mediaType, parser.parse(data)) 0081 } catch { 0082 lastError = error 0083 continue 0084 } 0085 } 0086 0087 if let lastError = lastError { 0088 throw lastError 0089 } else { 0090 throw MediaTypeParserCollectionError.NoSuitableParser 0091 } 0092 } 0093 } 0094
MediaTypeParserCollection.swift:58 throw MediaTypeParserCollectionError.MediaTypeNotFound