forked from vapor/postgres-nio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostgresMessageType.swift
32 lines (26 loc) · 1 KB
/
PostgresMessageType.swift
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
import NIOCore
public protocol PostgresMessageType {
static var identifier: PostgresMessage.Identifier { get }
static func parse(from buffer: inout ByteBuffer) throws -> Self
func serialize(into buffer: inout ByteBuffer) throws
}
extension PostgresMessageType {
func message() throws -> PostgresMessage {
var buffer = ByteBufferAllocator().buffer(capacity: 0)
try self.serialize(into: &buffer)
return .init(identifier: Self.identifier, data: buffer)
}
public init(message: PostgresMessage) throws {
var message = message
self = try Self.parse(from: &message.data)
}
public static var identifier: PostgresMessage.Identifier {
return .none
}
public static func parse(from buffer: inout ByteBuffer) throws -> Self {
fatalError("\(Self.self) does not support parsing.")
}
public func serialize(into buffer: inout ByteBuffer) throws {
fatalError("\(Self.self) does not support serializing.")
}
}