format::desktop_entry: Add untested parsing for integer values

This commit is contained in:
Sasha Koshka 2024-10-21 17:36:40 -04:00
parent 9e4d7e4ae4
commit fc0cab93bc
2 changed files with 33 additions and 1 deletions

View File

@ -12,6 +12,7 @@ export type syntaxerr = !(
invalid_escape | invalid_escape |
invalid_boolean | invalid_boolean |
invalid_numeric | invalid_numeric |
invalid_integer |
expected_single); expected_single);
// Returned when a malformed group header is encountered while parsing. // Returned when a malformed group header is encountered while parsing.
@ -35,6 +36,10 @@ export type invalid_boolean = !void;
// was given. // was given.
export type invalid_numeric = !void; export type invalid_numeric = !void;
// Returned when a numeric value was expected while parsing, but something else
// was given.
export type invalid_integer = !void;
// Returned when a singular value was expected while parsing, but multiple // Returned when a singular value was expected while parsing, but multiple
// values were given. // values were given.
export type expected_single = !void; export type expected_single = !void;
@ -48,6 +53,7 @@ case invalid_ascii => yield "invalid ascii";
case invalid_escape => yield "invalid escape"; case invalid_escape => yield "invalid escape";
case invalid_boolean => yield "invalid boolean"; case invalid_boolean => yield "invalid boolean";
case invalid_numeric => yield "invalid numeric"; case invalid_numeric => yield "invalid numeric";
case invalid_integer => yield "invalid integer";
case expected_single => yield "expected single"; case expected_single => yield "expected single";
case let err: io::error => yield io::strerror(err); case let err: io::error => yield io::strerror(err);
case let err: utf8::invalid => yield utf8::strerror(err); case let err: utf8::invalid => yield utf8::strerror(err);

View File

@ -2,7 +2,7 @@ use ascii;
use strconv; use strconv;
use strings; use strings;
// TODO icon_theme references an "integer" data type. support that here. // TODO numeric should really be f32
// Parses a string value. It may contain all ASCII characters except for control // Parses a string value. It may contain all ASCII characters except for control
// characters. The memory must be freed by the caller. // characters. The memory must be freed by the caller.
@ -56,6 +56,15 @@ case =>
yield invalid_numeric; yield invalid_numeric;
}; };
// Parses an integer value. While there is no documentation on this data type,
// it is referred to by the Icon Theme Specification, §4, tables 2-3.
export fn parse_integer(in: str) (int | error) = match(strconv::stoi(in)) {
case let integer: int => yield integer;
case =>
if (strings::contains(in, ";")) return expected_single;
yield invalid_integer;
};
// Parses multiple string values. See [[parse_string]] for more information. The // Parses multiple string values. See [[parse_string]] for more information. The
// memory must be freed by the caller. // memory must be freed by the caller.
// Specification: §4 // Specification: §4
@ -125,6 +134,23 @@ export fn parse_numerics(in: str) ([]f64 | error) = {
return result; return result;
}; };
// Parses multiple integer values. See [[parse_integer]] for more information.
// The memory must be freed by the caller.
export fn parse_integers(in: str) ([]int | error) = {
let splitte = split(in);
let result: []int = alloc([], 0);
for (let in => splitter_next(&splitte)) {
match (parse_integer(in)) {
case let number: int =>
append(result, number);
case let err: error =>
free(result);
return err;
};
};
return result;
};
type string_escaper = []u8; type string_escaper = []u8;
fn escape_string(in: str) string_escaper = strings::toutf8(in): string_escaper; fn escape_string(in: str) string_escaper = strings::toutf8(in): string_escaper;