Initial Commit
This commit is contained in:
1625 files changed
+43618
No files matched your search
@@ -0,0 +1,278 @@
|
||||
#! @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.
|
||||
pub val defaults: bool = false; #! Whether a member missing for a marked field leaves it at its default, as a file written before its type gained the field reads, rather than being an error.
|
||||
}
|
||||
|
||||
#! 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>;
|
||||
}
|
||||
Reference in new issue
Block a user