forked from ishkawa/APIKit
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStringDataParser.swift
More file actions
33 lines (26 loc) · 943 Bytes
/
StringDataParser.swift
File metadata and controls
33 lines (26 loc) · 943 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import Foundation
/// `StringDataParser` parses data and convert it to string.
public class StringDataParser: DataParser {
public enum Error: Swift.Error {
case invalidData(Data)
}
/// The string encoding of the data.
public let encoding: String.Encoding
/// Returns `StringDataParser` with the string encoding.
public init(encoding: String.Encoding = .utf8) {
self.encoding = encoding
}
// MARK: - DataParser
/// Value for `Accept` header field of HTTP request.
public var contentType: String? {
return nil
}
/// Return `String` that converted from `Data`.
/// - Throws: `StringDataParser.Error` when the parser fails to initialize `String` from `Data`.
public func parse(data: Data) throws -> Any {
guard let string = String(data: data, encoding: encoding) else {
throw Error.invalidData(data)
}
return string
}
}