Compare commits

...

2 Commits

Author SHA1 Message Date
pfg
ea5349e7ed fix 2025-05-02 14:16:56 -07:00
pfg
90c914a24d remove exactsizematcher 2025-05-02 14:14:09 -07:00
6 changed files with 99 additions and 175 deletions

View File

@@ -133,19 +133,19 @@ pub const Bunfig = struct {
}
}
const LogLevelMap = bun.ComptimeStringMap(Api.MessageLevel, .{
.{ "debug", Api.MessageLevel.debug },
.{ "error", Api.MessageLevel.err },
.{ "warn", Api.MessageLevel.warn },
.{ "info", Api.MessageLevel.info },
});
fn loadLogLevel(this: *Parser, expr: js_ast.Expr) !void {
try this.expectString(expr);
const Matcher = strings.ExactSizeMatcher(8);
this.bunfig.log_level = switch (Matcher.match(expr.asString(this.allocator).?)) {
Matcher.case("debug") => Api.MessageLevel.debug,
Matcher.case("error") => Api.MessageLevel.err,
Matcher.case("warn") => Api.MessageLevel.warn,
Matcher.case("info") => Api.MessageLevel.info,
else => {
try this.addError(expr.loc, "Invalid log level, must be one of debug, error, or warn");
unreachable;
},
this.bunfig.log_level = LogLevelMap.get(expr.asString(this.allocator).?) orelse blk: {
try this.addError(expr.loc, "Invalid log level, must be one of debug, error, or warn");
break :blk .info;
};
}

View File

@@ -103,7 +103,6 @@ pub const debug_flags = if (Environment.show_crash_trace) struct {
}
} else @compileError("Do not access this namespace in a release build");
const LoaderMatcher = strings.ExactSizeMatcher(4);
const ColonListType = @import("./cli/colon_list_type.zig").ColonListType;
pub const LoaderColonList = ColonListType(Api.Loader, Arguments.loader_resolver);
pub const DefineColonList = ColonListType(string, Arguments.noop_resolver);
@@ -949,7 +948,6 @@ pub const Arguments = struct {
}
}
const TargetMatcher = strings.ExactSizeMatcher(8);
if (args.option("--target")) |_target| brk: {
if (comptime cmd == .BuildCommand) {
if (args.flag("--compile")) {
@@ -965,12 +963,18 @@ pub const Arguments = struct {
}
}
opts.target = opts.target orelse switch (TargetMatcher.match(_target)) {
TargetMatcher.case("browser") => Api.Target.browser,
TargetMatcher.case("node") => Api.Target.node,
TargetMatcher.case("macro") => if (cmd == .BuildCommand) Api.Target.bun_macro else Api.Target.bun,
TargetMatcher.case("bun") => Api.Target.bun,
else => invalidTarget(&diag, _target),
const TargetMap = bun.ComptimeStringMap(Api.Target, .{
.{ "browser", Api.Target.browser },
.{ "node", Api.Target.node },
.{ "macro", Api.Target.bun_macro },
.{ "bun", Api.Target.bun },
});
opts.target = opts.target orelse blk: {
const match = TargetMap.get(_target) orelse {
break :blk invalidTarget(&diag, _target);
};
if (match == .bun_macro and cmd != .BuildCommand) break :blk .bun;
break :blk match;
};
if (opts.target.? == .bun) {
@@ -1710,79 +1714,79 @@ pub const Command = struct {
}
const first_arg_name = next_arg;
const RootCommandMatcher = strings.ExactSizeMatcher(12);
return switch (RootCommandMatcher.match(first_arg_name)) {
RootCommandMatcher.case("init") => .InitCommand,
RootCommandMatcher.case("build"), RootCommandMatcher.case("bun") => .BuildCommand,
RootCommandMatcher.case("discord") => .DiscordCommand,
RootCommandMatcher.case("upgrade") => .UpgradeCommand,
RootCommandMatcher.case("completions") => .InstallCompletionsCommand,
RootCommandMatcher.case("getcompletes") => .GetCompletionsCommand,
RootCommandMatcher.case("link") => .LinkCommand,
RootCommandMatcher.case("unlink") => .UnlinkCommand,
RootCommandMatcher.case("x") => .BunxCommand,
RootCommandMatcher.case("repl") => .ReplCommand,
RootCommandMatcher.case("i"),
RootCommandMatcher.case("install"),
=> brk: {
for (args_iter.buf) |arg| {
if (arg.len > 0 and (strings.eqlComptime(arg, "-g") or strings.eqlComptime(arg, "--global"))) {
break :brk .AddCommand;
}
const match = command_map.get(first_arg_name) orelse .AutoCommand;
if (match == .InstallCommand) {
for (args_iter.buf) |arg| {
if (arg.len > 0 and (strings.eqlComptime(arg, "-g") or strings.eqlComptime(arg, "--global"))) {
return .AddCommand;
}
break :brk .InstallCommand;
},
RootCommandMatcher.case("c"), RootCommandMatcher.case("create") => .CreateCommand,
RootCommandMatcher.case("test") => .TestCommand,
RootCommandMatcher.case("pm") => .PackageManagerCommand,
RootCommandMatcher.case("add"), RootCommandMatcher.case("a") => .AddCommand,
RootCommandMatcher.case("update") => .UpdateCommand,
RootCommandMatcher.case("patch") => .PatchCommand,
RootCommandMatcher.case("patch-commit") => .PatchCommitCommand,
RootCommandMatcher.case("r"),
RootCommandMatcher.case("remove"),
RootCommandMatcher.case("rm"),
RootCommandMatcher.case("uninstall"),
=> .RemoveCommand,
RootCommandMatcher.case("run") => .RunCommand,
RootCommandMatcher.case("help") => .HelpCommand,
RootCommandMatcher.case("exec") => .ExecCommand,
RootCommandMatcher.case("outdated") => .OutdatedCommand,
RootCommandMatcher.case("publish") => .PublishCommand,
// These are reserved for future use by Bun, so that someone
// doing `bun deploy` to run a script doesn't accidentally break
// when we add our actual command
RootCommandMatcher.case("deploy") => .ReservedCommand,
RootCommandMatcher.case("cloud") => .ReservedCommand,
RootCommandMatcher.case("info") => .ReservedCommand,
RootCommandMatcher.case("config") => .ReservedCommand,
RootCommandMatcher.case("use") => .ReservedCommand,
RootCommandMatcher.case("auth") => .ReservedCommand,
RootCommandMatcher.case("login") => .ReservedCommand,
RootCommandMatcher.case("logout") => .ReservedCommand,
RootCommandMatcher.case("whoami") => .ReservedCommand,
RootCommandMatcher.case("prune") => .ReservedCommand,
RootCommandMatcher.case("list") => .ReservedCommand,
RootCommandMatcher.case("why") => .ReservedCommand,
RootCommandMatcher.case("-e") => .AutoCommand,
else => .AutoCommand,
};
}
}
return match;
}
const command_map = bun.ComptimeStringMap(Tag, .{
.{ "init", .InitCommand },
.{ "build", .BuildCommand },
.{ "discord", .DiscordCommand },
.{ "upgrade", .UpgradeCommand },
.{ "completions", .InstallCompletionsCommand },
.{ "getcompletes", .GetCompletionsCommand },
.{ "link", .LinkCommand },
.{ "unlink", .UnlinkCommand },
.{ "x", .BunxCommand },
.{ "repl", .ReplCommand },
.{ "i", .InstallCommand },
.{ "iinstall", .InstallCommand },
.{ "c", .CreateCommand },
.{ "create", .CreateCommand },
.{ "test", .TestCommand },
.{ "pm", .PackageManagerCommand },
.{ "add", .AddCommand },
.{ "a", .AddCommand },
.{ "update", .UpdateCommand },
.{ "patch", .PatchCommand },
.{ "patch-commit", .PatchCommitCommand },
.{ "r", .RemoveCommand },
.{ "remove", .RemoveCommand },
.{ "rm", .RemoveCommand },
.{ "uninstall", .RemoveCommand },
.{ "run", .RunCommand },
.{ "help", .HelpCommand },
.{ "exec", .ExecCommand },
.{ "outdated", .OutdatedCommand },
.{ "publish", .PublishCommand },
// These are reserved for future use by Bun, so that someone
// doing `bun deploy` to run a script doesn't accidentally break
// when we add our actual command
.{ "deploy", .ReservedCommand },
.{ "cloud", .ReservedCommand },
.{ "info", .ReservedCommand },
.{ "config", .ReservedCommand },
.{ "use", .ReservedCommand },
.{ "auth", .ReservedCommand },
.{ "login", .ReservedCommand },
.{ "logout", .ReservedCommand },
.{ "whoami", .ReservedCommand },
.{ "prune", .ReservedCommand },
.{ "list", .ReservedCommand },
.{ "why", .ReservedCommand },
.{ "-e", .AutoCommand },
});
const default_completions_list = [_]string{
"build",
"install",

View File

@@ -1,8 +1,6 @@
const std = @import("std");
const bun = @import("bun");
const c = @import("picohttpparser.zig");
const ExactSizeMatcher = bun.ExactSizeMatcher;
const Match = ExactSizeMatcher(2);
const Output = bun.Output;
const Environment = bun.Environment;
const StringBuilder = bun.StringBuilder;

View File

@@ -1,75 +0,0 @@
const std = @import("std");
const bun = @import("bun");
pub fn ExactSizeMatcher(comptime max_bytes: usize) type {
switch (max_bytes) {
1, 2, 4, 8, 12, 16 => {},
else => {
@compileError("max_bytes must be 1, 2, 4, 8, 12, or 16.");
},
}
const T = std.meta.Int(
.unsigned,
max_bytes * 8,
);
return struct {
pub fn match(str: anytype) T {
switch (str.len) {
1...max_bytes - 1 => {
var tmp: [max_bytes]u8 = undefined;
@memcpy(tmp[0..str.len], str);
@memset(tmp[str.len..], 0);
return std.mem.readInt(T, &tmp, .little);
},
max_bytes => {
return std.mem.readInt(T, str[0..max_bytes], .little);
},
0 => {
return 0;
},
else => {
return std.math.maxInt(T);
},
}
}
pub fn matchLower(str: anytype) T {
switch (str.len) {
1...max_bytes - 1 => {
var tmp: [max_bytes]u8 = undefined;
for (str, 0..) |char, i| {
tmp[i] = std.ascii.toLower(char);
}
@memset(tmp[str.len..], 0);
return std.mem.readInt(T, &tmp, .little);
},
max_bytes => {
return std.mem.readInt(T, str[0..max_bytes], .little);
},
0 => {
return 0;
},
else => {
return std.math.maxInt(T);
},
}
}
pub fn case(comptime str: []const u8) T {
if (str.len < max_bytes) {
var bytes = std.mem.zeroes([max_bytes]u8);
bytes[0..str.len].* = str[0..str.len].*;
return std.mem.readInt(T, &bytes, .little);
} else if (str.len == max_bytes) {
return std.mem.readInt(T, str[0..str.len], .little);
} else {
@compileError("str: \"" ++ str ++ "\" too long");
}
}
};
}
const expect = std.testing.expect;

View File

@@ -107,18 +107,17 @@ pub const Integrity = extern struct {
return @intFromEnum(this) >= @intFromEnum(Tag.sha1) and @intFromEnum(this) <= @intFromEnum(Tag.sha512);
}
pub fn parse(buf: []const u8) Tag {
const Matcher = strings.ExactSizeMatcher(8);
const Map = bun.ComptimeStringMap(Tag, .{
.{ "sha1", Tag.sha1 },
.{ "sha256", Tag.sha256 },
.{ "sha384", Tag.sha384 },
.{ "sha512", Tag.sha512 },
});
pub fn parse(buf: []const u8) Tag {
const i = strings.indexOfChar(buf[0..@min(buf.len, 7)], '-') orelse return Tag.unknown;
return switch (Matcher.match(buf[0..i])) {
Matcher.case("sha1") => Tag.sha1,
Matcher.case("sha256") => Tag.sha256,
Matcher.case("sha384") => Tag.sha384,
Matcher.case("sha512") => Tag.sha512,
else => .unknown,
};
return Map.get(buf[0..i]) orelse .unknown;
}
pub inline fn digestLen(this: Tag) usize {

View File

@@ -5063,8 +5063,6 @@ pub fn moveSlice(slice: string, from: string, to: string) string {
return result;
}
pub const ExactSizeMatcher = @import("exact_size_matcher.zig").ExactSizeMatcher;
pub const unicode_replacement = 0xFFFD;
pub const unicode_replacement_str = brk: {
var out: [std.unicode.utf8CodepointSequenceLength(unicode_replacement) catch unreachable]u8 = undefined;