tiny.zen.DiagramDocument
Defined in diagram.spec.
API (9)
Actions
Public operations.
Fields and members
Public fields and members.
Source
Source: lib/zen/src/diagram/spec.zig:310
zig
pub const Document = struct { allocator: std.mem.Allocator, frame: Frame = .{}, scales: Scales = .{}, transforms: std.ArrayList(Transform) = .empty, data: std.ArrayList(DataRow) = .empty, marks: std.ArrayList(Mark) = .empty, scratch_high_water: usize = 0, pub fn parse(allocator: std.mem.Allocator, line_scratch: []u8, jsonl: []const u8) !Document { var document = Document{ .allocator = allocator }; errdefer document.deinit(); var scratch = std.heap.FixedBufferAllocator.init(line_scratch); var lines = LineIterator.init(jsonl); while (lines.next()) |line| { scratch.reset(); var parsed = std.json.parseFromSlice(std.json.Value, scratch.allocator(), line, .{}) catch |err| switch (err) { error.OutOfMemory => return error.OutOfMemory, else => return error.InvalidJsonl, }; defer parsed.deinit(); try document.applyRecord(parsed.value); document.scratch_high_water = @max(document.scratch_high_water, scratch.end_index); } try validateDocument(&document); return document; } pub fn deinit(self: *Document) void { self.frame.deinit(self.allocator); self.scales.deinit(self.allocator); for (self.transforms.items) |*transform| transform.deinit(self.allocator); self.transforms.deinit(self.allocator); for (self.data.items) |*row| row.deinit(self.allocator); self.data.deinit(self.allocator); for (self.marks.items) |*mark| mark.deinit(self.allocator); self.marks.deinit(self.allocator); } fn applyRecord(self: *Document, value: std.json.Value) !void { const object = switch (value) { .object => |object| object, else => return error.InvalidRecord, }; switch (try recordKindOf(value)) { .frame => try self.applyFrame(object), .scale => try self.applyScale(object), .transform => try self.applyTransform(object), .data => try self.applyData(object), .mark => try self.applyChannelMark(object), .bar => try self.marks.append(self.allocator, .{ .bar = try parseBar(self.allocator, object) }), .point => try self.marks.append(self.allocator, .{ .point = try parsePoint(self.allocator, object) }), .rule => try self.marks.append(self.allocator, .{ .rule = try parseRule(self.allocator, object) }), .text => try self.marks.append(self.allocator, .{ .text = try parseText(self.allocator, object) }), .box => try self.marks.append(self.allocator, .{ .box = try parseBox(self.allocator, object) }), .edge => try self.marks.append(self.allocator, .{ .edge = try parseEdge(self.allocator, object) }), } } fn applyFrame(self: *Document, object: std.json.ObjectMap) !void { if (objectNumber(object, "width")) |value| self.frame.width = value; if (objectNumber(object, "height")) |value| self.frame.height = value; if (objectNumber(object, "inset")) |value| self.frame.inset = value; if (objectBool(object, "axes")) |value| self.frame.axes = value; if (objectNumber(object, "y_min")) |value| self.frame.y_min = value; if (objectNumber(object, "y_max")) |value| self.frame.y_max = value; if (objectNumber(object, "x_min")) |value| self.frame.x_min = value; if (objectNumber(object, "x_max")) |value| self.frame.x_max = value; try replaceString(self.allocator, &self.frame.title, objectString(object, "title")); try replaceString(self.allocator, &self.frame.x_label, objectString(object, "x_label")); try replaceString(self.allocator, &self.frame.y_label, objectString(object, "y_label")); if (objectString(object, "background")) |background| { if (!validPaint(background)) return error.InvalidColor; try replaceString(self.allocator, &self.frame.background, background); } } fn applyScale(self: *Document, object: std.json.ObjectMap) !void { const axis = try parseAxis(object); const scale = switch (axis) { .x => &self.scales.x, .y => &self.scales.y, }; if (objectString(object, "type")) |value| scale.kind = try parseScaleKind(value); if (objectNumber(object, "min")) |value| scale.min = value; if (objectNumber(object, "max")) |value| scale.max = value; if (objectNumber(object, "base")) |value| scale.base = value; try replaceString(self.allocator, &scale.label, objectString(object, "label")); } fn applyTransform(self: *Document, object: std.json.ObjectMap) !void { var transform = try parseTransform(self.allocator, object); errdefer transform.deinit(self.allocator); try self.transforms.append(self.allocator, transform); } fn applyData(self: *Document, object: std.json.ObjectMap) !void { var row = try parseDataRow(self.allocator, object); errdefer row.deinit(self.allocator); try self.data.append(self.allocator, row); } fn applyChannelMark(self: *Document, object: std.json.ObjectMap) !void { var channel = try parseChannelMark(self.allocator, object); defer channel.deinit(self.allocator); var matched = false; for (self.data.items) |*row| { if (!std.mem.eql(u8, row.name, channel.data)) continue; matched = true; var mark = try markFromChannel(self.allocator, channel, row); var appended = false; errdefer if (!appended) mark.deinit(self.allocator); try self.marks.append(self.allocator, mark); appended = true; } if (!matched) return error.UnknownData; }};Source: lib/zen/src/root.zig:43
zig
/// Owned parsed diagram model; release it through its `deinit` method.pub const DiagramDocument = diagram.Document;Also reachable as
diagram.Document, diagram.document.Document.
Audit
| Definitions | 3 |
|---|---|
| Public names | 12 |
| Members | 7 |
| Version | 26.7.0 |
| Revision | daab053ee433 |