Compare commits

...

3 Commits

Author SHA1 Message Date
45d5bc4d31 add new data type, Stage 2025-05-05 11:44:49 +09:00
23aab8414b add new command impl, Sequence 2025-05-05 11:44:13 +09:00
e30773870f separate permanentized data in workspace 2025-05-05 10:51:30 +09:00
8 changed files with 178 additions and 17 deletions

48
src/hncore/Stage.zig Normal file
View File

@ -0,0 +1,48 @@
const std = @import("std");
const Task = @import("./Task.zig");
///
const Self = @This();
///
const TaskList = std.ArrayList(*Task);
///
alloc: std.mem.Allocator,
///
name: [:0]const u8,
///
tasks: TaskList,
///
pub fn init(alloc: std.mem.Allocator, name: []const u8) !Self {
return Self {
.alloc = alloc,
.name = try alloc.dupeZ(u8, name),
.tasks = TaskList.init(alloc),
};
}
///
pub fn deinit(self: *Self) void {
self.tasks.deinit();
self.alloc.free(self.name);
}
///
pub fn rename(self: *Self, name: []const u8) !void {
self.alloc.free(self.name);
self.name = try self.alloc.dupeZ(u8, name);
}
test {
var stage = try init(std.testing.allocator, "helloworld");
defer stage.deinit();
try std.testing.expectEqualStrings("helloworld", stage.name);
try stage.rename("goodbye");
try std.testing.expectEqualStrings("goodbye", stage.name);
}

View File

@ -17,9 +17,6 @@ details: [:0]const u8,
///
done: bool,
///
archived: bool,
///
pub fn init(alloc: std.mem.Allocator, id: usize) !Self {
return Self {
@ -28,7 +25,6 @@ pub fn init(alloc: std.mem.Allocator, id: usize) !Self {
.summary = try alloc.dupeZ(u8, ""),
.details = try alloc.dupeZ(u8, ""),
.done = true,
.archived = true,
};
}
///

View File

@ -2,11 +2,19 @@ const std = @import("std");
const Task = @import("./Task.zig");
///
const Self = @This();
///
const Map = std.AutoHashMap(usize, *Task);
///
alloc: std.mem.Allocator,
///
map: Map,
///
nextId: usize,
///

View File

@ -3,20 +3,36 @@ const std = @import("std");
const CommandHistory = @import("./CommandHistory.zig");
const TaskStore = @import("./TaskStore.zig");
///
const Self = @This();
///
const Perma = struct {
tasks: TaskStore,
};
///
commands: CommandHistory,
///
perma: Perma,
///
pub fn init(allocator: std.mem.Allocator) Self {
return Self {
.commands = CommandHistory.init(allocator),
.perma = .{
.tasks = TaskStore.init(allocator),
},
};
}
///
pub fn deinit(self: *Self) void {
self.tasks.deinit();
self.perma.tasks.deinit();
self.commands.deinit();
}
test {
var ws = init(std.testing.allocator);
defer ws.deinit();

View File

@ -34,6 +34,12 @@ pub const Mock = struct {
target : ?*i32 = null,
destroyed: ?*bool = null,
pub fn init(alloc: std.mem.Allocator, target: ?*i32, destroyed: ?*bool) !Self {
return make(alloc, Mock {
.target = target,
.destroyed = destroyed,
});
}
pub fn deinit(self: *@This(), _: anytype) void {
if (self.destroyed) |ptr| {
ptr.* = true;
@ -117,10 +123,7 @@ test {
var value: i32 = 0;
var destroyed = false;
{
const cmd = try Self.make(std.testing.allocator, Mock {
.target = &value,
.destroyed = &destroyed,
});
const cmd = try Mock.init(std.testing.allocator, &value, &destroyed);
defer cmd.deinit();
try cmd.apply();

View File

@ -0,0 +1,88 @@
const std = @import("std");
const Interface = @import("./Interface.zig");
///
const Sequence = struct {
///
const Self = @This();
///
commands: []Interface,
///
pub fn init(alloc: std.mem.Allocator, commands: []const Interface) !Interface {
const list = try alloc.dupe(Interface, commands);
errdefer alloc.free(list);
return try Interface.make(alloc, Self {
.commands = list,
});
}
///
pub fn deinit(self: *Self, alloc: std.mem.Allocator) void {
for (self.commands) |command| {
command.deinit();
}
alloc.free(self.commands);
}
///
pub fn apply(self: *Self, _: std.mem.Allocator) Interface.Error!void {
for (self.commands) |command| {
try command.apply();
}
}
///
pub fn revert(self: *Self, _: std.mem.Allocator) Interface.Error!void {
var i = self.commands.len;
while (i > 0) {
i -= 1;
try self.commands[i].revert();
}
}
};
test {
var value1: i32 = 0;
var value2: i32 = 0;
var value3: i32 = 0;
var finished1 = false;
var finished2 = false;
var finished3 = false;
{
var seq = blk: {
const alloc = std.testing.allocator;
var cmd1 = try Interface.Mock.init(alloc, &value1, &finished1);
errdefer cmd1.deinit();
var cmd2 = try Interface.Mock.init(alloc, &value2, &finished2);
errdefer cmd2.deinit();
var cmd3 = try Interface.Mock.init(alloc, &value3, &finished3);
errdefer cmd3.deinit();
break :blk try Sequence.init(std.testing.allocator, &[_]Interface {
cmd1, cmd2, cmd3,
});
};
defer seq.deinit();
try seq.apply();
try std.testing.expectEqual(1, value1);
try std.testing.expectEqual(1, value2);
try std.testing.expectEqual(1, value3);
try seq.revert();
try std.testing.expectEqual(0, value1);
try std.testing.expectEqual(0, value2);
try std.testing.expectEqual(0, value3);
}
try std.testing.expect(finished1);
try std.testing.expect(finished2);
try std.testing.expect(finished3);
}

View File

@ -1,5 +1,6 @@
pub const Interface = @import("./Interface.zig");
pub const generic = @import("./generic.zig");
pub const task = @import("./task.zig");
test {

View File

@ -1,6 +1,7 @@
pub const command = @import("./command/root.zig");
pub const CommandHistory = @import("./CommandHistory.zig");
pub const Stage = @import("./Stage.zig");
pub const Task = @import("./Task.zig");
pub const TaskStore = @import("./TaskStore.zig");
pub const Workspace = @import("./Workspace.zig");