From c32c1df00a94f5537f58c031bff0cd88f0eb39cf Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Mon, 8 Apr 2024 21:39:47 -0700 Subject: [PATCH] module: improve validation of missing mod name Signed-off-by: Stephen Gutekanst --- src/module/module.zig | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/module/module.zig b/src/module/module.zig index e9ac05bf..ec45f967 100644 --- a/src/module/module.zig +++ b/src/module/module.zig @@ -69,15 +69,21 @@ const is_debug = @import("Archetype.zig").is_debug; /// Verifies that M matches the basic layout of a Mach module fn ModuleInterface(comptime M: type) type { - if (@typeInfo(M) != .Struct) @compileError("mach: expected module struct, found: " ++ @typeName(M)); - if (!@hasDecl(M, "name")) @compileError("mach: module must have `pub const name = .foobar;`"); - if (@typeInfo(@TypeOf(M.name)) != .EnumLiteral) @compileError("mach: module must have `pub const name = .foobar;`, found type:" ++ @typeName(M.name)); - if (@hasDecl(M, "global_events")) validateEvents("mach: module ." ++ @tagName(M.name) ++ " global_events ", M.global_events); - if (@hasDecl(M, "local_events")) validateEvents("mach: module ." ++ @tagName(M.name) ++ " local_events ", M.local_events); - _ = ComponentTypesM(M); + validateModule(M, true); return M; } +fn validateModule(comptime M: type, comptime events: bool) void { + if (@typeInfo(M) != .Struct) @compileError("mach: expected module struct, found: " ++ @typeName(M)); + if (!@hasDecl(M, "name")) @compileError("mach: module must have `pub const name = .foobar;`: " ++ @typeName(M)); + if (@typeInfo(@TypeOf(M.name)) != .EnumLiteral) @compileError("mach: module must have `pub const name = .foobar;`, found type:" ++ @typeName(M.name)); + if (events) { + if (@hasDecl(M, "global_events")) validateEvents("mach: module ." ++ @tagName(M.name) ++ " global_events ", M.global_events); + if (@hasDecl(M, "local_events")) validateEvents("mach: module ." ++ @tagName(M.name) ++ " local_events ", M.local_events); + _ = ComponentTypesM(M); + } +} + /// TODO: implement serialization constraints /// For now this exists just to indicate things that we expect will be required to be serializable in /// the future. @@ -908,6 +914,7 @@ pub fn ComponentTypesByName(comptime modules: anytype) type { /// } /// ``` fn ComponentTypesM(comptime M: anytype) type { + validateModule(M, false); const error_prefix = "mach: module ." ++ @tagName(M.name) ++ " .components "; if (!@hasDecl(M, "components")) { return struct {};