grace/tpb
Tee your Paste Board
git clone https://git.gae.moe/grace/tpb
c6a3242
0.1.4
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 = 8if ( 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 { 20if ( ! @import ( "std" ) . meta . hasFn ( T , name )) 21@compileError ( @typeName ( T ) ++ " missing fn " ++ name ) ; 22if ( @TypeOf ( @field ( T , name )) != Sig ) 23@compileError ( @typeName ( T ) ++ "." ++ name ++ " must be " ++ @typeName ( Sig )) ; 24} 25 26test { 27inline for ( @typeInfo ( @This ()) . @"struct" . decl_names ) | decl | { 28// Only grab types of structs 29const T = @field ( @This () , decl ) ; 30if ( @TypeOf ( T ) != type ) continue ; 31if ( @typeInfo ( T ) != . @"struct" ) continue ; 32 33// ============================== 34// === Interface Requirements === 35// ============================== 36assertFn ( T , "writeCopyBuffer" , fn ( * T , [] const u8 ) anyerror ! void ) ; 37assertFn ( T , "writePasteboard" , fn ( * T ) anyerror ! void ) ; 38} 39}