···
let mailbox_of_json json =
+
Printf.printf "Parsing mailbox JSON\n";
let id = get_string (find json ["id"]) in
+
Printf.printf "Got id: %s\n" id;
let name = get_string (find json ["name"]) in
+
Printf.printf "Got name: %s\n" name;
+
(* Handle parentId which can be null *)
+
match find_opt json ["parentId"] with
+
| Some (`String s) -> Some s
+
Printf.printf "Got parent_id: %s\n" (match parent_id with Some p -> p | None -> "None");
+
(* Handle role which might be null *)
+
match find_opt json ["role"] with
+
| Some (`String s) -> Some (Json.mailbox_role_of_string s)
+
Printf.printf "Got role\n";
let sort_order = get_int (find json ["sortOrder"]) in
+
Printf.printf "Got sort_order: %d\n" sort_order;
let total_emails = get_int (find json ["totalEmails"]) in
+
Printf.printf "Got total_emails: %d\n" total_emails;
let unread_emails = get_int (find json ["unreadEmails"]) in
+
Printf.printf "Got unread_emails: %d\n" unread_emails;
let total_threads = get_int (find json ["totalThreads"]) in
+
Printf.printf "Got total_threads: %d\n" total_threads;
let unread_threads = get_int (find json ["unreadThreads"]) in
+
Printf.printf "Got unread_threads: %d\n" unread_threads;
let is_subscribed = get_bool (find json ["isSubscribed"]) in
+
Printf.printf "Got is_subscribed: %b\n" is_subscribed;
let rights_json = find json ["myRights"] in
+
Printf.printf "Got rights_json\n";
Types.may_read_items = get_bool (find rights_json ["mayReadItems"]);
may_add_items = get_bool (find rights_json ["mayAddItems"]);
···
may_delete = get_bool (find rights_json ["mayDelete"]);
may_submit = get_bool (find rights_json ["maySubmit"]);
+
Printf.printf "Constructed my_rights\n";
···
+
Printf.printf "Constructed mailbox result\n";
+
Printf.printf "Not_found error: %s\n" (Printexc.to_string e);
+
Printexc.print_backtrace stdout;
+
Error (Parse_error "Required field not found in mailbox object")
+
| Invalid_argument msg ->
+
Printf.printf "Invalid_argument error: %s\n" msg;
+
Error (Parse_error msg)
+
Printf.printf "Unknown error: %s\n" (Printexc.to_string e);
+
Error (Parse_error (Printexc.to_string e))
(** Convert JSON email object to OCaml type *)
+
Printf.printf "Parsing email JSON\n";
let id = get_string (find json ["id"]) in
+
Printf.printf "Got email id: %s\n" id;
let blob_id = get_string (find json ["blobId"]) in
let thread_id = get_string (find json ["threadId"]) in
···
let size = get_int (find json ["size"]) in
let received_at = get_string (find json ["receivedAt"]) in
+
(* Handle messageId which might be an array or missing *)
+
match find_opt json ["messageId"] with
+
| Some (`A ids) -> List.map (fun id ->
+
| _ -> raise (Invalid_argument "messageId item is not a string")
+
| Some (`String s) -> [s] (* Handle single string case *)
+
| None -> [] (* Handle missing case *)
+
| _ -> raise (Invalid_argument "messageId has unexpected type")
(* Parse optional fields *)
···
Some (List.map (fun addr_json ->
+
match find_opt addr_json ["name"] with
+
| Some (`String s) -> Some s
let email = get_string (find addr_json ["email"]) in
+
match find_opt addr_json ["parameters"] with
+
| Some (`O items) -> List.map (fun (k, v) ->
{ Types.name; email; parameters }
···
+
(* Handle optional string arrays with null handling *)
+
let parse_string_array_opt field_name =
+
match find_opt json [field_name] with
+
Some (List.filter_map (function
+
let in_reply_to = parse_string_array_opt "inReplyTo" in
+
let references = parse_string_array_opt "references" in
let sender = parse_email_addresses (find_opt json ["sender"]) in
let from = parse_email_addresses (find_opt json ["from"]) in
···
let bcc = parse_email_addresses (find_opt json ["bcc"]) in
let reply_to = parse_email_addresses (find_opt json ["replyTo"]) in
+
(* Handle optional string fields with null handling *)
+
let parse_string_opt field_name =
+
match find_opt json [field_name] with
+
| Some (`String s) -> Some s
+
let subject = parse_string_opt "subject" in
+
let sent_at = parse_string_opt "sentAt" in
+
(* Handle optional boolean fields with null handling *)
+
let parse_bool_opt field_name =
+
match find_opt json [field_name] with
+
| Some (`Bool b) -> Some b
+
let has_attachment = parse_bool_opt "hasAttachment" in
+
let preview = parse_string_opt "preview" in
(* Body parts parsing would go here - omitting for brevity *)
+
Printf.printf "Email parsed successfully\n";
···
+
Printf.printf "Email parse error - Not_found: %s\n" (Printexc.to_string e);
+
Printexc.print_backtrace stdout;
+
Error (Parse_error "Required field not found in email object")
+
| Invalid_argument msg ->
+
Printf.printf "Email parse error - Invalid_argument: %s\n" msg;
+
Error (Parse_error msg)
+
Printf.printf "Email parse error - Unknown: %s\n" (Printexc.to_string e);
+
Error (Parse_error (Printexc.to_string e))
(** Login to a JMAP server and establish a connection
@param uri The URI of the JMAP server