PWA Manifest Generator — Zig source
Assemble a valid Web App Manifest — name, colors, display mode, icon sizes, maskable purpose — with a stable key order and instant JSON you can copy or download.
This is the Zig implementation — the same logic the interactive tool runs, in a shareable, citable form.
// pwa-manifest — Zig port: fixed-key-order manifest.json builder for the
// pwa-manifest-generator tool. Zig (0.15+), standard library only. Port of
// src/lib/pwa-manifest.ts — same logic as this dir's typescript.ts and the
// live Go twin at cli/pwa-manifest-generator/pwa-manifest-generator.go.
// Fixed key order (name, short_name, description, start_url, display,
// orientation, background_color, theme_color, icons); blank optional fields
// are omitted; icon sizes default to [192, 512]; hard errors suppress
// emission; the short_name >12 warning never blocks. The caller owns every
// slice in the result (freed with the same allocator).
const std = @import("std");
/// Mirrors the ManifestInput interface in src/lib/pwa-manifest.ts. A null
/// icon_sizes means "not provided" and falls back to the TS default
/// [192, 512], while an explicit empty slice emits an empty icons array —
/// the same distinction the TS lib draws with `??`.
pub const ManifestInput = struct {
name: []const u8 = "",
short_name: []const u8 = "",
description: []const u8 = "",
start_url: []const u8 = "",
display: []const u8 = "", // "" means absent
orientation: []const u8 = "", // "" means absent
background_color: []const u8 = "",
theme_color: []const u8 = "",
icon_sizes: ?[]const i64 = null,
maskable: bool = false,
};
/// Mirrors the ManifestResult interface in src/lib/pwa-manifest.ts.
pub const ManifestResult = struct {
ok: bool,
errors: []const []const u8,
warnings: []const []const u8,
json: []const u8,
};
/// The default icon edge sizes (TS `input.iconSizes ?? [192, 512]`).
const default_icon_sizes = [_]i64{ 192, 512 };
const ws = " \t\n\r\x0b\x0c";
/// Same contract as TS HEX = /^#[0-9a-fA-F]{6}$/.
fn isHexColor(s: []const u8) bool {
if (s.len != 7 or s[0] != '#') return false;
for (s[1..]) |c| {
const hex = (c >= '0' and c <= '9') or (c >= 'a' and c <= 'f') or (c >= 'A' and c <= 'F');
if (!hex) return false;
}
return true;
}
/// UTF-8 code-point count — the rune count the Go twin uses for the
/// short_name length warning. Falls back to the byte length on invalid UTF-8.
fn runeCount(s: []const u8) usize {
return std.unicode.utf8CountCodepoints(s) catch s.len;
}
/// Double-quote s exactly like JavaScript's JSON.stringify: escape ", \, and
/// the control characters (short escapes for \b \f \n \r \t, \u00xx for the
/// rest), leave everything else — including non-ASCII bytes — untouched.
fn appendJsonString(alloc: std.mem.Allocator, out: *std.ArrayList(u8), s: []const u8) !void {
try out.append(alloc, '"');
for (s) |c| {
switch (c) {
'"' => try out.appendSlice(alloc, "\\\""),
'\\' => try out.appendSlice(alloc, "\\\\"),
0x08 => try out.appendSlice(alloc, "\\b"),
0x0c => try out.appendSlice(alloc, "\\f"),
'\n' => try out.appendSlice(alloc, "\\n"),
'\r' => try out.appendSlice(alloc, "\\r"),
'\t' => try out.appendSlice(alloc, "\\t"),
else => {
if (c < 0x20) {
var buf: [6]u8 = undefined;
_ = std.fmt.bufPrint(&buf, "\\u{x:0>4}", .{c}) catch unreachable;
try out.appendSlice(alloc, &buf);
} else {
try out.append(alloc, c);
}
},
}
}
try out.append(alloc, '"');
}
/// The Zig twin of buildManifest() in src/lib/pwa-manifest.ts. The caller
/// owns every slice in the returned result.
pub fn buildManifest(alloc: std.mem.Allocator, input: ManifestInput) !ManifestResult {
var errors: std.ArrayList([]const u8) = .empty;
var warnings: std.ArrayList([]const u8) = .empty;
const name = std.mem.trim(u8, input.name, ws);
const short_name = std.mem.trim(u8, input.short_name, ws);
const description = std.mem.trim(u8, input.description, ws);
const start_url = std.mem.trim(u8, input.start_url, ws);
const background_color = std.mem.trim(u8, input.background_color, ws);
const theme_color = std.mem.trim(u8, input.theme_color, ws);
const sizes: []const i64 = input.icon_sizes orelse &default_icon_sizes;
if (name.len == 0) try errors.append(alloc, "name is required.");
if (background_color.len != 0 and !isHexColor(background_color))
try errors.append(alloc, "background_color must be a #rrggbb hex value.");
if (theme_color.len != 0 and !isHexColor(theme_color))
try errors.append(alloc, "theme_color must be a #rrggbb hex value.");
var bad_size = false;
for (sizes) |s| {
if (s <= 0) {
bad_size = true;
break;
}
}
if (bad_size) try errors.append(alloc, "icon sizes must be positive integers.");
for (sizes, 0..) |s, i| {
for (sizes[0..i]) |prev| {
if (prev == s) {
try errors.append(alloc, try std.fmt.allocPrint(alloc, "icon size {d} is listed twice.", .{s}));
break;
}
}
}
if (errors.items.len > 0) {
return .{ .ok = false, .errors = try errors.toOwnedSlice(alloc), .warnings = &.{}, .json = "" };
}
const sn_len = runeCount(short_name);
if (sn_len > 12) {
try warnings.append(alloc, try std.fmt.allocPrint(
alloc,
"short_name is {d} characters \u{2014} home-screen labels truncate around 12.",
.{sn_len},
));
}
var lines: std.ArrayList([]const u8) = .empty;
const Line = struct {
fn push(a: std.mem.Allocator, list: *std.ArrayList([]const u8), key: []const u8, value: []const u8) !void {
var line: std.ArrayList(u8) = .empty;
try line.appendSlice(a, " ");
try appendJsonString(a, &line, key);
try line.appendSlice(a, ": ");
try appendJsonString(a, &line, value);
try list.append(a, try line.toOwnedSlice(a));
}
};
try Line.push(alloc, &lines, "name", name);
if (short_name.len != 0) try Line.push(alloc, &lines, "short_name", short_name);
if (description.len != 0) try Line.push(alloc, &lines, "description", description);
if (start_url.len != 0) try Line.push(alloc, &lines, "start_url", start_url);
if (input.display.len != 0) try Line.push(alloc, &lines, "display", input.display);
if (input.orientation.len != 0) try Line.push(alloc, &lines, "orientation", input.orientation);
if (background_color.len != 0) try Line.push(alloc, &lines, "background_color", background_color);
if (theme_color.len != 0) try Line.push(alloc, &lines, "theme_color", theme_color);
const purpose: []const u8 = if (input.maskable) "maskable" else "any";
var icons: std.ArrayList(u8) = .empty;
for (sizes, 0..) |s, i| {
if (i > 0) try icons.appendSlice(alloc, ",\n");
try icons.appendSlice(alloc, " {\n \"src\": ");
try appendJsonString(alloc, &icons, try std.fmt.allocPrint(alloc, "icons/icon-{d}x{d}.png", .{ s, s }));
try icons.appendSlice(alloc, ",\n \"sizes\": ");
try appendJsonString(alloc, &icons, try std.fmt.allocPrint(alloc, "{d}x{d}", .{ s, s }));
try icons.appendSlice(alloc, ",\n \"type\": \"image/png\",\n \"purpose\": ");
try appendJsonString(alloc, &icons, purpose);
try icons.appendSlice(alloc, "\n }");
}
var doc: std.ArrayList(u8) = .empty;
try doc.appendSlice(alloc, "{\n");
for (lines.items) |line| {
try doc.appendSlice(alloc, line);
try doc.appendSlice(alloc, ",\n");
}
if (sizes.len == 0) {
try doc.appendSlice(alloc, " \"icons\": []"); // JSON.stringify renders an empty array inline
} else {
try doc.appendSlice(alloc, " \"icons\": [\n");
try doc.appendSlice(alloc, icons.items);
try doc.appendSlice(alloc, "\n ]");
}
try doc.appendSlice(alloc, "\n}\n");
return .{
.ok = true,
.errors = try errors.toOwnedSlice(alloc),
.warnings = try warnings.toOwnedSlice(alloc),
.json = try doc.toOwnedSlice(alloc),
};
}
Also available in 13 other languages
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →