Initial Commit
This commit is contained in:
1625 files changed
+43221
No files matched your search
@@ -0,0 +1,277 @@
|
||||
#! @file serial.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains what writing and reading values knows apart from any format: the
|
||||
#! `@serial` attribute a program marks its types and fields with, the traits a
|
||||
#! format implements to be written and read through, and the traits a type with a
|
||||
#! form of its own implements. `serial::encode` and `serial::decode` walk a value
|
||||
#! by what is marked, so JSON, and any other format, read the marks the same way.
|
||||
|
||||
module std::serial;
|
||||
|
||||
|
||||
#! Marks a type to be written and read, and each of its fields, enum members,
|
||||
#! and union fields that are. A field left unmarked is never written, and
|
||||
#! reading leaves it at its default, or else its zero value.
|
||||
#!
|
||||
#! The marks are also what lets `serial::encode` and `serial::decode` see a type
|
||||
#! private to the program, since reflection gives a module what carries an
|
||||
#! attribute it declares. So a type is marked, and each field written, even
|
||||
#! where it is `pub`.
|
||||
@usage{ on = target::structs | target::unions | target::enums | target::fields | target::members }
|
||||
pub struct serial : attribute {
|
||||
pub val name: strview = ""; #! The name written in place of the declaration's, where it is not empty.
|
||||
}
|
||||
|
||||
#! How strictly `serial::decode` reads.
|
||||
pub struct options {
|
||||
pub val strict: bool = false; #! Whether a member no field takes, and a member written twice, are errors.
|
||||
}
|
||||
|
||||
#! The kinds of values every format writes and reads.
|
||||
pub enum kind: u8 {
|
||||
empty, #! Nothing: JSON's `null`.
|
||||
boolean, #! `true` or `false`.
|
||||
number, #! A number, integer or real.
|
||||
text, #! Text.
|
||||
array, #! A list of values, in order.
|
||||
object, #! Members, each a name and a value.
|
||||
}
|
||||
|
||||
#! Why a value could not be written or read, whatever the format.
|
||||
pub enum error_kind: u8 {
|
||||
wrong_kind, #! A value is of another kind than the type asked for, as text for a number.
|
||||
missing_member, #! An object lacks a member for a field that is not a `maybe`.
|
||||
unknown_member, #! An object has a member no field takes, which only strict reading refuses.
|
||||
repeated_member, #! An object has a member twice, which only strict reading refuses.
|
||||
out_of_range, #! A number does not fit the type asked for, or a name is no member's.
|
||||
not_serial, #! A value holds an enum member or union field that is not `@serial`.
|
||||
}
|
||||
|
||||
#! Where a value was read, from the start of the text it was read from.
|
||||
pub struct place {
|
||||
pub var offset: u64; #! The byte the value begins at.
|
||||
pub var line: u32; #! Counted from 1.
|
||||
pub var column: u32; #! The byte within the line, counted from 1.
|
||||
}
|
||||
|
||||
#! Why a value could not be read or written, and where it was read.
|
||||
#!
|
||||
#! An error is plain data, so `else (e)` names it.
|
||||
pub struct error {
|
||||
pub var kind: error_kind; #! What went wrong.
|
||||
pub var offset: u64; #! The byte the value that failed begins at.
|
||||
pub var line: u32; #! Its line, counted from 1.
|
||||
pub var column: u32; #! The byte within the line, counted from 1.
|
||||
}
|
||||
|
||||
#! Makes an error of a kind, at a place.
|
||||
#! @param kind The kind of error.
|
||||
#! @param at Where the value that failed was read.
|
||||
#! @returns The error.
|
||||
pub def error::at(kind: error_kind, at: place): error {
|
||||
return error { kind = kind, offset = at.offset, line = at.line, column = at.column };
|
||||
}
|
||||
|
||||
#! Says what the error is, in a sentence, after where it is.
|
||||
#! @param this The error to describe.
|
||||
#! @returns The description, as `3:14: An object lacks a member a field needs.`
|
||||
pub def error::describe(&this): string {
|
||||
return $"{this.line}:{this.column}: {describe(this.kind)}";
|
||||
}
|
||||
|
||||
#! Says what a kind of error is, in a sentence.
|
||||
#! @param kind The kind to describe.
|
||||
#! @returns The sentence.
|
||||
pub def describe(kind: error_kind): strview {
|
||||
return match kind {
|
||||
wrong_kind => "The value is not of the kind the type asks for.",
|
||||
missing_member => "An object lacks a member that a field needs.",
|
||||
unknown_member => "An object has a member that no field takes.",
|
||||
repeated_member => "An object has a member more than once.",
|
||||
out_of_range => "The value does not fit the type asked for.",
|
||||
not_serial => "The value holds an enum member or union field that is not `@serial`.",
|
||||
};
|
||||
}
|
||||
|
||||
#! What a format writes values through, as `json::writer` does.
|
||||
#!
|
||||
#! A format keeps the first failure in the data, such as a number it cannot
|
||||
#! hold, or one `fail` reports, and gives it when it finishes, so each value
|
||||
#! needs no check. Misuse, such as ending an array where an object is open, is
|
||||
#! the program's mistake and stops it.
|
||||
pub trait encoder {
|
||||
#! Begins an object, whose members are each written as `member` and then a
|
||||
#! value.
|
||||
#! @param this The format to write through.
|
||||
#! @param count How many members it has, which a format writing their count
|
||||
#! ahead uses.
|
||||
def begin_object(&mut this, count: u64): void;
|
||||
|
||||
#! Writes the name of the member whose value is written next.
|
||||
#! @param this The format to write through.
|
||||
#! @param name The member's name.
|
||||
def member(&mut this, name: strview): void;
|
||||
|
||||
#! Ends the object begun last.
|
||||
#! @param this The format to write through.
|
||||
def end_object(&mut this): void;
|
||||
|
||||
#! Begins an array, whose elements are each written as a value.
|
||||
#! @param this The format to write through.
|
||||
#! @param count How many elements it has, which a format writing their count
|
||||
#! ahead uses.
|
||||
def begin_array(&mut this, count: u64): void;
|
||||
|
||||
#! Ends the array begun last.
|
||||
#! @param this The format to write through.
|
||||
def end_array(&mut this): void;
|
||||
|
||||
#! Writes text as a value.
|
||||
#! @param this The format to write through.
|
||||
#! @param value The text, as UTF-8.
|
||||
def text(&mut this, value: strview): void;
|
||||
|
||||
#! Writes a signed integer as a value.
|
||||
#! @param this The format to write through.
|
||||
#! @param value The integer.
|
||||
def signed(&mut this, value: i64): void;
|
||||
|
||||
#! Writes an unsigned integer as a value.
|
||||
#! @param this The format to write through.
|
||||
#! @param value The integer.
|
||||
def unsigned(&mut this, value: u64): void;
|
||||
|
||||
#! Writes a real number as a value.
|
||||
#! @param this The format to write through.
|
||||
#! @param value The number, which a format that holds no infinity or NaN
|
||||
#! keeps as a failure.
|
||||
def real(&mut this, value: f64): void;
|
||||
|
||||
#! Writes `true` or `false` as a value.
|
||||
#! @param this The format to write through.
|
||||
#! @param value The value.
|
||||
def boolean(&mut this, value: bool): void;
|
||||
|
||||
#! Writes nothing as a value: JSON's `null`.
|
||||
#! @param this The format to write through.
|
||||
def empty(&mut this): void;
|
||||
|
||||
#! Keeps a failure the walk finds, where the format keeps its own, so it is
|
||||
#! given when the format finishes.
|
||||
#! @param this The format to write through.
|
||||
#! @param kind What went wrong.
|
||||
def fail(&mut this, kind: error_kind): void;
|
||||
}
|
||||
|
||||
#! A document a format has read, as `json::document` is, whose values are known
|
||||
#! by their indices in it. It names no `self`, so a form of a type's own takes
|
||||
#! it as a `&decoder`.
|
||||
pub trait decoder {
|
||||
#! The kind of a value.
|
||||
#! @param this The document to read.
|
||||
#! @param at The index of the value.
|
||||
#! @returns Its kind.
|
||||
def kind_of(&this, at: u32): kind;
|
||||
|
||||
#! Finds the first member of an object by its name.
|
||||
#! @param this The document to read.
|
||||
#! @param at The index of the object.
|
||||
#! @param name The name to look for.
|
||||
#! @returns The index of its value, or `nothing` where the object has no
|
||||
#! member of the name.
|
||||
def member(&this, at: u32, name: strview): maybe<u32>;
|
||||
|
||||
#! How many members an object has, repeats included, or how many elements an
|
||||
#! array has.
|
||||
#! @param this The document to read.
|
||||
#! @param at The index of the object or array.
|
||||
#! @returns The count, which is zero for any other kind.
|
||||
def count(&this, at: u32): u64;
|
||||
|
||||
#! An element of an array, or the value of a member of an object, by its
|
||||
#! position.
|
||||
#! @param this The document to read.
|
||||
#! @param at The index of the array or object.
|
||||
#! @param index Its position, below `count`.
|
||||
#! @returns The index of the value.
|
||||
def element(&this, at: u32, index: u64): u32;
|
||||
|
||||
#! The element of an array, or the value of a member of an object, after
|
||||
#! another, so walking them in order counts none from the first.
|
||||
#! @param this The document to read.
|
||||
#! @param at The index of the array or object.
|
||||
#! @param previous The index of the value before it.
|
||||
#! @returns The index of the value after it.
|
||||
def next_element(&this, at: u32, previous: u32): u32;
|
||||
|
||||
#! The name of the member of an object whose value is given.
|
||||
#! @param this The document to read.
|
||||
#! @param value The index of the member's value.
|
||||
#! @returns The member's name, decoded.
|
||||
def name_of(&this, value: u32): strview;
|
||||
|
||||
#! Reads a value as text.
|
||||
#! @param this The document to read.
|
||||
#! @param at The index of the value.
|
||||
#! @returns The text, decoded, or `wrong_kind` where the value is not text.
|
||||
def as_text(&this, at: u32): result<strview, error>;
|
||||
|
||||
#! Reads a value as a signed integer.
|
||||
#! @param this The document to read.
|
||||
#! @param at The index of the value.
|
||||
#! @returns The integer, or `wrong_kind` where the value is not a number, or
|
||||
#! `out_of_range` where it is not an integer `i64` holds.
|
||||
def as_i64(&this, at: u32): result<i64, error>;
|
||||
|
||||
#! Reads a value as an unsigned integer.
|
||||
#! @param this The document to read.
|
||||
#! @param at The index of the value.
|
||||
#! @returns The integer, or `wrong_kind` where the value is not a number, or
|
||||
#! `out_of_range` where it is not an integer `u64` holds.
|
||||
def as_u64(&this, at: u32): result<u64, error>;
|
||||
|
||||
#! Reads a value as a real number.
|
||||
#! @param this The document to read.
|
||||
#! @param at The index of the value.
|
||||
#! @returns The number, or `wrong_kind` where the value is not a number, or
|
||||
#! `out_of_range` where it is too large for an `f64`.
|
||||
def as_f64(&this, at: u32): result<f64, error>;
|
||||
|
||||
#! Reads a value as `true` or `false`.
|
||||
#! @param this The document to read.
|
||||
#! @param at The index of the value.
|
||||
#! @returns The value, or `wrong_kind` where it is not a boolean.
|
||||
def as_bool(&this, at: u32): result<bool, error>;
|
||||
|
||||
#! Where a value was read, for an error about it.
|
||||
#! @param this The document to read.
|
||||
#! @param at The index of the value.
|
||||
#! @returns Its place in the text.
|
||||
def place(&this, at: u32): place;
|
||||
}
|
||||
|
||||
#! A type that writes itself, in place of what `serial::encode` would write of
|
||||
#! it. It writes what it holds through `serial::encode`, so its parts are
|
||||
#! written by their own forms.
|
||||
pub trait encodable {
|
||||
#! Writes the value through a format.
|
||||
#! @param this The value to write.
|
||||
#! @param out The format to write through.
|
||||
def encode(&this, out: &mut encoder): void;
|
||||
}
|
||||
|
||||
#! A type that reads itself, in place of what `serial::decode` would read into
|
||||
#! it. It reads what it holds through `serial::decode`, giving it the options it
|
||||
#! is given, so its parts are read by their own forms, as strictly.
|
||||
pub trait decodable {
|
||||
#! Reads a value of the type from a document.
|
||||
#! @param from The document to read.
|
||||
#! @param at The index of the value in it.
|
||||
#! @param given How strictly to read, which the value's parts are read with
|
||||
#! too.
|
||||
#! @returns The value, or why it could not be read.
|
||||
def decode(from: &decoder, at: u32, given: options): result<self, error>;
|
||||
}
|
||||
@@ -0,0 +1,251 @@
|
||||
#! @file text.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains what reading and writing text formats share, whatever the format:
|
||||
#! checking and appending UTF-8, reading a number's text as an integer exactly, and
|
||||
#! finding where a byte of the text is by its line and column.
|
||||
import core::swar;
|
||||
|
||||
module std::serial;
|
||||
|
||||
|
||||
# The most an exponent is read as, past which a number is zero or too large for
|
||||
# any type anyway.
|
||||
const exponent_limit: u64 = 100000;
|
||||
|
||||
#! Whether text is UTF-8: no overlong forms, no surrogates, nothing past
|
||||
#! U+10FFFF, and nothing cut short.
|
||||
#! @param text The text to check.
|
||||
#! @returns Whether it is UTF-8.
|
||||
pub def is_utf8(text: strview): bool {
|
||||
val data := text.data;
|
||||
var at: u64 = 0;
|
||||
while at < text.length {
|
||||
if text[at] < 0x80 {
|
||||
# ASCII, which is passed a word at a time to the next byte past it.
|
||||
at++;
|
||||
while at + 8 <= text.length {
|
||||
val wide: swar::mask8 = swar::load(data + at) & 0x8080808080808080;
|
||||
if wide.any() {
|
||||
at += wide.first();
|
||||
break;
|
||||
}
|
||||
at += 8;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
val count := utf8_length(text, at);
|
||||
if count == 0 {
|
||||
return false;
|
||||
}
|
||||
at += count;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#! The length of the UTF-8 sequence beginning at a byte, or 0 where the bytes
|
||||
#! there are not one: overlong, a surrogate, past U+10FFFF, or cut short.
|
||||
#! @param text The text to read.
|
||||
#! @param at Where the sequence begins, which must be within the text.
|
||||
#! @returns How many bytes it takes, from 1 to 4, or 0.
|
||||
pub def utf8_length(text: strview, at: u64): u64 {
|
||||
val lead := text[at];
|
||||
if lead < 0x80 {
|
||||
return 1;
|
||||
}
|
||||
|
||||
# The second byte is narrowed where the lead alone would allow an overlong form, a
|
||||
# surrogate, or a character past U+10FFFF.
|
||||
var count: u64 = 0;
|
||||
var low: u8 = 0x80;
|
||||
var high: u8 = 0xBF;
|
||||
if lead >= 0xC2 && lead <= 0xDF {
|
||||
count = 2;
|
||||
} else if lead == 0xE0 {
|
||||
count = 3;
|
||||
low = 0xA0;
|
||||
} else if lead == 0xED {
|
||||
count = 3;
|
||||
high = 0x9F;
|
||||
} else if lead >= 0xE1 && lead <= 0xEF {
|
||||
count = 3;
|
||||
} else if lead == 0xF0 {
|
||||
count = 4;
|
||||
low = 0x90;
|
||||
} else if lead == 0xF4 {
|
||||
count = 4;
|
||||
high = 0x8F;
|
||||
} else if lead >= 0xF1 && lead <= 0xF3 {
|
||||
count = 4;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if at + count > text.length || text[at + 1] < low || text[at + 1] > high {
|
||||
return 0;
|
||||
}
|
||||
for index in 2..count {
|
||||
val byte := text[at + index];
|
||||
if byte < 0x80 || byte > 0xBF {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
#! Where a byte of the text is, by its line and its column, counting a line
|
||||
#! break as `\n`, `\r\n`, or a `\r` alone.
|
||||
#! @param text The text the byte is in.
|
||||
#! @param offset The byte, which is at most the text's length.
|
||||
#! @returns Where it is: its line counted from 1, and its column, the byte
|
||||
#! within the line, counted from 1.
|
||||
pub def place_of(text: strview, offset: u64): place {
|
||||
var line: u32 = 1;
|
||||
var begun: u64 = 0;
|
||||
val end := offset < text.length ? offset : text.length;
|
||||
for index in 0..end {
|
||||
val byte := text[index];
|
||||
if byte == '\n' || (byte == '\r' && (index + 1 >= text.length || text[index + 1] != '\n')) {
|
||||
line++;
|
||||
begun = index + 1;
|
||||
}
|
||||
}
|
||||
return place { offset = offset, line = line, column = (offset - begun + 1) as u32 };
|
||||
}
|
||||
|
||||
# Where a run of decimal digits beginning at `at` ends.
|
||||
def digits_end(text: strview, at: u64): u64 {
|
||||
var index := at;
|
||||
while index < text.length && text[index].is_digit {
|
||||
index++;
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
#! What a number's text is as an integer: its magnitude and sign, and whether it
|
||||
#! is one and fits in 64 bits.
|
||||
pub struct integral {
|
||||
pub var magnitude: u64; #! The integer's magnitude, where it fits.
|
||||
pub var negative: bool; #! Whether it is below zero.
|
||||
pub var whole: bool; #! Whether it has no fraction, as `1.50` has and `1.5e1` has not.
|
||||
pub var fits: bool; #! Whether its magnitude fits in 64 bits.
|
||||
}
|
||||
|
||||
#! Reads a decimal number's text as an integer exactly, as
|
||||
#! `[-+]?[0-9]*(\.[0-9]*)?([eE][-+]?[0-9]+)?` writes it, so `1.5e1` is 15.
|
||||
#! @param text The number's text, which follows that grammar and holds a
|
||||
#! digit.
|
||||
#! @returns Its magnitude and sign, whether it is whole, and whether it fits.
|
||||
pub def integral_of(text: strview): integral {
|
||||
var made := integral { whole = true, fits = true };
|
||||
var at: u64 = 0;
|
||||
if text[0] == '-' || text[0] == '+' {
|
||||
made.negative = text[0] == '-';
|
||||
at = 1;
|
||||
}
|
||||
|
||||
# The digits, with where the fraction among them begins, and the exponent.
|
||||
val start := at;
|
||||
at = digits_end(text, at);
|
||||
val integers := at - start;
|
||||
var fraction := at;
|
||||
var fractions: u64 = 0;
|
||||
if at < text.length && text[at] == '.' {
|
||||
fraction = at + 1;
|
||||
at = digits_end(text, fraction);
|
||||
fractions = at - fraction;
|
||||
}
|
||||
|
||||
var exponent: u64 = 0;
|
||||
var lowered: bool = false;
|
||||
if at < text.length {
|
||||
at++;
|
||||
if text[at] == '+' || text[at] == '-' {
|
||||
lowered = text[at] == '-';
|
||||
at++;
|
||||
}
|
||||
while at < text.length {
|
||||
exponent = exponent * 10 + text[at].digit_value;
|
||||
if exponent > exponent_limit {
|
||||
exponent = exponent_limit;
|
||||
}
|
||||
at++;
|
||||
}
|
||||
}
|
||||
|
||||
# The value is the digits times ten to the exponent, less one for each digit of
|
||||
# the fraction, so that many digits from the end are the fraction where it is
|
||||
# negative, and that many zeros follow where it is not.
|
||||
var kept := integers + fractions;
|
||||
var shift: u64 = 0;
|
||||
if lowered || exponent < fractions {
|
||||
val dropped := lowered ? exponent + fractions : fractions - exponent;
|
||||
if dropped > kept {
|
||||
made.whole = !any_but_zero(text, start, integers, fraction, fractions, 0, kept);
|
||||
return made;
|
||||
}
|
||||
made.whole = !any_but_zero(text, start, integers, fraction, fractions, kept - dropped, kept);
|
||||
kept -= dropped;
|
||||
} else {
|
||||
shift = exponent - fractions;
|
||||
}
|
||||
|
||||
for index in 0..kept {
|
||||
val digit := digit_at(text, start, integers, fraction, index);
|
||||
if made.magnitude > (0xFFFFFFFFFFFFFFFFu - digit) / 10 {
|
||||
made.fits = false;
|
||||
return made;
|
||||
}
|
||||
made.magnitude = made.magnitude * 10 + digit;
|
||||
}
|
||||
if made.magnitude == 0 {
|
||||
return made;
|
||||
}
|
||||
for _ in 0..shift {
|
||||
if made.magnitude > 0xFFFFFFFFFFFFFFFFu / 10 {
|
||||
made.fits = false;
|
||||
return made;
|
||||
}
|
||||
made.magnitude *= 10;
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
# The digit at an index among a number's digits, those of its integer part then
|
||||
# those of its fraction.
|
||||
def digit_at(text: strview, start: u64, integers: u64, fraction: u64, index: u64): u64 {
|
||||
return index < integers ? text[start + index].digit_value : text[fraction + index - integers].digit_value;
|
||||
}
|
||||
|
||||
# Whether any digit from `from` to `to` among a number's digits is not zero.
|
||||
def any_but_zero(text: strview, start: u64, integers: u64, fraction: u64, fractions: u64, from: u64, to: u64): bool {
|
||||
for index in from..to {
|
||||
if digit_at(text, start, integers, fraction, index) != 0 {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#! Appends a character as UTF-8.
|
||||
#! @param code The character, which must be no more than U+10FFFF.
|
||||
#! @param into The string to append to.
|
||||
pub def append_utf8(code: u64, into: &mut string): void {
|
||||
if code < 0x80 {
|
||||
into.push(code as u8);
|
||||
} else if code < 0x800 {
|
||||
into.push((0xC0 | (code >> 6)) as u8);
|
||||
into.push((0x80 | (code & 0x3F)) as u8);
|
||||
} else if code < 0x10000 {
|
||||
into.push((0xE0 | (code >> 12)) as u8);
|
||||
into.push((0x80 | ((code >> 6) & 0x3F)) as u8);
|
||||
into.push((0x80 | (code & 0x3F)) as u8);
|
||||
} else {
|
||||
into.push((0xF0 | (code >> 18)) as u8);
|
||||
into.push((0x80 | ((code >> 12) & 0x3F)) as u8);
|
||||
into.push((0x80 | ((code >> 6) & 0x3F)) as u8);
|
||||
into.push((0x80 | (code & 0x3F)) as u8);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,428 @@
|
||||
#! @file walk.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains `serial::encode` and `serial::decode`, which walk a value by what
|
||||
#! `@serial` marks of its type, and the forms of the types std gives: numbers,
|
||||
#! `bool`, strings, views, `maybe`, vectors, sets, and dictionaries keyed by
|
||||
#! text.
|
||||
|
||||
import std::containers;
|
||||
module std::serial;
|
||||
|
||||
|
||||
# A type whose value is left out where it is missing, as `maybe` is, rather than
|
||||
# the missing member being an error.
|
||||
trait vacant {
|
||||
def vacant(): self;
|
||||
}
|
||||
|
||||
# A key read from a member's name, as a dictionary's `string` keys are.
|
||||
trait named {
|
||||
def from(text: strview, alloc: maybe<&mut allocator>): self;
|
||||
}
|
||||
|
||||
#! Writes a value through a format.
|
||||
#!
|
||||
#! A type of its own form, one implementing `encodable`, writes itself, as std's
|
||||
#! numbers, strings, and containers do. Otherwise the type must be `@serial`:
|
||||
#! - a struct is written as an object of its marked fields, named as they are or
|
||||
#! by the `name` of their marks;
|
||||
#! - an enum as the name of its member, which must be marked;
|
||||
#! - a union as an object of one member, the marked field it holds.
|
||||
#! A value holding an unmarked member or field is written as nothing, and the
|
||||
#! format keeps `not_serial` as its failure.
|
||||
#! @tparam T The type of the value.
|
||||
#! @param value The value to write.
|
||||
#! @param out The format to write it through.
|
||||
pub def encode<T>(value: &T, out: &mut encoder): void {
|
||||
if const T is encodable {
|
||||
value.encode(out);
|
||||
return;
|
||||
}
|
||||
if const attached::<serial>(decl_of::<T>()).length == 0 {
|
||||
compile_error("The type is not `@serial`, so it is not written. Mark it, and each field to write, with `@serial`.");
|
||||
}
|
||||
|
||||
if const decl_of::<T>().kind == target::enums {
|
||||
for const member in members_of(decl_of::<T>()) {
|
||||
if const member.kind == target::members && attached::<serial>(member).length > 0 {
|
||||
if value == value_of(member) {
|
||||
out.text(attached::<serial>(member)[0].name.length > 0 ? attached::<serial>(member)[0].name : member.name);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
out.fail(error_kind::not_serial);
|
||||
out.empty();
|
||||
return;
|
||||
}
|
||||
|
||||
if const decl_of::<T>().kind == target::unions {
|
||||
for const field in members_of(decl_of::<T>()) {
|
||||
if const field.kind == target::fields && attached::<serial>(field).length > 0 {
|
||||
if value is (field) {
|
||||
out.begin_object(1);
|
||||
out.member(attached::<serial>(field)[0].name.length > 0 ? attached::<serial>(field)[0].name : field.name);
|
||||
encode(&value.(field), out);
|
||||
out.end_object();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
out.fail(error_kind::not_serial);
|
||||
out.empty();
|
||||
return;
|
||||
}
|
||||
|
||||
var count: u64 = 0;
|
||||
for const field in members_of(decl_of::<T>()) {
|
||||
if const field.kind == target::fields && attached::<serial>(field).length > 0 {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
out.begin_object(count);
|
||||
for const field in members_of(decl_of::<T>()) {
|
||||
if const field.kind == target::fields && attached::<serial>(field).length > 0 {
|
||||
out.member(attached::<serial>(field)[0].name.length > 0 ? attached::<serial>(field)[0].name : field.name);
|
||||
encode(&value.(field), out);
|
||||
}
|
||||
}
|
||||
out.end_object();
|
||||
}
|
||||
|
||||
#! Reads a value a format has read.
|
||||
#!
|
||||
#! A type of its own form, one implementing `decodable`, reads itself, as std's
|
||||
#! numbers, strings, and containers do. Otherwise the type must be `@serial`,
|
||||
#! and is read as `encode` writes it. A struct starts at its zero value, with
|
||||
#! its fields at their defaults, and each marked field is read from its member,
|
||||
#! so a field left unmarked keeps its default.
|
||||
#!
|
||||
#! A member missing for a marked field is a `missing_member` error, unless the
|
||||
#! field is a `maybe`, which is then `none`. A member no field takes is ignored,
|
||||
#! and so is a member written again after its first, unless the options are
|
||||
#! strict.
|
||||
#! @tparam T The type to read, which is `zeroable`, so a field left unread
|
||||
#! has a value.
|
||||
#! @param from The document the format read.
|
||||
#! @param at The value to read, by its index in the document.
|
||||
#! @param given How strictly to read.
|
||||
#! @returns The value, or why it could not be read, at the value that failed.
|
||||
pub def decode<T>(from: &decoder, at: u32, given: options = options {}): result<T, error> where T: zeroable {
|
||||
if const T is decodable {
|
||||
return T::decode(from, at, given);
|
||||
}
|
||||
if const attached::<serial>(decl_of::<T>()).length == 0 {
|
||||
compile_error("The type is not `@serial`, so it is not read. Mark it, and each field to read, with `@serial`.");
|
||||
}
|
||||
|
||||
if const decl_of::<T>().kind == target::enums {
|
||||
val written := from.as_text(at) else (e) { return e; };
|
||||
for const member in members_of(decl_of::<T>()) {
|
||||
if const member.kind == target::members && attached::<serial>(member).length > 0 {
|
||||
val name: strview = attached::<serial>(member)[0].name.length > 0 ? attached::<serial>(member)[0].name : member.name;
|
||||
if written == name {
|
||||
return value_of(member);
|
||||
}
|
||||
}
|
||||
}
|
||||
return error::at(error_kind::out_of_range, from.place(at));
|
||||
}
|
||||
|
||||
if const decl_of::<T>().kind == target::unions {
|
||||
if from.kind_of(at) != kind::object || from.count(at) != 1 {
|
||||
return error::at(error_kind::wrong_kind, from.place(at));
|
||||
}
|
||||
|
||||
val inner := from.element(at, 0);
|
||||
val written := from.name_of(inner);
|
||||
for const field in members_of(decl_of::<T>()) {
|
||||
if const field.kind == target::fields && attached::<serial>(field).length > 0 {
|
||||
val name: strview = attached::<serial>(field)[0].name.length > 0 ? attached::<serial>(field)[0].name : field.name;
|
||||
if written == name {
|
||||
var read := decode::<type_of(field)>(from, inner, given) else (e) { return e; };
|
||||
var made: T;
|
||||
made.(field) = move(read);
|
||||
return move(made);
|
||||
}
|
||||
}
|
||||
}
|
||||
return error::at(error_kind::out_of_range, from.place(inner));
|
||||
}
|
||||
|
||||
if from.kind_of(at) != kind::object {
|
||||
return error::at(error_kind::wrong_kind, from.place(at));
|
||||
}
|
||||
|
||||
var made: T;
|
||||
for const field in members_of(decl_of::<T>()) {
|
||||
if const field.kind == target::fields && attached::<serial>(field).length > 0 {
|
||||
val name: strview = attached::<serial>(field)[0].name.length > 0 ? attached::<serial>(field)[0].name : field.name;
|
||||
if from.member(at, name) is some(found) {
|
||||
var read := decode::<type_of(field)>(from, found, given) else (e) { return e; };
|
||||
made.(field) = move(read);
|
||||
} else {
|
||||
if const !(type_of(field) is vacant) {
|
||||
return error::at(error_kind::missing_member, from.place(at));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Strictly, every member is one a field takes, and is the first of its name.
|
||||
if given.strict {
|
||||
var item: u32 = 0;
|
||||
for index in 0..from.count(at) {
|
||||
item = index == 0 ? from.element(at, 0) : from.next_element(at, item);
|
||||
val written := from.name_of(item);
|
||||
var known := false;
|
||||
for const field in members_of(decl_of::<T>()) {
|
||||
if const field.kind == target::fields && attached::<serial>(field).length > 0 {
|
||||
val name: strview = attached::<serial>(field)[0].name.length > 0 ? attached::<serial>(field)[0].name : field.name;
|
||||
known = known || written == name;
|
||||
}
|
||||
}
|
||||
if !known {
|
||||
return error::at(error_kind::unknown_member, from.place(item));
|
||||
}
|
||||
val first := from.member(at, written) else { continue; };
|
||||
if first != item {
|
||||
return error::at(error_kind::repeated_member, from.place(item));
|
||||
}
|
||||
}
|
||||
}
|
||||
return move(made);
|
||||
}
|
||||
|
||||
def i8::encode(&this, out: &mut encoder): void { out.signed(this as i64); }
|
||||
def i16::encode(&this, out: &mut encoder): void { out.signed(this as i64); }
|
||||
def i32::encode(&this, out: &mut encoder): void { out.signed(this as i64); }
|
||||
def i64::encode(&this, out: &mut encoder): void { out.signed(this); }
|
||||
def u8::encode(&this, out: &mut encoder): void { out.unsigned(this as u64); }
|
||||
def u16::encode(&this, out: &mut encoder): void { out.unsigned(this as u64); }
|
||||
def u32::encode(&this, out: &mut encoder): void { out.unsigned(this as u64); }
|
||||
def u64::encode(&this, out: &mut encoder): void { out.unsigned(this); }
|
||||
def f32::encode(&this, out: &mut encoder): void { out.real(this as f64); }
|
||||
def f64::encode(&this, out: &mut encoder): void { out.real(this); }
|
||||
def bool::encode(&this, out: &mut encoder): void { out.boolean(this); }
|
||||
|
||||
def i8::decode(from: &decoder, at: u32, given: options): result<i8, error> {
|
||||
val read := signed_within(from, at, -128, 127) else (e) { return e; };
|
||||
return read as i8;
|
||||
}
|
||||
|
||||
def i16::decode(from: &decoder, at: u32, given: options): result<i16, error> {
|
||||
val read := signed_within(from, at, -32768, 32767) else (e) { return e; };
|
||||
return read as i16;
|
||||
}
|
||||
|
||||
def i32::decode(from: &decoder, at: u32, given: options): result<i32, error> {
|
||||
val read := signed_within(from, at, -2147483648, 2147483647) else (e) { return e; };
|
||||
return read as i32;
|
||||
}
|
||||
|
||||
def i64::decode(from: &decoder, at: u32, given: options): result<i64, error> {
|
||||
return from.as_i64(at);
|
||||
}
|
||||
|
||||
def u8::decode(from: &decoder, at: u32, given: options): result<u8, error> {
|
||||
val read := unsigned_within(from, at, 255) else (e) { return e; };
|
||||
return read as u8;
|
||||
}
|
||||
|
||||
def u16::decode(from: &decoder, at: u32, given: options): result<u16, error> {
|
||||
val read := unsigned_within(from, at, 65535) else (e) { return e; };
|
||||
return read as u16;
|
||||
}
|
||||
|
||||
def u32::decode(from: &decoder, at: u32, given: options): result<u32, error> {
|
||||
val read := unsigned_within(from, at, 4294967295) else (e) { return e; };
|
||||
return read as u32;
|
||||
}
|
||||
|
||||
def u64::decode(from: &decoder, at: u32, given: options): result<u64, error> {
|
||||
return from.as_u64(at);
|
||||
}
|
||||
|
||||
def f32::decode(from: &decoder, at: u32, given: options): result<f32, error> {
|
||||
val read := from.as_f64(at) else (e) { return e; };
|
||||
if read > 3.4028234663852886e38 || read < -3.4028234663852886e38 {
|
||||
return error::at(error_kind::out_of_range, from.place(at));
|
||||
}
|
||||
return read as f32;
|
||||
}
|
||||
|
||||
def f64::decode(from: &decoder, at: u32, given: options): result<f64, error> {
|
||||
return from.as_f64(at);
|
||||
}
|
||||
|
||||
def bool::decode(from: &decoder, at: u32, given: options): result<bool, error> {
|
||||
return from.as_bool(at);
|
||||
}
|
||||
|
||||
def string::encode(&this, out: &mut encoder): void {
|
||||
out.text(this.view());
|
||||
}
|
||||
|
||||
def string::decode(from: &decoder, at: u32, given: options): result<string, error> {
|
||||
val read := from.as_text(at) else (e) { return e; };
|
||||
return string::from(read, nothing {});
|
||||
}
|
||||
|
||||
# A view of one-byte integers is text, and any other view an array, since a view of
|
||||
# text cannot be told apart from one of bytes by its type.
|
||||
def span<T>::encode(&this, out: &mut encoder): void {
|
||||
if const size_of::<T>() == 1 && T is integer {
|
||||
out.text(strview::over(this.data as *u8, this.length));
|
||||
return;
|
||||
}
|
||||
|
||||
out.begin_array(this.length);
|
||||
for &item in this {
|
||||
encode(item, out);
|
||||
}
|
||||
out.end_array();
|
||||
}
|
||||
|
||||
def maybe<T>::encode(&this, out: &mut encoder): void {
|
||||
match this {
|
||||
some(&held) => encode(held, out);
|
||||
none => out.empty();
|
||||
}
|
||||
}
|
||||
|
||||
def maybe<T>::decode(from: &decoder, at: u32, given: options): result<maybe<T>, error> {
|
||||
if const !(T is zeroable) {
|
||||
compile_error("What a `maybe` holds is read from its zero value, so it is `zeroable`.");
|
||||
}
|
||||
if from.kind_of(at) == kind::empty {
|
||||
return maybe::<T>::vacant();
|
||||
}
|
||||
var read := decode::<T>(from, at, given) else (e) { return e; };
|
||||
var made: maybe<T> = move(read);
|
||||
return move(made);
|
||||
}
|
||||
|
||||
def maybe<T>::vacant(): maybe<T> {
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
def vector<T>::encode(&this, out: &mut encoder): void {
|
||||
out.begin_array(this.length);
|
||||
for &item in this {
|
||||
encode(item, out);
|
||||
}
|
||||
out.end_array();
|
||||
}
|
||||
|
||||
def vector<T>::decode(from: &decoder, at: u32, given: options): result<vector<T>, error> {
|
||||
if const !(T is zeroable) {
|
||||
compile_error("A vector's elements are each read from their zero value, so they are `zeroable`.");
|
||||
}
|
||||
if from.kind_of(at) != kind::array {
|
||||
return error::at(error_kind::wrong_kind, from.place(at));
|
||||
}
|
||||
|
||||
val count := from.count(at);
|
||||
var made := vector::<T>::new(count, nothing {});
|
||||
var item: u32 = 0;
|
||||
for index in 0..count {
|
||||
item = index == 0 ? from.element(at, 0) : from.next_element(at, item);
|
||||
var read := decode::<T>(from, item, given) else (e) { return e; };
|
||||
made.append(move(read));
|
||||
}
|
||||
return move(made);
|
||||
}
|
||||
|
||||
# A dictionary is an object, so its keys are text.
|
||||
def dict<K, V>::encode(&this, out: &mut encoder): void {
|
||||
if const !(K is viewable<u8>) {
|
||||
compile_error("A dictionary is written as an object, whose members are named, so its keys are text.");
|
||||
}
|
||||
|
||||
out.begin_object(this.length);
|
||||
for &entry in this {
|
||||
out.member(entry.key.view());
|
||||
encode(&entry.value, out);
|
||||
}
|
||||
out.end_object();
|
||||
}
|
||||
|
||||
def dict<K, V>::decode(from: &decoder, at: u32, given: options): result<dict<K, V>, error> {
|
||||
if const !(K is named) {
|
||||
compile_error("A dictionary is read from an object, whose members are named, so its keys are `string`s.");
|
||||
}
|
||||
if const !(V is zeroable) {
|
||||
compile_error("A dictionary's values are each read from their zero value, so they are `zeroable`.");
|
||||
}
|
||||
if from.kind_of(at) != kind::object {
|
||||
return error::at(error_kind::wrong_kind, from.place(at));
|
||||
}
|
||||
|
||||
# A member written again is read from its first, as a struct's is.
|
||||
val count := from.count(at);
|
||||
var made := dict::<K, V>::new(count, nothing {});
|
||||
var item: u32 = 0;
|
||||
for index in 0..count {
|
||||
item = index == 0 ? from.element(at, 0) : from.next_element(at, item);
|
||||
var key := K::from(from.name_of(item), nothing {});
|
||||
if made.get(&key) is some(_) {
|
||||
if given.strict {
|
||||
return error::at(error_kind::repeated_member, from.place(item));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
var read := decode::<V>(from, item, given) else (e) { return e; };
|
||||
made.set(move(key), move(read));
|
||||
}
|
||||
return move(made);
|
||||
}
|
||||
|
||||
# A set is an array of its values, in no particular order.
|
||||
def hashset<T>::encode(&this, out: &mut encoder): void {
|
||||
out.begin_array(this.length);
|
||||
for &item in this {
|
||||
encode(item, out);
|
||||
}
|
||||
out.end_array();
|
||||
}
|
||||
|
||||
# A value written twice is held once, as a set holds every value.
|
||||
def hashset<T>::decode(from: &decoder, at: u32, given: options): result<hashset<T>, error> {
|
||||
if const !(T is zeroable) {
|
||||
compile_error("A set's values are each read from their zero value, so they are `zeroable`.");
|
||||
}
|
||||
if from.kind_of(at) != kind::array {
|
||||
return error::at(error_kind::wrong_kind, from.place(at));
|
||||
}
|
||||
|
||||
val count := from.count(at);
|
||||
var made := hashset::<T>::new(count, nothing {});
|
||||
var item: u32 = 0;
|
||||
for index in 0..count {
|
||||
item = index == 0 ? from.element(at, 0) : from.next_element(at, item);
|
||||
var read := decode::<T>(from, item, given) else (e) { return e; };
|
||||
made.insert(move(read));
|
||||
}
|
||||
return move(made);
|
||||
}
|
||||
|
||||
# A signed integer read exactly, within bounds.
|
||||
def signed_within(from: &decoder, at: u32, low: i64, high: i64): result<i64, error> {
|
||||
val read := from.as_i64(at) else (e) { return e; };
|
||||
if read < low || read > high {
|
||||
return error::at(error_kind::out_of_range, from.place(at));
|
||||
}
|
||||
return read;
|
||||
}
|
||||
|
||||
# An unsigned integer read exactly, within a bound.
|
||||
def unsigned_within(from: &decoder, at: u32, high: u64): result<u64, error> {
|
||||
val read := from.as_u64(at) else (e) { return e; };
|
||||
if read > high {
|
||||
return error::at(error_kind::out_of_range, from.place(at));
|
||||
}
|
||||
return read;
|
||||
}
|
||||
Reference in new issue
Block a user