refactor(commands): add a substitute constructor

This commit recuces some repetitiveness from creating substitution
literals in tests.
This commit is contained in:
Jacob Jonsson 2026-03-14 11:43:26 +01:00
parent a05229f72d
commit b0f0daa19d
Signed by: Jassob
GPG key ID: 7E30B9B047F7202E
2 changed files with 12 additions and 14 deletions

View file

@ -269,9 +269,7 @@ test "execute substitution" {
bot.hear(msg);
// execute substitution
const cmd = UserCommand{
.substitute = .{ .author = "jassob", .needle = "What", .replacement = "what" },
};
const cmd = UserCommand.init_substitute("jassob", "What", "what", false);
const response = try bot.execute(&cmd, null, "#test");
// expect response matching the correct message
@ -292,9 +290,7 @@ test "execute substitution with no matching needle" {
bot.hear(msg);
// execute substitution
const cmd = UserCommand{
.substitute = .{ .author = "jassob", .needle = "something else", .replacement = "weird" },
};
const cmd = UserCommand.init_substitute("jassob", "something else", "weird", false);
try std.testing.expectError(Error.NoMessage, bot.execute(
&cmd,
null,

View file

@ -9,6 +9,15 @@ pub const UserCommand = union(enum) {
/// !help
help: void,
pub fn init_substitute(author: []const u8, needle: []const u8, replacement: []const u8, all: bool) UserCommand {
return .{ .substitute = .{
.author = author,
.needle = needle,
.replacement = replacement,
.all = all,
} };
}
pub fn parse(nick: []const u8, text: []const u8) ?UserCommand {
const original = Parser.init(text);
if (original.consume_str("!help")) |_| {
@ -38,14 +47,7 @@ pub const UserCommand = union(enum) {
return null;
}
parser, const correction = result.?;
return .{
.substitute = .{
.author = nick,
.needle = typo,
.replacement = correction,
.all = false,
},
};
return .init_substitute(nick, typo, correction, false);
}
return null;
}