// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! What `users.getProfile` returns: who the mailbox belongs to, how much is //! in it, and where the history sequence has got to. //! //! `history_id` is the reason to call it. Synchronising a mailbox for the //! first time means listing it *and* remembering where the history stood when //! the listing began, so the catch-up afterwards has somewhere to start; this //! is where that number comes from. const Profile = @This(); const std = @import("std"); const json = @import("json.zig"); /// The address of the mailbox, which need not be the address that was /// authenticated -- an alias or a delegated mailbox answers with its own. email_address: []const u8, messages_total: ?u32 = null, threads_total: ?u32 = null, /// The current point in the mailbox history. Sent as a quoted decimal string. history_id: ?u64 = null, pub const ParseError = error{Malformed} || std.mem.Allocator.Error; pub fn fromJson(value: std.json.Value) ParseError!Profile { if (value != .object) return error.Malformed; return .{ .email_address = json.string(value, "emailAddress") orelse return error.Malformed, .messages_total = json.unsigned(value, "messagesTotal"), .threads_total = json.unsigned(value, "threadsTotal"), .history_id = json.unsigned64(value, "historyId"), }; } const testing = std.testing; test "a profile" { var p = try std.json.parseFromSlice(std.json.Value, testing.allocator, \\{"emailAddress":"jeff@ocjtech.us","messagesTotal":123456, \\ "threadsTotal":65432,"historyId":"9988776655"} , .{}); defer p.deinit(); const profile: Profile = try .fromJson(p.value); try testing.expectEqualStrings("jeff@ocjtech.us", profile.email_address); try testing.expectEqual(@as(?u32, 123456), profile.messages_total); try testing.expectEqual(@as(?u32, 65432), profile.threads_total); try testing.expectEqual(@as(?u64, 9988776655), profile.history_id); } test "only the address is required" { var p = try std.json.parseFromSlice(std.json.Value, testing.allocator, "{\"emailAddress\":\"a@b\"}", .{}); defer p.deinit(); const profile: Profile = try .fromJson(p.value); try testing.expectEqual(@as(?u64, null), profile.history_id); var q = try std.json.parseFromSlice(std.json.Value, testing.allocator, "{}", .{}); defer q.deinit(); try testing.expectError(error.Malformed, Profile.fromJson(q.value)); }