Начало фикса json

This commit is contained in:
2026-03-02 17:19:55 +03:00
parent b692539a30
commit 8ea5d97c2d
12 changed files with 55 additions and 58 deletions

View File

@@ -7,71 +7,69 @@ pub const Object = @import("Object.zig");
const shape = @import("shape/shape.zig");
size: Size_f,
allocator: std.mem.Allocator,
objects: std.ArrayList(Object),
next_object_id: u64,
pub fn init(allocator: std.mem.Allocator, size: Size_f) Document {
pub fn init(size: Size_f) Document {
return .{
.size = size,
.allocator = allocator,
.objects = std.ArrayList(Object).empty,
.next_object_id = 1,
};
}
pub fn deinit(self: *Document) void {
for (self.objects.items) |*obj| obj.deinit(self.allocator);
self.objects.deinit(self.allocator);
pub fn deinit(self: *Document, allocator: std.mem.Allocator) void {
for (self.objects.items) |*obj| obj.deinit(allocator);
self.objects.deinit(allocator);
}
pub fn addObject(self: *Document, template: Object) !void {
const obj = try template.clone(self.allocator, &self.next_object_id);
try self.objects.append(self.allocator, obj);
pub fn addObject(self: *Document, allocator: std.mem.Allocator, template: Object) !void {
const obj = try template.clone(allocator, &self.next_object_id);
try self.objects.append(allocator, obj);
}
/// Добавляет объект в документ: как ребёнка родителя (если id найден), иначе в корень.
pub fn addObjectUnderParentId(self: *Document, parent_id: ?u64, template: Object) !void {
pub fn addObjectUnderParentId(self: *Document, allocator: std.mem.Allocator, parent_id: ?u64, template: Object) !void {
if (parent_id) |id| {
if (self.findObjectById(id)) |parent| {
try parent.addChild(self.allocator, template, &self.next_object_id);
try parent.addChild(allocator, template, &self.next_object_id);
return;
}
}
try self.addObject(template);
try self.addObject(allocator, template);
}
pub fn addShape(self: *Document, parent: ?*Object, shape_kind: Object.ShapeKind) !void {
const obj = try shape.createObject(self.allocator, shape_kind);
pub fn addShape(self: *Document, allocator: std.mem.Allocator, parent: ?*Object, shape_kind: Object.ShapeKind) !void {
const obj = try shape.createObject(allocator, shape_kind);
if (parent) |p| {
try p.addChild(self.allocator, obj, &self.next_object_id);
try p.addChild(allocator, obj, &self.next_object_id);
} else {
try self.addObject(obj);
try self.addObject(allocator, obj);
}
}
/// Удаляет объект из документа (из корня или из детей родителя). Возвращает true, если объект был найден и удалён.
pub fn removeObject(self: *Document, obj: *Object) bool {
pub fn removeObject(self: *Document, allocator: std.mem.Allocator, obj: *Object) bool {
for (self.objects.items, 0..) |*item, i| {
if (item == obj) {
var removed = self.objects.orderedRemove(i);
removed.deinit(self.allocator);
removed.deinit(allocator);
return true;
}
if (removeFromChildren(self.allocator, &item.children, obj)) return true;
if (removeFromChildren(allocator, &item.children, obj)) return true;
}
return false;
}
/// Удаляет объект по id. Возвращает true, если объект был найден и удалён.
pub fn removeObjectById(self: *Document, obj_id: u64) bool {
pub fn removeObjectById(self: *Document, allocator: std.mem.Allocator, obj_id: u64) bool {
for (self.objects.items, 0..) |*item, i| {
if (item.id == obj_id) {
var removed = self.objects.orderedRemove(i);
removed.deinit(self.allocator);
removed.deinit(allocator);
return true;
}
if (removeFromChildrenById(self.allocator, &item.children, obj_id)) return true;
if (removeFromChildrenById(allocator, &item.children, obj_id)) return true;
}
return false;
}