tiny.smg.command.inspection
Defined in command.
API (1)
Actions
Public operations.
Source
Source: tools/smg/src/command/inspection.zig
zig
const std = @import("std");const smg = @import("../root.zig");const cli = @import("../root.zig").cli;pub fn run(allocator: std.mem.Allocator, args: []const []const u8) !u8 { std.debug.assert(cli.options.hasFlag(args, "--inspect")); const options = &.{ "--root", "--bytes", "--deadline-ms" }; if (try cli.validation.rejectMissingOptionValues(allocator, args, options)) return 2; if (try cli.validation.rejectUnexpectedOptionsPlain( allocator, "context", "--inspect --root ABS [OPTIONS] TARGET...", args, &.{ "--inspect", "--root", "--bytes", "--deadline-ms" }, )) return 2; const request = parse(args) catch |err| { try cli.output.writeClickUsageError( allocator, "context", "--inspect --root ABS [OPTIONS] TARGET...", @errorName(err), ); return 2; }; var inspected = try smg.context.inspect( allocator, request.root, request.targets[0..request.count], request.options, ); defer inspected.deinit(); const bytes = try smg.context.render.bounded( allocator, inspected.result, request.options.bytes, ); defer allocator.free(bytes); std.debug.assert(bytes.len <= request.options.bytes); return cli.output.writeOut(bytes);}const Request = struct { root: []const u8 = "", targets: [smg.context.model.targets_max][]const u8 = undefined, count: usize = 0, options: smg.context.InspectOptions = .{},};fn parse(args: []const []const u8) !Request { var result = Request{}; var seen: u8 = 0; var index: usize = 0; while (index < args.len) : (index += 1) { std.debug.assert(result.count <= result.targets.len); const arg = args[index]; if (std.mem.eql(u8, arg, "--inspect")) continue; if (std.mem.startsWith(u8, arg, "--")) { if (index + 1 == args.len) return error.MissingInspectionOption; index += 1; try option(&result, &seen, arg, args[index]); continue; } if (result.count == result.targets.len) return error.InvalidInspectionTargets; result.targets[result.count] = arg; result.count += 1; } try smg.context.model.validate(result.root, result.targets[0..result.count], result.options); return result;}fn option(result: *Request, seen: *u8, name: []const u8, value: []const u8) !void { const bit: u8 = if (std.mem.eql(u8, name, "--root")) 1 else if (std.mem.eql(u8, name, "--bytes")) 2 else if (std.mem.eql(u8, name, "--deadline-ms")) 4 else return error.InvalidInspectionOption; std.debug.assert(seen.* <= 7); if (seen.* & bit != 0) return error.DuplicateInspectionOption; seen.* |= bit; switch (bit) { 1 => result.root = value, 2 => result.options.bytes = try std.fmt.parseInt(u32, value, 10), 4 => result.options.deadline_ms = try std.fmt.parseInt(u16, value, 10), else => unreachable, }}test "context inspection arguments expose immediate cooperative exhaustion" { const parsed = try parse(&.{ "--inspect", "--root", "/tmp", "--deadline-ms", "0", "src/a.zig", }); try std.testing.expectEqual(@as(u16, 0), parsed.options.deadline_ms); try std.testing.expectEqual(@as(usize, 1), parsed.count); try std.testing.expectError(error.InvalidInspectionRoot, parse(&.{ "--inspect", "a" })); try std.testing.expectError(error.InvalidInspectionLimit, parse(&.{ "--inspect", "--root", "/tmp", "--bytes", "65537", "a", }));}Source: tools/smg/src/command/root.zig:10
zig
pub const inspection = @import("inspection.zig");Audit
| Definitions | 2 |
|---|---|
| Public names | 2 |
| Members | 0 |
| Version | 26.7.0 |
| Revision | daab053ee433 |