56 lines
1.6 KiB
Markdown
56 lines
1.6 KiB
Markdown
# std::yaml
|
|||
|
|
|
||
|
|
`std::yaml` reads and writes YAML for Sakura, 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`, keeping each value's line,
|
||
|
|
tag, and every key a mapping repeats; `yaml::reader` gives one event at a time,
|
||
|
|
and `yaml::writer` writes YAML in block style as it is given it. A value whose
|
||
|
|
type marks its fields with `@serial` is written as YAML and read back by those
|
||
|
|
marks, over `std::serial`. Limits on depth and aliases stop text written to
|
||
|
|
exhaust a reader.
|
||
|
|
|
||
|
|
## Using it
|
||
|
|
|
||
|
|
Require the standard library in your blossomfile, and name the module among a
|
||
|
|
target's `deps`, with the other modules of std it reads:
|
||
|
|
|
||
|
|
```yaml
|
||
|
|
requires:
|
||
|
|
- https://git.malunal.studio/sakura/std@v1.0.0
|
||
|
|
|
||
|
|
targets:
|
||
|
|
- name: app
|
||
|
|
type: program
|
||
|
|
deps: [std.yaml, std.io]
|
||
|
|
```
|
||
|
|
|
||
|
|
```sakura
|
||
|
|
import std::io;
|
||
|
|
import std::yaml;
|
||
|
|
|
||
|
|
def main(): void {
|
||
|
|
val read := yaml::parse("name: sakura\nstars: 5\n") else (_) {
|
||
|
|
io::println("That isn't YAML.");
|
||
|
|
return;
|
||
|
|
};
|
||
|
|
if read.root.get("name") is some(name) {
|
||
|
|
if name.as_text() is value(text) {
|
||
|
|
io::println(text);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
```
|
||
|
|
|
||
|
|
## Testing
|
||
|
|
|
||
|
|
From the standard library's root, where the conformance test finds its suite,
|
||
|
|
`blossom test --only std.yaml` runs them, or:
|
||
|
|
|
||
|
|
```sh
|
||
|
|
sakurac run --lib std=. tests/main.sakura tests/yaml/*.sakura
|
||
|
|
```
|
||
|
|
|
||
|
|
The conformance test reads the YAML test suite's cases, in `tests/yaml/suite`, under
|
||
|
|
its MIT license. The tests compare what YAML's documents hold with the JSON the suite
|
||
|
|
gives, so they read `std::json` too.
|