From 8521320a7c4dd3b839add88c95649db2c7159fa9 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Sat, 12 Oct 2024 23:51:52 -0400 Subject: [PATCH] Locale defines its own invalid error --- format/desktop_entry/error.ha | 2 +- format/desktop_entry/parse.ha | 2 +- locale/+linux.ha | 10 +++++----- locale/-linux.ha | 2 -- locale/error.ha | 7 +++++++ locale/locale.ha | 14 +++++++------- locale/locale_test.ha | 5 ++--- 7 files changed, 23 insertions(+), 19 deletions(-) create mode 100644 locale/error.ha diff --git a/format/desktop_entry/error.ha b/format/desktop_entry/error.ha index 0e46951..f59784d 100644 --- a/format/desktop_entry/error.ha +++ b/format/desktop_entry/error.ha @@ -1,4 +1,4 @@ -// Any error which may be returned from a function in this module. +// All errors defined in this module. export type error = !( invalid_group_header | invalid_entry | diff --git a/format/desktop_entry/parse.ha b/format/desktop_entry/parse.ha index 2c11852..2439335 100644 --- a/format/desktop_entry/parse.ha +++ b/format/desktop_entry/parse.ha @@ -109,7 +109,7 @@ fn parse_entry(line: str) ((str, str, (locale::locale | void)) | error) = { local = match(locale::parse(local_string)) { case let local: locale::locale => yield local; - case errors::invalid => return invalid_entry; + case locale::invalid => return invalid_entry; }; }; diff --git a/locale/+linux.ha b/locale/+linux.ha index 886492d..82efbba 100644 --- a/locale/+linux.ha +++ b/locale/+linux.ha @@ -52,29 +52,29 @@ fn get_locale(var: str) locale = }; }; -fn get_locale_no_fallback(var: str) (locale | errors::invalid) = +fn get_locale_no_fallback(var: str) (locale | invalid) = match (get_env_locale(var)) { case let local: locale => yield local; case => yield (get_locale_conf_entry(var)); }; -fn get_env_locale(var: str) (locale | errors::invalid) = +fn get_env_locale(var: str) (locale | invalid) = match (os::getenv(var)) { case let env: str => return parse(env); - case => return errors::invalid; + case => return invalid; }; let locale_conf: []str = []; @fini fn locale_conf() void = strings::freeall(locale_conf); -fn get_locale_conf_entry(var: str) (locale | errors::invalid) = { +fn get_locale_conf_entry(var: str) (locale | invalid) = { get_locale_conf(); for (let entry .. locale_conf) { let (key, value) = strings::cut(entry, "="); if (key == var) return parse(value); }; - return errors::invalid; + return invalid; }; fn get_locale_conf() []str = { diff --git a/locale/-linux.ha b/locale/-linux.ha index f31a887..b705b57 100644 --- a/locale/-linux.ha +++ b/locale/-linux.ha @@ -1,5 +1,3 @@ -use errors; - // Returns the locale to use for character classification and case conversion. // The memory is statically allocated and must not be free'd. It may be // overwritten later, so use [[dup]] to extend its lifetime. diff --git a/locale/error.ha b/locale/error.ha new file mode 100644 index 0000000..7d357c0 --- /dev/null +++ b/locale/error.ha @@ -0,0 +1,7 @@ +// All errors defined in this module. +export type error = !invalid; + +// Returned when a malformed locale is encountered. +export type invalid = !void; + +export fn strerror(err: error) str = "invalid locale"; diff --git a/locale/locale.ha b/locale/locale.ha index f514321..b72fcda 100644 --- a/locale/locale.ha +++ b/locale/locale.ha @@ -21,16 +21,16 @@ export def c = locale { // lang_COUNTRY.ENCODING@MODIFIER // // Where _COUNTRY, .ENCODING, and @MODIFIER may be omitted. The function -// returns a [[locale]], or [[errors::invalid]] if the input cannot be parsed. -// All memory is borrowed from the input, so [[finish]] should not be used to -// free it. -export fn parse(in: str) (locale | errors::invalid) = { +// returns a [[locale]], or [[invalid]] if the input cannot be parsed. All +// memory is borrowed from the input, so [[finish]] should not be used to free +// it. +export fn parse(in: str) (locale | invalid) = { let (in, modifier) = strings::rcut(in, "@"); - if (strings::compare(in, "") == 0) return void: errors::invalid; + if (strings::compare(in, "") == 0) return void: invalid; let (in, encoding) = strings::rcut(in, "."); - if (strings::compare(in, "") == 0) return void: errors::invalid; + if (strings::compare(in, "") == 0) return void: invalid; let (in, country) = strings::rcut(in, "_"); - if (strings::compare(in, "") == 0) return void: errors::invalid; + if (strings::compare(in, "") == 0) return void: invalid; return locale { lang = in, country = country, diff --git a/locale/locale_test.ha b/locale/locale_test.ha index d954d78..793c789 100644 --- a/locale/locale_test.ha +++ b/locale/locale_test.ha @@ -1,5 +1,4 @@ use fmt; -use errors; use strings; @test fn parse_full() void = { @@ -64,8 +63,8 @@ use strings; @test fn parse_error() void = { let local = match(parse("_COUNTRY.ENCODING@MODIFIER")) { - case errors::invalid => void; - case => abort("error"); + case invalid => void; + case => abort("no error"); }; };