gitbay/Networking/Envelope.swift
48 lines · 1585 bytes
1import Foundation
2
3/// Exit codes shared by the CLI, SSH and both HTTP surfaces.
4/// `internal/protocol/protocol.go` is the source of truth.
5nonisolated enum ExitCode: Int, Sendable {
6 case ok = 0
7 case failure = 1
8 case usage = 2
9 case notFound = 3
10 case denied = 4
11 case protocolError = 5
12}
13
14/// Every response from either API surface is the command's own JSON
15/// envelope with `exit_code` added.
16///
17/// `data` is absent when a command emits an empty result — Go omits a nil
18/// slice — so an empty list arrives as no key at all rather than `[]`.
19/// `output` carries commands that write raw text instead of JSON (`mr
20/// diff`, `help`); the server wraps those.
21nonisolated struct Envelope<Payload: Decodable & Sendable>: Decodable, Sendable {
22 let protocolVersion: Int
23 let data: Payload?
24 let error: String?
25 let stderr: String?
26 let output: String?
27 let exitCode: Int?
28
29 enum CodingKeys: String, CodingKey {
30 case protocolVersion = "protocol_version"
31 case data, error, stderr, output
32 case exitCode = "exit_code"
33 }
34
35 /// The message to put in front of a person. `error` and `stderr` were
36 /// both written to be read by one.
37 var message: String? {
38 let text = error ?? stderr
39 guard let text, !text.isEmpty else { return nil }
40 return text
41 }
42}
43
44/// Placeholder for commands whose `data` we do not decode. It accepts any
45/// JSON, so discarding a payload never fails on its shape.
46nonisolated struct NoPayload: Decodable, Sendable {
47 init(from _: any Decoder) throws {}
48}