Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make status and method directly codable #53

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Sources/HTTPTypes/HTTPRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,23 @@ extension HTTPRequest: Codable {
}
}

extension HTTPRequest.Method: Codable {
public func encode(to encoder: any Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(self.rawValue)
}

public init(from decoder: any Decoder) throws {
let container = try decoder.singleValueContainer()
let rawMethod = try container.decode(String.self)
guard let method = HTTPRequest.Method(rawMethod) else {
throw DecodingError.dataCorruptedError(in: container, debugDescription: "\"\(rawMethod)\" is not a valid method")
}

self = method
}
}

extension HTTPRequest.Method {
/// GET
///
Expand Down
12 changes: 12 additions & 0 deletions Sources/HTTPTypes/HTTPResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,18 @@ extension HTTPResponse: Codable {
}
}

extension HTTPResponse.Status: Codable {
public func encode(to encoder: any Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(self.code)
}

public init(from decoder: any Decoder) throws {
let code = try decoder.singleValueContainer().decode(Int.self)
self.init(code: code)
}
}

extension HTTPResponse.Status {
// MARK: 1xx

Expand Down