summaryrefslogtreecommitdiff
path: root/src/main.zig
blob: e425e56e48ded0bd9f61f36f053e0114ca459466 (plain) (blame)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const is_debug = builtin.mode == .Debug;

/// This creates a debug allocator that can only be referenced in debug mode.
/// You should check for is_debug around every reference to dba.
var dba: DebugAllocator =
    if (is_debug)
        DebugAllocator.init
    else
        @compileError("Should not use debug allocator in release mode");

pub fn main() !void {
    defer if (is_debug) {
        _ = dba.deinit();
    };

    const gpa = if (is_debug) dba.allocator() else std.heap.smp_allocator;

    // CLI parsing adapted from the example here
    // https://github.com/Hejsil/zig-clap/blob/e47028deaefc2fb396d3d9e9f7bd776ae0b2a43a/README.md#examples

    // First we specify what parameters our program can take.
    // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)`.
    const params = comptime clap.parseParamsComptime(
        \\-h, --help           Display this help and exit.
        \\-r, --relay <str>    A relay message to send.
        \\-c, --connect <str>  A connection message to send.
        \\
    );

    // Initialize our diagnostics, which can be used for reporting useful errors.
    // This is optional. You can also pass `.{}` to `clap.parse` if you don't
    // care about the extra information `Diagnostics` provides.
    var diag = clap.Diagnostic{};
    var res = clap.parse(clap.Help, &params, clap.parsers.default, .{
        .diagnostic = &diag,
        .allocator = gpa,
    }) catch |err| {
        // Report useful error and exit.
        diag.report(std.io.getStdErr().writer(), err) catch {};
        return err;
    };
    defer res.deinit();

    try Saprus.init();
    defer Saprus.deinit();

    if (res.args.help != 0) {
        return clap.help(std.io.getStdErr().writer(), clap.Help, &params, .{});
    }

    if (res.args.relay) |r| {
        try Saprus.sendRelay(if (r.len > 0) r else "Hello darkness my old friend", gpa);
        std.debug.print("Sent: {s}\n", .{r});
        return;
    } else if (res.args.connect) |c| {
        const conn_res: ?SaprusMessage = Saprus.connect(if (c.len > 0) c else "Hello darkness my old friend", gpa) catch |err| switch (err) {
            error.WouldBlock => null,
            else => return err,
        };
        defer if (conn_res) |r| r.deinit(gpa);
        if (conn_res) |r| {
            std.debug.print("{s}\n", .{r.connection.payload});
        } else {
            std.debug.print("No response from connection request\n", .{});
        }
        return;
    }

    return clap.help(std.io.getStdErr().writer(), clap.Help, &params, .{});
}

const builtin = @import("builtin");
const std = @import("std");
const DebugAllocator = std.heap.DebugAllocator(.{});
const ArrayList = std.ArrayList;

const Saprus = @import("./saprus.zig");
const SaprusMessage = Saprus.SaprusMessage;

const clap = @import("clap");