-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
-
30
-
31
-
32
-
33
-
34
-
35
-
36
-
37
-
38
-
39
-
40
-
41
-
42
-
43
-
44
-
45
-
46
-
47
-
48
-
49
-
50
-
51
-
52
-
53
-
54
-
55
-
56
-
57
-
58
-
59
-
60
-
61
-
62
-
63
-
64
-
65
-
66
-
67
-
68
-
69
-
70
-
71
-
72
-
73
-
74
-
75
-
76
-
77
-
78
-
79
-
80
-
81
-
82
-
83
-
84
-
85
-
86
-
87
-
88
-
89
-
90
-
91
-
92
-
93
-
94
-
95
-
96
-
97
-
98
-
99
-
100
-
101
-
102
-
103
-
104
-
105
-
106
-
107
-
108
-
109
-
110
-
111
-
112
-
113
-
114
-
115
-
116
-
117
-
118
-
119
-
120
-
121
-
122
-
123
-
124
-
125
-
126
-
127
-
128
-
129
-
130
-
131
-
132
-
133
-
134
-
135
-
136
-
137
-
138
-
139
-
140
-
141
-
142
-
143
-
144
-
145
-
146
-
147
-
148
-
149
-
150
-
151
-
152
-
153
-
154
-
155
-
156
-
157
-
158
-
159
-
160
-
161
-
162
-
163
-
164
-
165
-
166
-
167
-
168
-
169
-
170
-
171
-
172
-
173
-
174
-
175
-
176
-
177
-
178
-
179
-
180
-
181
-
182
-
183
-
184
-
185
-
186
-
187
-
188
-
189
-
190
-
191
-
192
-
193
-
194
-
195
-
196
-
197
-
198
-
199
-
200
-
201
-
202
-
203
-
204
-
205
-
206
-
207
-
208
-
209
-
210
-
211
-
212
-
213
-
214
-
215
-
216
-
217
-
218
-
219
-
220
-
221
-
222
-
223
-
224
-
225
-
226
-
227
-
228
-
229
-
230
-
231
-
232
-
233
-
234
-
235
-
236
-
237
-
238
-
239
-
240
-
241
-
242
-
243
-
244
-
245
-
246
-
247
-
248
-
249
-
250
-
251
-
252
-
253
-
254
-
255
-
256
-
257
-
258
-
259
-
260
-
261
-
262
-
263
-
264
-
265
-
266
-
267
-
268
-
269
-
270
-
271
-
272
-
273
-
274
-
275
-
276
-
277
-
278
-
279
-
280
-
281
-
282
-
283
-
284
-
285
-
286
-
287
-
288
-
289
-
290
-
291
-
292
-
293
-
294
-
295
-
296
-
297
-
298
-
299
-
300
-
301
-
302
-
303
-
304
-
305
-
306
-
307
-
308
-
309
-
310
-
311
-
312
-
313
-
314
-
315
-
316
-
317
-
318
-
319
-
320
-
321
-
322
-
323
-
324
-
325
-
326
-
327
-
328
-
329
-
330
-
331
-
332
-
333
-
334
-
335
-
336
-
337
-
338
-
339
-
340
-
341
-
342
-
343
-
344
-
345
-
346
-
347
-
348
-
349
-
350
-
351
-
352
-
353
-
354
-
355
-
356
-
357
-
358
-
359
-
360
-
361
-
362
-
363
-
364
-
365
-
366
-
367
-
368
-
369
-
370
-
371
-
372
-
373
-
374
-
375
-
376
-
377
-
378
-
379
-
380
-
381
-
382
-
383
-
384
-
385
-
386
-
387
-
388
-
389
-
390
-
391
-
392
-
393
-
394
-
395
-
396
-
397
-
398
-
399
-
400
-
401
-
402
-
403
-
404
-
405
-
406
-
407
-
408
-
409
-
410
-
411
-
412
-
413
-
414
-
415
-
416
-
417
-
418
-
419
-
420
-
421
-
422
-
423
-
424
-
425
-
426
-
427
-
428
-
429
-
430
-
431
-
432
-
433
-
434
-
435
-
436
-
437
-
438
-
439
-
440
-
441
-
442
-
443
-
444
-
445
-
446
-
447
-
448
-
449
-
450
-
451
-
452
-
453
-
454
-
455
-
456
-
457
-
458
-
459
-
460
-
461
-
462
-
463
-
464
-
465
-
466
-
467
-
468
-
469
-
470
-
471
-
472
-
473
-
474
-
475
-
476
-
477
-
478
-
479
-
480
-
481
-
482
-
483
-
484
-
485
-
486
-
487
-
488
-
489
-
490
-
491
-
492
-
493
-
494
-
495
-
496
-
497
-
498
-
499
-
500
-
501
-
502
-
503
-
504
-
505
-
506
-
507
-
508
-
509
-
510
-
511
-
512
-
513
-
514
-
515
-
516
-
517
-
518
-
519
-
520
-
521
-
522
-
523
-
524
-
525
-
526
-
527
-
528
-
529
-
530
-
531
-
532
-
533
-
534
-
535
-
536
-
537
-
538
-
539
-
540
-
541
-
542
-
543
-
544
-
545
-
546
-
547
-
548
-
549
-
550
-
551
-
552
-
553
-
554
-
555
-
556
-
557
-
558
-
559
-
560
-
561
-
562
-
563
-
564
-
565
-
566
-
567
-
568
-
569
-
570
-
571
-
572
-
573
-
574
-
575
-
576
-
577
-
578
-
579
-
580
-
581
-
582
-
583
-
584
-
585
-
586
-
587
-
588
-
589
-
590
-
591
-
592
-
593
-
594
-
595
-
596
-
597
-
598
-
599
-
600
-
601
-
602
-
603
-
604
-
605
-
606
-
607
-
608
-
609
-
610
-
611
-
612
-
613
-
614
-
615
-
616
-
617
-
618
-
619
-
620
-
621
-
622
-
623
-
624
-
625
-
626
-
627
-
628
-
629
-
630
-
631
-
632
-
633
-
634
-
635
-
636
-
637
-
638
-
639
-
640
-
641
-
642
-
643
-
644
-
645
-
646
-
647
-
648
-
649
-
650
-
651
-
652
-
653
-
654
-
655
-
656
-
657
-
658
-
659
-
660
-
661
-
662
-
663
-
664
-
665
-
666
-
667
-
668
-
669
-
670
-
671
-
672
-
673
-
674
-
675
-
676
-
677
-
678
-
679
-
680
-
681
-
682
-
683
-
684
-
685
-
686
-
687
-
688
-
689
-
690
-
691
-
692
-
693
-
694
-
695
-
696
-
697
-
698
-
699
-
700
-
701
-
702
-
703
-
704
-
705
-
706
-
707
-
708
-
709
-
710
-
711
-
712
-
713
-
714
-
715
-
716
-
717
-
718
-
719
-
720
-
721
-
722
-
723
-
724
-
725
-
726
-
727
-
728
-
729
-
730
-
731
-
732
-
733
-
734
-
735
-
736
-
737
-
738
-
739
-
740
-
741
// Copyright 2026 Shota FUJI
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//
// SPDX-License-Identifier: MPL-2.0
const std = @import("std");
const uuid = @import("uuid");
const events = @import("./events.zig");
const libc = @cImport(@cInclude("time.h"));
pub const RunningTimer = struct {
id: uuid.Uuid,
title: []const u8,
started_at: i64,
pub fn deinit(self: RunningTimer, allocator: std.mem.Allocator) void {
allocator.free(self.title);
}
};
pub const StoppedTimer = struct {
id: uuid.Uuid,
title: []const u8,
started_at: i64,
stopped_at: i64,
pub fn deinit(self: StoppedTimer, allocator: std.mem.Allocator) void {
allocator.free(self.title);
}
};
pub const PrintColumn = enum {
id,
title,
status,
started,
stopped,
const size = @typeInfo(PrintColumn).@"enum".fields.len;
pub const Order = struct {
len: usize,
columns: [size]PrintColumn,
pub const ParseError = error{
UnknownColumn,
TooManyColumns,
DuplicatedColumns,
};
pub fn parse(str: []const u8) ParseError!Order {
var len: usize = 0;
var cols: [size]PrintColumn = undefined;
var iter = std.mem.splitScalar(u8, str, ',');
token_loop: while (iter.next()) |token| {
if (len >= size) {
return ParseError.TooManyColumns;
}
inline for (@typeInfo(PrintColumn).@"enum".fields) |field| {
if (std.mem.eql(u8, field.name, token)) {
const e: PrintColumn = @enumFromInt(field.value);
for (cols[0..len]) |c| if (c == e) {
return ParseError.DuplicatedColumns;
};
cols[len] = e;
len += 1;
continue :token_loop;
}
}
return ParseError.UnknownColumn;
}
return .{ .len = len, .columns = cols };
}
test parse {
{
const actual = try parse("id");
const expected: []const PrintColumn = &.{.id};
try std.testing.expectEqualSlices(PrintColumn, expected, actual.toSlice());
}
{
const actual = try parse("status,started,stopped,title");
const expected: []const PrintColumn = &.{ .status, .started, .stopped, .title };
try std.testing.expectEqualSlices(PrintColumn, expected, actual.toSlice());
}
{
const actual = parse("version,status,started,stopped,title");
try std.testing.expectError(ParseError.UnknownColumn, actual);
}
{
const actual = parse("status, started, stopped, title");
try std.testing.expectError(ParseError.UnknownColumn, actual);
}
{
const actual = parse("id,status,status");
try std.testing.expectError(ParseError.DuplicatedColumns, actual);
}
{
const actual = parse("id,status,title,started,stopped,id");
try std.testing.expectError(ParseError.TooManyColumns, actual);
}
}
pub fn fixed(comptime columns: []const PrintColumn) Order {
comptime {
if (columns.len > size) {
@compileError("\"columns\" length cannot exceed the number of PrintColumn fields");
}
}
var cols: [size]PrintColumn = undefined;
for (columns, 0..) |c, i| {
cols[i] = c;
}
return .{
.len = columns.len,
.columns = cols,
};
}
pub fn toSlice(self: *const Order) []const PrintColumn {
return self.columns[0..self.len];
}
test fixed {
const t0 = fixed(&.{ .id, .started, .status, .title });
try std.testing.expectEqualSlices(PrintColumn, &.{ .id, .started, .status, .title }, t0.toSlice());
const t1 = fixed(&.{.stopped});
try std.testing.expectEqualSlices(PrintColumn, &.{.stopped}, t1.toSlice());
}
};
};
test {
_ = PrintColumn.Order;
}
pub const PrintFormat = enum {
tsv,
jsonl,
pub const ParseError = error{
UnknownFormat,
};
pub fn parse(str: []const u8) ParseError!PrintFormat {
inline for (@typeInfo(PrintFormat).@"enum".fields) |field| {
if (std.mem.eql(u8, str, field.name)) {
return @enumFromInt(field.value);
}
}
return ParseError.UnknownFormat;
}
};
pub const DateTimeFormat = enum {
/// Format using the current locale and timezone.
system,
/// Unix seconds.
unix,
pub const ParseError = error{
UnknownFormat,
};
pub fn parse(str: []const u8) ParseError!DateTimeFormat {
inline for (@typeInfo(DateTimeFormat).@"enum".fields) |field| {
if (std.mem.eql(u8, str, field.name)) {
return @enumFromInt(field.value);
}
}
return ParseError.UnknownFormat;
}
pub fn print(self: DateTimeFormat, writer: *std.Io.Writer, unix_sec: i64) std.Io.Writer.Error!void {
switch (self) {
.unix => try writer.printInt(unix_sec, 10, .lower, .{}),
.system => {
libc.tzset();
var tm: libc.tm = undefined;
_ = libc.localtime_r(&unix_sec, &tm);
// Reasonable size, I guess.
var buf: [256:0]u8 = undefined;
@memset(&buf, 0);
const wrote = libc.strftime(&buf, buf.len, "%c", &tm);
try writer.writeAll(buf[0..wrote]);
},
}
}
};
test {
_ = DateTimeFormat;
}
pub const State = enum {
running,
stopped,
};
pub const Timer = union(State) {
running: RunningTimer,
stopped: StoppedTimer,
pub fn deinit(self: *const Timer, allocator: std.mem.Allocator) void {
switch (self.*) {
.running => |t| t.deinit(allocator),
.stopped => |t| t.deinit(allocator),
}
}
pub const PrintOptions = struct {
format: PrintFormat,
order: PrintColumn.Order,
datetime: DateTimeFormat,
};
pub fn print(self: *const Timer, writer: *std.Io.Writer, opts: PrintOptions) std.Io.Writer.Error!void {
switch (opts.format) {
.tsv => {
for (opts.order.toSlice(), 0..) |column, i| {
if (i > 0) {
try writer.writeInt(u8, '\t', .native);
}
switch (column) {
.id => try writer.writeAll(&uuid.urn.serialize(switch (self.*) {
.running => |t| t.id,
.stopped => |t| t.id,
})),
// User can't input title containing newline or tab character via GUI.
// This is fine.
.title => try writer.writeAll(switch (self.*) {
.running => |t| t.title,
.stopped => |t| t.title,
}),
.status => try writer.writeAll(@tagName(self.*)),
.started => try opts.datetime.print(writer, switch (self.*) {
.running => |t| t.started_at,
.stopped => |t| t.started_at,
}),
.stopped => switch (self.*) {
.running => {},
.stopped => |t| try opts.datetime.print(writer, t.stopped_at),
},
}
}
},
.jsonl => {
var stringify: std.json.Stringify = .{
.writer = writer,
.options = .{ .emit_null_optional_fields = false, .whitespace = .minified },
};
try stringify.beginObject();
for (opts.order.toSlice()) |column| {
switch (column) {
.id => {
try stringify.objectField("id");
try stringify.write(uuid.urn.serialize(switch (self.*) {
.running => |t| t.id,
.stopped => |t| t.id,
}));
},
.title => {
try stringify.objectField("title");
try stringify.write(switch (self.*) {
.running => |t| t.title,
.stopped => |t| t.title,
});
},
.status => {
try stringify.objectField("status");
try stringify.write(@tagName(self.*));
},
.started => {
const started_at = switch (self.*) {
.running => |t| t.started_at,
.stopped => |t| t.started_at,
};
try stringify.objectField("started");
switch (opts.datetime) {
.unix => try stringify.write(started_at),
.system => {
try stringify.beginWriteRaw();
try stringify.writer.writeInt(u8, '"', .native);
try opts.datetime.print(writer, started_at);
try stringify.writer.writeInt(u8, '"', .native);
stringify.endWriteRaw();
},
}
},
.stopped => switch (self.*) {
.stopped => |t| {
try stringify.objectField("stopped");
switch (opts.datetime) {
.unix => try stringify.write(t.stopped_at),
.system => {
try stringify.beginWriteRaw();
try stringify.writer.writeInt(u8, '"', .native);
try opts.datetime.print(writer, t.stopped_at);
try stringify.writer.writeInt(u8, '"', .native);
stringify.endWriteRaw();
},
}
},
.running => {},
},
}
}
try stringify.endObject();
},
}
try writer.writeInt(u8, '\n', .native);
}
test "TSV single" {
var alloc = std.Io.Writer.Allocating.init(std.testing.allocator);
defer alloc.deinit();
try print(&.{
.running = .{
.id = 0xa84841db47f8ac9b2043d152f7089191,
.title = "Foo",
.started_at = 1,
},
}, &alloc.writer, .{
.datetime = .unix,
.format = .tsv,
.order = PrintColumn.Order.fixed(&.{.id}),
});
try std.testing.expectEqualSlices(
u8,
"919108f7-52d1-4320-9bac-f847db4148a8\n",
alloc.written(),
);
}
test "TSV running full" {
var alloc = std.Io.Writer.Allocating.init(std.testing.allocator);
defer alloc.deinit();
try print(&.{
.running = .{
.id = 0xa84841db47f8ac9b2043d152f7089191,
.title = "Foo",
.started_at = 1,
},
}, &alloc.writer, .{
.datetime = .unix,
.format = .tsv,
.order = PrintColumn.Order.fixed(&.{ .status, .title, .id, .started, .stopped }),
});
try std.testing.expectEqualSlices(
u8,
"running\tFoo\t919108f7-52d1-4320-9bac-f847db4148a8\t1\t\n",
alloc.written(),
);
}
test "TSV stopped" {
var alloc = std.Io.Writer.Allocating.init(std.testing.allocator);
defer alloc.deinit();
try print(&.{
.stopped = .{
.id = 0xa84841db47f8ac9b2043d152f7089191,
.title = "Foo",
.started_at = 1,
.stopped_at = 2,
},
}, &alloc.writer, .{
.datetime = .unix,
.format = .tsv,
.order = PrintColumn.Order.fixed(&.{ .status, .title, .id, .started, .stopped }),
});
try std.testing.expectEqualSlices(
u8,
"stopped\tFoo\t919108f7-52d1-4320-9bac-f847db4148a8\t1\t2\n",
alloc.written(),
);
}
test "JSONL single" {
var alloc = std.Io.Writer.Allocating.init(std.testing.allocator);
defer alloc.deinit();
try print(&.{
.running = .{
.id = 0xa84841db47f8ac9b2043d152f7089191,
.title = "Foo",
.started_at = 1,
},
}, &alloc.writer, .{
.datetime = .unix,
.format = .jsonl,
.order = PrintColumn.Order.fixed(&.{.id}),
});
try std.testing.expectEqualSlices(
u8,
\\{"id":"919108f7-52d1-4320-9bac-f847db4148a8"}
\\
,
alloc.written(),
);
}
test "JSONL running full" {
var alloc = std.Io.Writer.Allocating.init(std.testing.allocator);
defer alloc.deinit();
try print(&.{
.running = .{
.id = 0xa84841db47f8ac9b2043d152f7089191,
.title = "Foo",
.started_at = 1,
},
}, &alloc.writer, .{
.datetime = .unix,
.format = .jsonl,
.order = PrintColumn.Order.fixed(&.{ .status, .title, .id, .started, .stopped }),
});
try std.testing.expectEqualSlices(
u8,
\\{"status":"running","title":"Foo","id":"919108f7-52d1-4320-9bac-f847db4148a8","started":1}
\\
,
alloc.written(),
);
}
test "JSONL stopped" {
var alloc = std.Io.Writer.Allocating.init(std.testing.allocator);
defer alloc.deinit();
try print(&.{
.stopped = .{
.id = 0xa84841db47f8ac9b2043d152f7089191,
.title = "Foo",
.started_at = 1,
.stopped_at = 2,
},
}, &alloc.writer, .{
.datetime = .unix,
.format = .jsonl,
.order = PrintColumn.Order.fixed(&.{ .status, .title, .id, .started, .stopped }),
});
try std.testing.expectEqualSlices(
u8,
\\{"status":"stopped","title":"Foo","id":"919108f7-52d1-4320-9bac-f847db4148a8","started":1,"stopped":2}
\\
,
alloc.written(),
);
}
};
test {
_ = Timer;
}
pub const Aggregation = struct {
pub const RunningOnly = struct {
running: []RunningTimer,
pub fn deinit(self: RunningOnly, allocator: std.mem.Allocator) void {
for (self.running) |t| t.deinit(allocator);
allocator.free(self.running);
}
};
pub const Full = struct {
running: []RunningTimer,
stopped: []StoppedTimer,
pub fn deinit(self: Full, allocator: std.mem.Allocator) void {
for (self.running) |t| t.deinit(allocator);
allocator.free(self.running);
for (self.stopped) |t| t.deinit(allocator);
allocator.free(self.stopped);
}
};
};
pub const AggregateError = std.mem.Allocator.Error || events.Iterator.IterateError;
/// Caller is responsible for calling `deinit` on the returned struct after use.
pub fn aggregate(Type: type, gpa: std.mem.Allocator, io: std.Io, iterator: *events.Iterator) AggregateError!Type {
if (Type != Aggregation.RunningOnly and Type != Aggregation.Full) {
@compileError("Type parameter of \"aggregate\" must be either one of \"State.RunningOnly\" or \"State.Full\"");
}
var event_list = std.ArrayList(events.Event).empty;
defer event_list.deinit(gpa);
defer for (event_list.items) |event| event.deinit(gpa);
while (try iterator.next(gpa, io)) |event| {
try event_list.append(gpa, event);
}
var running_timers = std.array_hash_map.Auto(uuid.Uuid, RunningTimer).empty;
defer running_timers.deinit(gpa);
errdefer for (running_timers.values()) |timer| timer.deinit(gpa);
var stopped_timers = if (Type == Aggregation.Full) std.array_hash_map.Auto(uuid.Uuid, StoppedTimer).empty else std.array_hash_map.Auto(void, void).empty;
defer if (Type == Aggregation.Full) stopped_timers.deinit(gpa);
errdefer if (Type == Aggregation.Full) for (stopped_timers.values()) |timer| timer.deinit(gpa);
for (0..event_list.items.len) |i| {
const event = event_list.items[event_list.items.len - i - 1];
switch (event) {
.timer_started => |started| {
try running_timers.put(gpa, started.timer_id, .{
.id = started.timer_id,
.title = try gpa.dupe(u8, started.title),
.started_at = started.created_at,
});
},
.timer_stopped => |stopped| {
const found = running_timers.fetchOrderedRemove(stopped.timer_id) orelse {
std.log.warn("Detected stopping non-existing timer ID ({s}), skipping", .{uuid.urn.serialize(stopped.timer_id)});
continue;
};
if (Type == Aggregation.Full) {
try stopped_timers.put(gpa, stopped.timer_id, .{
.id = found.value.id,
.title = found.value.title,
.started_at = found.value.started_at,
.stopped_at = stopped.created_at,
});
} else {
found.value.deinit(gpa);
}
},
.timer_deleted => |deleted| {
if (running_timers.fetchOrderedRemove(deleted.timer_id)) |timer| {
timer.value.deinit(gpa);
}
if (Type == Aggregation.Full) {
if (stopped_timers.fetchOrderedRemove(deleted.timer_id)) |timer| {
timer.value.deinit(gpa);
}
}
},
.timer_title_changed => |title_changed| {
if (running_timers.getPtr(title_changed.timer_id)) |timer| {
gpa.free(timer.title);
timer.title = try gpa.dupe(u8, title_changed.new_title);
}
if (Type == Aggregation.Full) {
if (stopped_timers.getPtr(title_changed.timer_id)) |timer| {
gpa.free(timer.title);
timer.title = try gpa.dupe(u8, title_changed.new_title);
}
}
},
}
}
if (Type == Aggregation.RunningOnly) {
return .{
.running = try gpa.dupe(RunningTimer, running_timers.values()),
};
} else {
const running = try gpa.dupe(RunningTimer, running_timers.values());
errdefer gpa.free(running);
const stopped = try gpa.dupe(StoppedTimer, stopped_timers.values());
errdefer gpa.free(stopped);
return .{
.running = running,
.stopped = stopped,
};
}
}
pub const FindError = std.mem.Allocator.Error || events.Iterator.IterateError;
/// Caller is responsible for calling `deinit` on the returned struct after use.
pub fn findById(gpa: std.mem.Allocator, io: std.Io, iterator: *events.Iterator, id: uuid.Uuid) FindError!?Timer {
var stopped_at: ?i64 = null;
var latest_title: ?[]const u8 = null;
errdefer if (latest_title) |str| gpa.free(str);
while (try iterator.next(gpa, io)) |event| {
defer event.deinit(gpa);
switch (event) {
.timer_deleted => |deleted| {
if (deleted.timer_id == id) {
return null;
}
},
.timer_started => |started| {
if (started.timer_id == id) {
const title = latest_title orelse try gpa.dupe(u8, started.title);
if (stopped_at) |s| {
return .{
.stopped = .{
.id = id,
.title = title,
.started_at = started.created_at,
.stopped_at = s,
},
};
} else {
return .{
.running = .{
.id = id,
.title = title,
.started_at = started.created_at,
},
};
}
}
},
.timer_stopped => |stopped| {
if (stopped.timer_id == id) {
stopped_at = stopped.created_at;
}
},
.timer_title_changed => |changed| {
if (changed.timer_id == id and latest_title == null) {
latest_title = try gpa.dupe(u8, changed.new_title);
}
},
}
}
return null;
}
pub const TimerIterator = struct {
events_iter: *events.Iterator,
title_changes: std.hash_map.AutoHashMapUnmanaged(uuid.Uuid, []const u8) = .empty,
stopped_timestamps: std.hash_map.AutoHashMapUnmanaged(uuid.Uuid, i64) = .empty,
deleted_timer_ids: std.hash_map.AutoHashMapUnmanaged(uuid.Uuid, void) = .empty,
pub fn deinit(self: *TimerIterator, gpa: std.mem.Allocator) void {
var changes_iter = self.title_changes.iterator();
while (changes_iter.next()) |entry| {
gpa.free(entry.value_ptr.*);
}
self.title_changes.deinit(gpa);
self.stopped_timestamps.deinit(gpa);
self.deleted_timer_ids.deinit(gpa);
}
pub const Error = events.Iterator.IterateError || std.mem.Allocator.Error;
/// Caller is responsible for calling `deinit` on the returned struct after use.
pub fn next(self: *TimerIterator, gpa: std.mem.Allocator, io: std.Io) Error!?Timer {
while (try self.events_iter.next(gpa, io)) |event| {
defer event.deinit(gpa);
switch (event) {
.timer_deleted => |deleted| {
try self.deleted_timer_ids.put(gpa, deleted.timer_id, {});
},
.timer_stopped => |stopped| if (!self.deleted_timer_ids.contains(stopped.timer_id)) {
try self.stopped_timestamps.put(gpa, stopped.timer_id, stopped.created_at);
},
.timer_title_changed => |changed| if (!self.deleted_timer_ids.contains(changed.timer_id)) {
const new_title = try gpa.dupe(u8, changed.new_title);
errdefer gpa.free(new_title);
if (try self.title_changes.fetchPut(gpa, changed.timer_id, new_title)) |prev| {
gpa.free(prev.value);
}
},
.timer_started => |started| if (!self.deleted_timer_ids.contains(started.timer_id)) {
const stopped_at = if (self.stopped_timestamps.fetchRemove(started.timer_id)) |entry| entry.value else null;
const title = if (self.title_changes.fetchRemove(started.timer_id)) |entry| entry.value else try gpa.dupe(u8, started.title);
if (stopped_at) |ts| {
return .{
.stopped = .{
.id = started.timer_id,
.title = title,
.started_at = started.created_at,
.stopped_at = ts,
},
};
} else {
return .{
.running = .{
.id = started.timer_id,
.title = title,
.started_at = started.created_at,
},
};
}
},
}
}
return null;
}
};