tiny.profiling.iteration.parse
Defined in iteration.
API (2)
Actions
Public operations.
Source
Source: src/profiling/iteration/parse.zig
zig
const std = @import("std");const sys = @import("sys");const profiling = @import("../root.zig");const model = @import("model.zig");const json = profiling.json;const scenario = profiling.scenario;pub fn load( allocator: std.mem.Allocator, path: []const u8,) !model.Plan { const text = try sys.fs.readFileAlloc( allocator, path, model.maximum_plan_bytes, ); const value = try std.json.parseFromSliceLeaky( std.json.Value, allocator, text, .{}, ); return try parse(allocator, value);}pub fn parse( allocator: std.mem.Allocator, value: std.json.Value,) !model.Plan { const object = try json.object(value); const actual_schema = json.string(object.get("schema")) orelse return error.InvalidIterationSchema; if (!std.mem.eql(u8, actual_schema, model.schema)) { return error.InvalidIterationSchema; } const patch_value = object.get("patch") orelse return error.MissingIterationPatch; const patch_object = try json.object(patch_value); const plan = model.Plan{ .name = try requiredString(object, "name"), .step = try requiredString(object, "step"), .args = try parseArguments(allocator, object.get("args")), .patch = .{ .path = try requiredString(patch_object, "path"), .before = try requiredString(patch_object, "before"), .after = try requiredString(patch_object, "after"), }, .timeout_ms = json.asU64(object.get("timeout_ms")) orelse model.default_timeout_ms, }; try plan.validate(); return plan;}fn parseArguments( allocator: std.mem.Allocator, value: ?std.json.Value,) ![]const []const u8 { const rows = try json.array(value orelse return &.{}); if (rows.items.len > scenario.maximum_arguments) { return error.TooManyIterationArguments; } const result = try allocator.alloc([]const u8, rows.items.len); for (rows.items, 0..) |item, index| { result[index] = json.string(item) orelse return error.InvalidIterationArgument; } return result;}fn requiredString( object: std.json.ObjectMap, field: []const u8,) ![]const u8 { return json.string(object.get(field)) orelse error.InvalidIterationPlan;}test "iteration parser loads semantic edit plans" { var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena_state.deinit(); const allocator = arena_state.allocator(); const value = try std.json.parseFromSliceLeaky( std.json.Value, allocator, \\{"schema":"tiny.profiling.iteration/v1","name":"focused", \\ "step":"profile-harness-test","args":["-Dtest-filter=focused"], \\ "patch":{"path":"src/focused.zig","before":"1","after":"2"}, \\ "timeout_ms":2000} , .{}, ); const plan = try parse(allocator, value); try std.testing.expectEqualStrings("profile-harness-test", plan.step); try std.testing.expectEqual(@as(u64, 2000), plan.timeout_ms); try std.testing.expectEqualStrings("2", plan.patch.after);}Source: src/profiling/iteration/root.zig:3
zig
pub const parse = @import("parse.zig");Audit
| Definitions | 3 |
|---|---|
| Public names | 3 |
| Members | 0 |
| Version | 26.7.0 |
| Revision | daab053ee433 |