grace/tpb

Tee your Paste Board

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

Grace Yodermisc: cleanup63265d1

0.1.0
1.5 KiB37 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
10// I am not sure if this is super idiomatic Zig or if this is bad practice, but
11// by ensuring that all Clip backends implement the same methods, we are able
12// to use the `anytype` type and we are able to have an interface that is able
13// to dispatch statically
14
15/// Check if a function with the given name and type exists.
16/// `@compileError` if not
17fn assertFn(comptime T: type, comptime name: []const u8, comptime Sig: type) void {
18    if (!@import("std").meta.hasFn(T, name))
19        @compileError(@typeName(T) ++ " missing fn " ++ name);
20    if (@TypeOf(@field(T, name)) != Sig)
21        @compileError(@typeName(T) ++ "." ++ name ++ " must be " ++ @typeName(Sig));
22}
23
24test {
25    inline for (@typeInfo(@This()).@"struct".decls) |decl| {
26        // Only grab types of structs
27        const T = @field(@This(), decl.name);
28        if (@TypeOf(T) != type) continue;
29        if (@typeInfo(T) != .@"struct") continue;
30
31        // ==============================
32        // === Interface Requirements ===
33        // ==============================
34        assertFn(T, "writeCopyBuffer", fn (*T, []const u8) anyerror!void);
35        assertFn(T, "writePasteboard", fn (*T) anyerror!void);
36    }
37}