Support Codable

This commit is contained in:
Guoye Zhang committed 2023-08-07 18:09:53 -07:00
1 parent 68deedb6b9
commit 792abeab1f
6 files changed
+329 -8

No files matched your search

+25
View File
@@ -306,6 +306,31 @@ extension HTTPFields: CustomDebugStringConvertible {
}
}
extension HTTPFields: Codable {
public func encode(to encoder: Encoder) throws {
var container = encoder.unkeyedContainer()
try container.encode(contentsOf: self)
}
private enum DecodingError: Error {
case isPseudo(String)
}
public init(from decoder: Decoder) throws {
var container = try decoder.unkeyedContainer()
if let count = container.count {
self.reserveCapacity(count)
}
while !container.isAtEnd {
let field = try container.decode(HTTPField.self)
guard !field.name.rawName.hasPrefix(":") else {
throw DecodingError.isPseudo(field.name.rawName)
}
self.append(field)
}
}
}
extension Array {
// `removalIndices` must be ordered.
mutating func remove(at removalIndices: some Sequence<Index>) {