hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit 32f41590febed2bce40636146614192e105487ae
parent bccb701a15a201da31460c9bdc7f705fa890bff0
Author: Sebastian <sebastian@sebsite.pw>
Date:   Sun,  1 Dec 2024 20:36:08 -0500

mime: get rid of errors dependency

This is a breaking change.

References: https://todo.sr.ht/~sircmpwn/hare/873
Signed-off-by: Sebastian <sebastian@sebsite.pw>

Diffstat:
Mmime/parse.ha | 43++++++++++++++++++++++++-------------------
1 file changed, 24 insertions(+), 19 deletions(-)

diff --git a/mime/parse.ha b/mime/parse.ha @@ -2,27 +2,32 @@ // (c) Hare authors <https://harelang.org> use ascii; -use errors; use strings; const tspecial: str = "()<>@,;:\\/[]?="; export type type_params = strings::tokenizer; +// A syntax error. +export type syntax = !void; + +// Converts an error into a human-friendly string. +export fn strerror(err: syntax) str = "Can't parse Media Type"; + // Parses a Media Type, returning a tuple of the content type (e.g. -// "text/plain") and a parameter parser object, or [[errors::invalid]] if the -// input cannot be parsed. +// "text/plain") and a parameter parser object, or [[syntax]] if the input +// cannot be parsed. // // To enumerate the Media Type parameter list, pass the type_params object into // [[next_param]]. If you do not need the parameter list, you can safely discard // the object. Note that any format errors following the ";" token will not -// cause [[errors::invalid]] to be returned unless [[next_param]] is used to -// enumerate all of the parameters. -export fn parse(in: str) ((str, type_params) | errors::invalid) = { +// cause [[syntax]] to be returned unless [[next_param]] is used to enumerate +// all of the parameters. +export fn parse(in: str) ((str, type_params) | syntax) = { const items = strings::cut(in, ";"); const mtype = items.0, params = items.1; const items = strings::cut(mtype, "/"); if (len(items.0) < 1 || len(items.1) < 1) { - return errors::invalid; + return syntax; }; typevalid(items.0)?; typevalid(items.1)?; @@ -31,13 +36,13 @@ export fn parse(in: str) ((str, type_params) | errors::invalid) = { // Returns the next parameter as a (key, value) tuple from a [[type_params]] // object that was prepared via [[parse]], done if there are no remaining -// parameters, and [[errors::invalid]] if a syntax error was encountered. -export fn next_param(in: *type_params) ((str, str) | done | errors::invalid) = { +// parameters, and [[syntax]] if a syntax error was encountered. +export fn next_param(in: *type_params) ((str, str) | done | syntax) = { const tok = match (strings::next_token(in: *strings::tokenizer)) { case let s: str => if (s == "") { // empty parameter - return errors::invalid; + return syntax; }; yield s; case done => @@ -50,7 +55,7 @@ export fn next_param(in: *type_params) ((str, str) | done | errors::invalid) = { items.0 = strings::trim(items.0); items.1 = strings::trim(items.1); if (len(items.0) == 0 || len(items.1) == 0) { - return errors::invalid; + return syntax; }; if (strings::hasprefix(items.1, "\"")) { @@ -60,7 +65,7 @@ export fn next_param(in: *type_params) ((str, str) | done | errors::invalid) = { return (items.0, items.1); }; -fn quoted(in: str) (str | errors::invalid) = { +fn quoted(in: str) (str | syntax) = { // We have only a basic implementation of quoted-string. It has a couple // of problems: // @@ -74,18 +79,18 @@ fn quoted(in: str) (str | errors::invalid) = { if (strings::contains(in, "\\") || strings::contains(in, "\r") || strings::contains(in, "\n")) { - return errors::invalid; + return syntax; }; return in; }; -fn typevalid(in: str) (void | errors::invalid) = { +fn typevalid(in: str) (void | syntax) = { const miter = strings::iter(in); for (let rn => strings::next(&miter)) { if (!ascii::valid(rn) || rn == ' ' || ascii::iscntrl(rn) || strings::contains(tspecial, rn)) { - return errors::invalid; + return syntax; }; }; }; @@ -106,11 +111,11 @@ fn typevalid(in: str) (void | errors::invalid) = { assert(param.0 == "foo" && param.1 == "bar baz"); assert(next_param(&params) is done); - assert(parse("hi") is errors::invalid); - assert(parse("text/ spaces ") is errors::invalid); - assert(parse("text/@") is errors::invalid); + assert(parse("hi") is syntax); + assert(parse("text/ spaces ") is syntax); + assert(parse("text/@") is syntax); const res = parse("text/plain;charset")!; assert(res.0 == "text/plain"); - assert(next_param(&res.1) is errors::invalid); + assert(next_param(&res.1) is syntax); };