Lightpanda: the headless browser designed for AI and automation
`Updater`: continue on implementing `Updater.inform`
Halil Durak6acb58eff4d7
diff --git a/src/Updater.zig b/src/Updater.zigindex cc6e11be7..cba547d20 100644--- a/src/Updater.zig+++ b/src/Updater.zig@@ -111,9 +111,43 @@ fn resetConnection(self: *Updater) !void {self.conn_err = .none;}+const SortContext = struct {+ object: std.json.ObjectMap,++ pub fn lessThan(ctx: SortContext, a_index: usize, b_index: usize) bool {+ const keys = ctx.object.keys();+ const a_version = std.SemanticVersion.parse(keys[a_index]) catch unreachable;+ const b_version = std.SemanticVersion.parse(keys[b_index]) catch unreachable;+ return a_version.order(b_version).compare(.gt);+ }+};+/// Informs about running version to given `Writer` by desired `Channel`.-pub fn inform(self: *Updater, channel: Channel, writer: std.Io.Writer) void {+pub fn inform(self: *Updater, channel: Channel, writer: *std.Io.Writer) !void {const versions = try self.getVersions();+ defer self.resetConnection() catch {};++ switch (channel) {+ .stable => {+ var stable = versions.object;+ // Get rid of `nightly`.+ lp.assert(stable.swapRemove("nightly"), "Updater.inform: incorrect JSON", .{});++ // Sort and retrieve the newest.+ stable.sort(SortContext{ .object = stable });+ // Newest is at the beginning.+ const newest = stable.values()[0].object;+ const version = (newest.get("version") orelse return error.IncorrectJson).string;+ try writer.print("Latest stable Lightpanda version is {s}.\n", .{version});+ return writer.flush();+ },+ .nightly => {+ const nightly = (versions.object.get("nightly") orelse return error.IncorrectJson).object;+ const version = (nightly.get("version") orelse return error.IncorrectJson).string;+ try writer.print("Latest nightly Lightpanda version is {s}.\n", .{version});+ return writer.flush();+ },+ }}/// Invoked by `Connection` when there are body bytes.diff --git a/src/lightpanda.zig b/src/lightpanda.zigindex 21840e2f4..a6e3951bf 100644--- a/src/lightpanda.zig+++ b/src/lightpanda.zig@@ -227,12 +227,15 @@ fn dumpContent(app: *App, mode: Config.DumpFormat, dump_opts: dump.Opts, frame:/// Check `Config.zig` for `options`.pub fn update(allocator: std.mem.Allocator, config: *const Config, options: anytype) !void {- if (options.channel == .nightly) @panic("TODO");-var client = try Updater.init(allocator, config);defer client.deinit();- try client.getVersions();+ var stdout = std.fs.File.stdout();+ var buf: [4096]u8 = undefined;+ var writer = stdout.writer(&buf);+ const w = &writer.interface;+ // Inform to stdout about version.+ try client.inform(options.channel, w);}// Writes a single page's result object. Framing (the enclosing array and any