Better errors

This commit is contained in:
Sasha Koshka 2024-10-12 23:34:44 -04:00
parent 427d30c255
commit 3e72688127

View File

@ -1,28 +1,27 @@
// Any error which may be returned from a function in this module.
export type error = !enum {
INVALID_GROUP_HEADER,
INVALID_ENTRY,
// unused for now
DUPLICATE_GROUP,
DUPLICATE_ENTRY,
DUPLICATE_LOCALIZATION,
NO_DEFAULT_VALUE,
ENTRY_OUTSIDE_GROUP,
// --------------
UNSUPPORTED_ESCAPE,
STRING_NOT_ASCII,
};
export type error = !(
invalid_group_header |
invalid_entry |
invalid_ascii |
invalid_escape);
// Returned when a malformed group header is encountered while parsing.
export type invalid_group_header = !void;
// Returned when a malformed entry is encountered while parsing.
export type invalid_entry = !void;
// Returned when ASCII text was expected, while parsing, but something else was
// given.
export type invalid_ascii = !void;
// Returned when an invalid escape sequence was enctountered while parsing.
export type invalid_escape = !void;
// Converts a desktop entry [[error]] into a user-friendly string.
export fn strerror(err: error) str = switch {
case INVALID_GROUP_HEADER => yield "invalid group header";
case INVALID_ENTRY => yield "invalid entry";
case DUPLICATE_GROUP => yield "duplicate group";
case DUPLICATE_ENTRY => yield "duplicate entry";
case DUPLICATE_LOCALIZATION => yield "duplicate localization";
case NO_DEFAULT_VALUE => yield "no default value";
case ENTRY_OUTSIDE_GROUP => yield "entry outside group";
case UNSUPPORTED_ESCAPE => yield "unsupported escape";
case STRING_NOT_ASCII => yield "string not ascii";
case => yield "unknown";
export fn strerror(err: error) str = match (err) {
case invalid_group_header => yield "invalid group header";
case invalid_entry => yield "invalid entry";
case invalid_ascii => yield "invalid ascii";
case invalid_escape => yield "invalid escape";
};