summaryrefslogtreecommitdiff
path: root/src/lib/reply.zig
blob: 866f6b43d17cb49e457b93f10ba632ed94a70937 (plain)
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
34
35
36
37
38
39
40
41
42
43
44
const std = @import("std");
const Io = std.Io;

pub const Code = enum(u16) {
    ready = 220,
    cannot_verify = 252,
    closing = 221,
    start_data = 354,
    ok = 250,
    not_avail = 421,
    bad_cmd = 500,
    bad_param = 501,
    not_impl = 502,
    bad_sequence = 503,
    param_not_impl = 504,
    rejected = 550,
    too_many = 452,
    exceeded = 552,
    param_unrecognized = 555,
    relay_denied = 554,
    storage_error = 451,
    _, //@as(Code, @enumFromInt(user defined code))
};

pub const Action = enum { cont, data, close };

pub const Reply = struct {
    code: Code,
    lines: []const []const u8,
    action: Action = .cont,

    pub fn single(code: Code, comptime text: []const u8) Reply {
        return .{ .code = code, .lines = &.{text} };
    }

    pub fn send(self: Reply, w: *Io.Writer) !void {
        const code = @backingInt(self.code);
        for (self.lines, 0..) |line, i| {
            const sep: u8 = if (i == self.lines.len - 1) ' ' else '-';
            try w.print("{d}{c}{s}\r\n", .{ code, sep, line });
            std.debug.print("S: {d}{c}{s}\r\n", .{ code, sep, line });
        }
    }
};