Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-10-29 06:59:07 -05:00
commit 8b9d119213
1625 files changed
+43384

No files matched your search

+217
View File
@@ -0,0 +1,217 @@
#! @file yaml.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains YAML's errors and limits, and the functions that write a value as YAML and
#! read one back by what `@serial` marks of its type, over `std::serial`.
#!
#! YAML is read as YAML 1.2.2 writes it, by its core schema: a plain scalar is empty,
#! a boolean, an integer, a float, or text by how it is written, and a quoted or block
#! scalar is always text. `yaml::parse` reads a stream of documents into a
#! `yaml::document`, `yaml::reader` gives one event at a time, and `yaml::writer`
#! writes YAML in block style as it is given it.
import std::io;
module std::yaml;
#! How deep collections may nest before reading fails with `too_deep`, unless
#! the limits given say otherwise.
pub const default_depth: u32 = 512;
#! How many nodes the aliases of a stream may stand for in all before reading
#! fails with `too_many_aliases`, unless the limits given say otherwise.
pub const default_aliases: u64 = 1000000;
#! How far reading a stream may go, against text written to exhaust a reader.
pub struct limits {
pub val depth: u32 = default_depth; #! How deep collections may nest.
pub val aliases: u64 = default_aliases; #! How many nodes aliases may stand for, in all.
}
#! Why YAML could not be read or written.
pub enum error_kind: u8 {
unexpected_end, #! The text ends where something more must follow.
unexpected_byte, #! A byte stands where YAML allows none of its kind.
invalid_utf8, #! The text is not UTF-8.
invalid_character, #! The text holds a character YAML does not allow, as a control character.
invalid_escape, #! A double-quoted scalar holds an escape YAML has not.
tab_indent, #! A tab indents a line, where only spaces may.
wrong_indent, #! A line is indented where nothing open can go on.
long_key, #! An implicit key runs over its line, or past 1,024 characters.
missing_colon, #! A key that must be one, as a line of a block mapping is, has no `:` after it.
unknown_anchor, #! An alias names an anchor not given before it.
recursive_alias, #! An alias stands within the node it names.
too_many_aliases, #! The aliases stand for more nodes than the limit.
invalid_directive, #! A directive is written wrongly, or given twice.
unsupported_version, #! The `%YAML` directive names a version this does not read.
unknown_handle, #! A tag's handle is not given by a `%TAG` directive.
mistagged, #! A scalar's tag names a kind its text is not written as.
too_deep, #! Collections nest deeper than the limit.
too_long, #! The text is longer than the 4 GiB a document holds.
more_documents, #! A stream holds more than the one document asked for.
not_written, #! The stream the text was passed on to failed, as its own error says.
# What `std::serial` finds reading and writing typed values, as its own kinds say.
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`.
}
#! Why YAML could not be read or written, and where in the text.
#!
#! An error is plain data, so `else (e)` names it. One from writing has no
#! place, and is at line 0.
pub struct error {
pub var kind: error_kind; #! What went wrong.
pub var offset: u64; #! The byte of the text the error is at.
pub var line: u32; #! Counted from 1.
pub var column: u32; #! The byte within the line, counted from 1.
}
#! Says what the error is, in a sentence, after where it is.
#! @param this The error to describe.
#! @returns The description, as `4:3: A tab indents the line, where only spaces
#! may.`
pub def error::describe(&this): string {
if this.line == 0 {
return string::from(describe(this.kind), nothing {});
}
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 {
unexpected_end => "The text ends where something more must follow.",
unexpected_byte => "A byte stands where YAML allows none of its kind.",
invalid_utf8 => "The text is not UTF-8.",
invalid_character => "The text holds a character YAML does not allow.",
invalid_escape => "A double-quoted scalar holds an escape YAML does not have.",
tab_indent => "A tab indents the line, where only spaces may.",
wrong_indent => "The line is indented where nothing open can go on.",
long_key => "An implicit key runs over its line, or past 1,024 characters.",
missing_colon => "A key has no `:` after it.",
unknown_anchor => "An alias names an anchor not given before it.",
recursive_alias => "An alias stands within the node it names.",
too_many_aliases => "The aliases stand for more nodes than the limit allows.",
invalid_directive => "A directive is written wrongly, or given twice.",
unsupported_version => "The `%YAML` directive names a version this does not read.",
unknown_handle => "A tag's handle is not given by a `%TAG` directive.",
mistagged => "A scalar's tag names a kind its text is not written as.",
too_deep => "Collections nest deeper than the limit the text is read with.",
too_long => "The text is longer than the 4 GiB a document holds.",
more_documents => "The stream holds more than one document.",
not_written => "The stream the text was written to failed, as its own error says.",
wrong_kind => serial::describe(serial::error_kind::wrong_kind),
missing_member => serial::describe(serial::error_kind::missing_member),
unknown_member => serial::describe(serial::error_kind::unknown_member),
repeated_member => serial::describe(serial::error_kind::repeated_member),
out_of_range => serial::describe(serial::error_kind::out_of_range),
not_serial => serial::describe(serial::error_kind::not_serial),
};
}
#! The error YAML gives for one `std::serial` gives, at the same place.
#! @param problem The error reading or writing a typed value gave.
#! @returns The error.
pub def error::from(problem: &serial::error): error {
return error {
kind = of_serial(problem.kind),
offset = problem.offset,
line = problem.line,
column = problem.column
};
}
# The kind of YAML's error for a kind of `std::serial`'s.
def of_serial(kind: serial::error_kind): error_kind {
return match kind {
wrong_kind => error_kind::wrong_kind,
missing_member => error_kind::missing_member,
unknown_member => error_kind::unknown_member,
repeated_member => error_kind::repeated_member,
out_of_range => error_kind::out_of_range,
not_serial => error_kind::not_serial,
};
}
# An error of a kind at a byte of the text.
def error_at(kind: error_kind, text: strview, offset: u64): error {
val at := serial::place_of(text, offset);
return error { kind = kind, offset = offset, line = at.line, column = at.column };
}
#! Writes a typed value through a writer, by what `@serial` marks of its type,
#! or the form of its own the type gives, among whatever else the writer is
#! given.
#! @tparam T The type of the value.
#! @param value The value to write.
#! @param out The writer to write it with.
pub def encode<T>(value: &T, out: &mut writer): void {
serial::encode(value, out);
}
#! Writes a typed value as YAML text, by what `@serial` marks of its type.
#! @tparam T The type of the value.
#! @param value The value to write.
#! @param indent Spaces for each level of nesting.
#! @param alloc An optional allocator for the text to take memory from.
#! @returns The text, or the first failure in the value, as an enum member or
#! union field that is not marked.
pub def to_text<T>(value: &T, indent: u32 = 2, alloc: maybe<&mut allocator> = nothing {}): result<string, error> {
var out := writer::new(indent, alloc);
serial::encode(value, &mut out);
_ = out.finish() else (e) { return e; };
return move(out._text);
}
#! Writes a typed value as YAML text into a stream, by what `@serial` marks of
#! its type, passing the text on as it goes rather than holding it all.
#! @tparam T The type of the value.
#! @param value The value to write.
#! @param out The stream to write to, which is not flushed: whoever made it
#! does that.
#! @param indent Spaces for each level of nesting.
#! @returns Nothing, or the first failure in the value, or `not_written` where
#! the stream failed, as its own error then says.
pub def write_to<T>(value: &T, out: &mut writable, indent: u32 = 2): result<void, error> {
var w := writer::to(out, indent);
serial::encode(value, &mut w);
_ = w.finish() else (e) { return e; };
return;
}
#! Reads a typed value from a value of a document, by what `@serial` marks of
#! its type, or the form of its own the type gives.
#! @tparam T The type to read.
#! @param from The value to read.
#! @param given How strictly to read: strictly, a member no field takes, and a
#! member written more than once, are errors.
#! @returns The value, or why it could not be read, at the value that failed.
pub def decode<T>(from: value, given: serial::options = serial::options {}): result<T, error> where T: zeroable {
var made := serial::decode::<T>(&from._document[0], from._at, given) else (e) { return error::from(&e); };
return move(made);
}
#! Reads a typed value from YAML text holding one document, by what `@serial`
#! marks of its type.
#! @tparam T The type to read.
#! @param text The text to read.
#! @param given How strictly to read.
#! @returns The value, or why the text is not YAML, holds more than one
#! document, or could not be read as the type.
pub def from_text<T>(text: strview, given: serial::options = serial::options {}): result<T, error> where T: zeroable {
val read := parse(text) else (e) { return e; };
if read.document_count > 1 {
return error_at(error_kind::more_documents, text, read.document_start(1));
}
var made := decode::<T>(read.root, given) else (e) { return e; };
return move(made);
}