grace/tpb

Tee your Paste Board

git clone https://git.gae.moe/grace/tpb

Grace Yodermisc: update to zig 0.17c6a3242

0.1.4
1.5 KiB39 linesraw
1const std = @import("std");
2const builtin = @import("builtin");
3
4pub const OSC52 = @import("Clip/OSC52.zig");
5// Ideally we would make else be `@compileError` but that
6// messes up with the interface checking
7pub const NSPasteboard =
8    if (builtin.target.os.tag == .macos) @import("Clip/NSPasteboard.zig") else void;
9
10pub const ForkOut = @import("Clip/ForkOut.zig");
11
12// I am not sure if this is super idiomatic Zig or if this is bad practice, but
13// by ensuring that all Clip backends implement the same methods, we are able
14// to use the `anytype` type and we are able to have an interface that is able
15// to dispatch statically
16
17/// Check if a function with the given name and type exists.
18/// `@compileError` if not
19fn assertFn(comptime T: type, comptime name: []const u8, comptime Sig: type) void {
20    if (!@import("std").meta.hasFn(T, name))
21        @compileError(@typeName(T) ++ " missing fn " ++ name);
22    if (@TypeOf(@field(T, name)) != Sig)
23        @compileError(@typeName(T) ++ "." ++ name ++ " must be " ++ @typeName(Sig));
24}
25
26test {
27    inline for (@typeInfo(@This()).@"struct".decl_names) |decl| {
28        // Only grab types of structs
29        const T = @field(@This(), decl);
30        if (@TypeOf(T) != type) continue;
31        if (@typeInfo(T) != .@"struct") continue;
32
33        // ==============================
34        // === Interface Requirements ===
35        // ==============================
36        assertFn(T, "writeCopyBuffer", fn (*T, []const u8) anyerror!void);
37        assertFn(T, "writePasteboard", fn (*T) anyerror!void);
38    }
39}