Compare commits
3 Commits
8263ccad82
...
45d5bc4d31
Author | SHA1 | Date | |
---|---|---|---|
45d5bc4d31 | |||
23aab8414b | |||
e30773870f |
48
src/hncore/Stage.zig
Normal file
48
src/hncore/Stage.zig
Normal 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);
|
||||||
|
}
|
@ -17,9 +17,6 @@ details: [:0]const u8,
|
|||||||
///
|
///
|
||||||
done: bool,
|
done: bool,
|
||||||
|
|
||||||
///
|
|
||||||
archived: bool,
|
|
||||||
|
|
||||||
///
|
///
|
||||||
pub fn init(alloc: std.mem.Allocator, id: usize) !Self {
|
pub fn init(alloc: std.mem.Allocator, id: usize) !Self {
|
||||||
return Self {
|
return Self {
|
||||||
@ -28,7 +25,6 @@ pub fn init(alloc: std.mem.Allocator, id: usize) !Self {
|
|||||||
.summary = try alloc.dupeZ(u8, ""),
|
.summary = try alloc.dupeZ(u8, ""),
|
||||||
.details = try alloc.dupeZ(u8, ""),
|
.details = try alloc.dupeZ(u8, ""),
|
||||||
.done = true,
|
.done = true,
|
||||||
.archived = true,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
///
|
///
|
||||||
|
@ -2,11 +2,19 @@ const std = @import("std");
|
|||||||
|
|
||||||
const Task = @import("./Task.zig");
|
const Task = @import("./Task.zig");
|
||||||
|
|
||||||
|
///
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
|
|
||||||
|
///
|
||||||
const Map = std.AutoHashMap(usize, *Task);
|
const Map = std.AutoHashMap(usize, *Task);
|
||||||
|
|
||||||
|
///
|
||||||
alloc: std.mem.Allocator,
|
alloc: std.mem.Allocator,
|
||||||
|
|
||||||
|
///
|
||||||
map: Map,
|
map: Map,
|
||||||
|
|
||||||
|
///
|
||||||
nextId: usize,
|
nextId: usize,
|
||||||
|
|
||||||
///
|
///
|
||||||
|
@ -3,20 +3,36 @@ const std = @import("std");
|
|||||||
const CommandHistory = @import("./CommandHistory.zig");
|
const CommandHistory = @import("./CommandHistory.zig");
|
||||||
const TaskStore = @import("./TaskStore.zig");
|
const TaskStore = @import("./TaskStore.zig");
|
||||||
|
|
||||||
|
///
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
|
|
||||||
tasks: TaskStore,
|
///
|
||||||
|
const Perma = struct {
|
||||||
|
tasks: TaskStore,
|
||||||
|
};
|
||||||
|
|
||||||
|
///
|
||||||
commands: CommandHistory,
|
commands: CommandHistory,
|
||||||
|
|
||||||
|
///
|
||||||
|
perma: Perma,
|
||||||
|
|
||||||
|
///
|
||||||
pub fn init(allocator: std.mem.Allocator) Self {
|
pub fn init(allocator: std.mem.Allocator) Self {
|
||||||
return Self {
|
return Self {
|
||||||
.commands = CommandHistory.init(allocator),
|
.commands = CommandHistory.init(allocator),
|
||||||
|
.perma = .{
|
||||||
.tasks = TaskStore.init(allocator),
|
.tasks = TaskStore.init(allocator),
|
||||||
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///
|
||||||
pub fn deinit(self: *Self) void {
|
pub fn deinit(self: *Self) void {
|
||||||
self.tasks.deinit();
|
self.perma.tasks.deinit();
|
||||||
|
self.commands.deinit();
|
||||||
}
|
}
|
||||||
|
|
||||||
test {
|
test {
|
||||||
var ws = init(std.testing.allocator);
|
var ws = init(std.testing.allocator);
|
||||||
defer ws.deinit();
|
defer ws.deinit();
|
||||||
|
@ -34,6 +34,12 @@ pub const Mock = struct {
|
|||||||
target : ?*i32 = null,
|
target : ?*i32 = null,
|
||||||
destroyed: ?*bool = 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 {
|
pub fn deinit(self: *@This(), _: anytype) void {
|
||||||
if (self.destroyed) |ptr| {
|
if (self.destroyed) |ptr| {
|
||||||
ptr.* = true;
|
ptr.* = true;
|
||||||
@ -117,10 +123,7 @@ test {
|
|||||||
var value: i32 = 0;
|
var value: i32 = 0;
|
||||||
var destroyed = false;
|
var destroyed = false;
|
||||||
{
|
{
|
||||||
const cmd = try Self.make(std.testing.allocator, Mock {
|
const cmd = try Mock.init(std.testing.allocator, &value, &destroyed);
|
||||||
.target = &value,
|
|
||||||
.destroyed = &destroyed,
|
|
||||||
});
|
|
||||||
defer cmd.deinit();
|
defer cmd.deinit();
|
||||||
|
|
||||||
try cmd.apply();
|
try cmd.apply();
|
||||||
|
88
src/hncore/command/generic.zig
Normal file
88
src/hncore/command/generic.zig
Normal 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);
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
pub const Interface = @import("./Interface.zig");
|
pub const Interface = @import("./Interface.zig");
|
||||||
|
|
||||||
|
pub const generic = @import("./generic.zig");
|
||||||
pub const task = @import("./task.zig");
|
pub const task = @import("./task.zig");
|
||||||
|
|
||||||
test {
|
test {
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
pub const command = @import("./command/root.zig");
|
pub const command = @import("./command/root.zig");
|
||||||
|
|
||||||
pub const CommandHistory = @import("./CommandHistory.zig");
|
pub const CommandHistory = @import("./CommandHistory.zig");
|
||||||
|
pub const Stage = @import("./Stage.zig");
|
||||||
pub const Task = @import("./Task.zig");
|
pub const Task = @import("./Task.zig");
|
||||||
pub const TaskStore = @import("./TaskStore.zig");
|
pub const TaskStore = @import("./TaskStore.zig");
|
||||||
pub const Workspace = @import("./Workspace.zig");
|
pub const Workspace = @import("./Workspace.zig");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user