Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
6.2 kB · 145 lines
Zig
at zig-0.16
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146// SPDX-FileCopyrightText: © 2024 Jeffrey C. Ollie// SPDX-License-Identifier: GPL-3.0-or-later
//! The `Error` set covering every `notmuch_status_t` code, and `wrap`, which//! converts a C status return into a Zig error union.//!//! Wrappers with narrowed error sets instead convert the codes they document//! individually and map anything undocumented to `error.Unexpected`.const std = @import("std");
const c = @import("c");
const status = @import("enums.zig").status;const Status = @import("enums.zig").Status;
pub const Error = error{ /// Syntax error in query. BadQuerySyntax, /// Database is not fully opened, or has been closed. ClosedDatabase, /// Database already exists, so not (re-)created. DatabaseExists, /// A file contains a message ID that is identical to a message already in /// the database. DuplicateMessageID, /// Notmuch attempted to do crypto processing, but could not initialize the /// engine needed to do so. FailedCryptoContextCreation, /// An error occurred trying to read or write to a file (this could be file /// not found, permission denied, etc.) FileError, /// A file was presented that doesn't appear to be an email message. FileNotEmail, /// There was an error determining the database format version. FormatVersionError, /// The requested operation was ignored. Depending on the function, this may /// not be an actual error. Ignored, /// One of the arguments violates the preconditions for the function, in a /// way not covered by a more specific argument. IllegalArgument, /// The iterator being examined has been exhausted and contains no more /// items. IteratorExhausted, /// A MIME object claimed to have cryptographic protection which notmuch /// tried to handle, but the protocol was not specified in an intelligible /// way. MalformedCryptoProtocol, /// Unable to load a config file. NoConfig, /// Unable to load a database. NoDatabase, /// No mail root could be deduced from parameters and environment. NoMailRoot, /// A newer version of the notmuch library is required. NotmuchVersion, /// The user erroneously passed a NULL pointer to a notmuch function. NullPointer, /// An operation that was being performed on the database has been /// invalidated while in progress, and must be re-executed. /// /// This will typically happen while iterating over query results and the /// underlying Xapian database is modified by another process so that the /// currently open version cannot be read anymore. OperationInvalidated, /// Out of memory. OutOfMemory, /// There is a problem with the proposed path, e.g. a relative path passed /// to a function expecting an absolute path. PathError, /// An attempt was made to write to a database opened in read-only mode. ReadOnlyDatabase, /// A tag value is too long (exceeds NOTMUCH_TAG_MAX). TagTooLong, /// notmuch_database_end_atomic has been called more times than /// notmuch_database_begin_atomic. UnbalancedAtomic, /// The notmuch_message_thaw function has been called more times than /// notmuch_message_freeze. UnbalancedFreezeThaw, /// libnotmuch returned a status code that is not documented for this /// operation. This can happen when a newer libnotmuch adds return values, /// or when an operation is attempted on a closed database. Unexpected, /// A MIME object claimed to have cryptographic protection, and notmuch /// attempted to process it, but the specific protocol was something that /// notmuch doesn't know how to handle. UnknownCryptoProtocol, /// The operation is not supported. UnsupportedOperation, /// The operation requires a database upgrade. UpgradeRequired, /// A Xapian exception occurred. XapianException,};
pub fn wrap(rc: c.notmuch_status_t) Error!void { return switch (status(rc)) { .success => {}, .bad_query_syntax => error.BadQuerySyntax, .closed_database => error.ClosedDatabase, .database_exists => error.DatabaseExists, .duplicate_message_id => error.DuplicateMessageID, .failed_crypto_context_creation => error.FailedCryptoContextCreation, .file_error => error.FileError, .file_not_email => error.FileNotEmail, .ignored => error.Ignored, .illegal_argument => error.IllegalArgument, .iterator_exhausted => error.IteratorExhausted, .malformed_crypto_protocol => error.MalformedCryptoProtocol, .no_config => error.NoConfig, .no_database => error.NoDatabase, .no_mail_root => error.NoMailRoot, .null_pointer => error.NullPointer, .operation_invalidated => error.OperationInvalidated, .out_of_memory => error.OutOfMemory, .path_error => error.PathError, .read_only_database => error.ReadOnlyDatabase, .tag_too_long => error.TagTooLong, .unbalanced_atomic => error.UnbalancedAtomic, .unbalanced_freeze_thaw => error.UnbalancedFreezeThaw, .unknown_crypto_protocol => error.UnknownCryptoProtocol, .unsupported_operation => error.UnsupportedOperation, .upgrade_required => error.UpgradeRequired, .xapian_exception => error.XapianException, };}
test wrap { // NOTMUCH_STATUS_SUCCESS maps to no error at all. try wrap(c.NOTMUCH_STATUS_SUCCESS);
// Every other status code maps to its corresponding error. try std.testing.expectError(error.OutOfMemory, wrap(c.NOTMUCH_STATUS_OUT_OF_MEMORY)); try std.testing.expectError(error.ReadOnlyDatabase, wrap(c.NOTMUCH_STATUS_READ_ONLY_DATABASE)); try std.testing.expectError(error.BadQuerySyntax, wrap(c.NOTMUCH_STATUS_BAD_QUERY_SYNTAX));
// No status other than success maps to success. inline for (@typeInfo(Status).@"enum".fields) |field| { const code: Status = @enumFromInt(field.value); if (code != .success) { try std.testing.expect(std.meta.isError(wrap(@intCast(field.value)))); } }}