Something went wrong. Try again.
A Gmail API client library for Zig: labels, messages, and the mailbox history that turns a second sync into an update.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! A Gmail label, which is what Gmail has instead of folders: a message//! carries a set of them rather than living in one place, which is why//! `labelIds` on a message is a list and why moving a message is adding and//! removing labels rather than a move.//!//! The strings are borrowed from the arena of whatever parsed them -- a//! `labels.List` -- so a `Label` must not outlive it.
const Label = @This();
const std = @import("std");
const json = @import("json.zig");
/// Immutable, and the only thing worth storing. A user label's *name* changes/// whenever the user renames it; its id does not.id: []const u8,/// What the user sees. For a system label this is the id -- `INBOX`, not/// `Inbox` -- which is why `conventionalName` exists.name: []const u8,message_list_visibility: MessageListVisibility = .show,label_list_visibility: LabelListVisibility = .label_show,type: Type = .user,/// Absent from `labels.list`, which returns only id, name and type; a/// `labels.get` fills them in.messages_total: ?u32 = null,messages_unread: ?u32 = null,threads_total: ?u32 = null,threads_unread: ?u32 = null,color: ?Color = null,
pub const Type = enum { /// Created by Gmail, and not removable: the ids in `SystemId`. system, /// Created by the user. user,};
pub const MessageListVisibility = enum { show, hide };
pub const LabelListVisibility = enum { label_show, label_show_if_unread, label_hide,
pub fn fromWire(s: []const u8) ?LabelListVisibility { if (std.mem.eql(u8, s, "labelShow")) return .label_show; if (std.mem.eql(u8, s, "labelShowIfUnread")) return .label_show_if_unread; if (std.mem.eql(u8, s, "labelHide")) return .label_hide; return null; }};
pub const Color = struct { /// `#rrggbb`, and only from a palette of about eighty that Gmail accepts. text_color: ?[]const u8 = null, background_color: ?[]const u8 = null,};
/// The labels Gmail creates itself. Their ids are stable and documented, which/// is what makes it safe to name them here; a label whose id is not one of/// these is a user label, or a system label Gmail has added since.pub const SystemId = enum { INBOX, SPAM, TRASH, UNREAD, STARRED, IMPORTANT, SENT, DRAFT, CHAT, CATEGORY_PERSONAL, CATEGORY_SOCIAL, CATEGORY_PROMOTIONS, CATEGORY_UPDATES, CATEGORY_FORUMS,
pub fn fromId(id: []const u8) ?SystemId { return std.meta.stringToEnum(SystemId, id); }
/// The lowercase name these are conventionally given outside Gmail -- /// `INBOX` becomes `inbox` -- which is what notmuch, maildir flags and /// every other mail store expect to see. /// /// The five `CATEGORY_` labels have no convention because nothing outside /// Gmail has the concept, so they get their Gmail names lowercased and a /// caller that does not want them can drop them. Which labels are worth /// keeping at all is the application's decision, not this library's. pub fn conventionalName(s: SystemId) []const u8 { return switch (s) { .INBOX => "inbox", .SPAM => "spam", .TRASH => "trash", .UNREAD => "unread", .STARRED => "starred", .IMPORTANT => "important", .SENT => "sent", .DRAFT => "draft", .CHAT => "chat", .CATEGORY_PERSONAL => "category/personal", .CATEGORY_SOCIAL => "category/social", .CATEGORY_PROMOTIONS => "category/promotions", .CATEGORY_UPDATES => "category/updates", .CATEGORY_FORUMS => "category/forums", }; }};
/// Whether this label is one Gmail made.pub fn isSystem(l: *const Label) bool { return l.type == .system;}
/// `SystemId.conventionalName` for a system label, and the user's own name/// for a user label. Borrowed, like everything else here.pub fn conventionalName(l: *const Label) []const u8 { if (SystemId.fromId(l.id)) |s| return s.conventionalName(); return l.name;}
pub const ParseError = error{Malformed} || std.mem.Allocator.Error;
/// Reads one label out of an already-parsed JSON value. Strings are borrowed/// from whatever `value` was parsed into.pub fn fromJson(value: std.json.Value) ParseError!Label { if (value != .object) return error.Malformed;
return .{ .id = json.string(value, "id") orelse return error.Malformed, // A label with no name is not something Gmail sends, but a label with // no name is also not worth failing a whole page over. .name = json.string(value, "name") orelse "", .message_list_visibility = if (json.string(value, "messageListVisibility")) |s| std.meta.stringToEnum(MessageListVisibility, s) orelse .show else .show, .label_list_visibility = if (json.string(value, "labelListVisibility")) |s| LabelListVisibility.fromWire(s) orelse .label_show else .label_show, .type = if (json.string(value, "type")) |s| std.meta.stringToEnum(Type, s) orelse .user else .user, .messages_total = json.unsigned(value, "messagesTotal"), .messages_unread = json.unsigned(value, "messagesUnread"), .threads_total = json.unsigned(value, "threadsTotal"), .threads_unread = json.unsigned(value, "threadsUnread"), .color = color: { const c = json.object(value, "color") orelse break :color null; break :color .{ .text_color = json.string(c, "textColor"), .background_color = json.string(c, "backgroundColor"), }; }, };}
const testing = std.testing;
fn parseOne(text: []const u8) !std.json.Parsed(std.json.Value) { return std.json.parseFromSlice(std.json.Value, testing.allocator, text, .{});}
test "a user label" { var p = try parseOne( \\{"id":"Label_12","name":"zig/mailing list","type":"user", \\ "messageListVisibility":"hide","labelListVisibility":"labelShowIfUnread", \\ "messagesTotal":42,"messagesUnread":3, \\ "color":{"textColor":"#ffffff","backgroundColor":"#000000"}} ); defer p.deinit();
const l: Label = try .fromJson(p.value); try testing.expectEqualStrings("Label_12", l.id); try testing.expectEqualStrings("zig/mailing list", l.name); try testing.expectEqual(Label.Type.user, l.type); try testing.expectEqual(Label.MessageListVisibility.hide, l.message_list_visibility); try testing.expectEqual(Label.LabelListVisibility.label_show_if_unread, l.label_list_visibility); try testing.expectEqual(@as(?u32, 42), l.messages_total); try testing.expectEqual(@as(?u32, null), l.threads_total); try testing.expectEqualStrings("#000000", l.color.?.background_color.?); try testing.expect(!l.isSystem()); try testing.expectEqualStrings("zig/mailing list", l.conventionalName());}
test "a system label gets its conventional name" { var p = try parseOne("{\"id\":\"INBOX\",\"name\":\"INBOX\",\"type\":\"system\"}"); defer p.deinit();
const l: Label = try .fromJson(p.value); try testing.expect(l.isSystem()); try testing.expectEqualStrings("inbox", l.conventionalName()); try testing.expectEqual(Label.SystemId.INBOX, Label.SystemId.fromId(l.id).?);}
test "defaults are the documented ones" { var p = try parseOne("{\"id\":\"Label_1\"}"); defer p.deinit();
const l: Label = try .fromJson(p.value); try testing.expectEqualStrings("", l.name); try testing.expectEqual(Label.Type.user, l.type); try testing.expectEqual(Label.MessageListVisibility.show, l.message_list_visibility); try testing.expectEqual(Label.LabelListVisibility.label_show, l.label_list_visibility);}
test "an enum value nobody has heard of does not fail the parse" { // A label list is fetched to be walked. A new visibility value Gmail adds // next year must not turn the whole page into an error. var p = try parseOne( \\{"id":"X","type":"archangel","messageListVisibility":"shimmer", \\ "labelListVisibility":"labelShimmer"} ); defer p.deinit();
const l: Label = try .fromJson(p.value); try testing.expectEqual(Label.Type.user, l.type); try testing.expectEqual(Label.MessageListVisibility.show, l.message_list_visibility); try testing.expectEqual(Label.LabelListVisibility.label_show, l.label_list_visibility);}
test "no id is malformed" { var p = try parseOne("{\"name\":\"nameless\"}"); defer p.deinit(); try testing.expectError(error.Malformed, Label.fromJson(p.value));
var q = try parseOne("[]"); defer q.deinit(); try testing.expectError(error.Malformed, Label.fromJson(q.value));}
test "every system id has a conventional name" { for (std.enums.values(Label.SystemId)) |s| { try testing.expect(s.conventionalName().len > 0); try testing.expectEqual(s, Label.SystemId.fromId(@tagName(s)).?); } try testing.expectEqual(@as(?Label.SystemId, null), Label.SystemId.fromId("Label_99"));}