Data formats

YAML

YAML to values and back, documents in a stream, and a Markdown file's front matter.

Version 1.0.0 · 4 functions · 2 crates

#yaml#config#front-matter#markdown

Functions

What use yaml.qwer gives a program, with the types Quillo sees. To take only some, or under other names: use yaml_parse, yaml_parse_all from yaml (Quillo 0.6).

yaml_parse(text: str) → anyor an error

YAML text as a value: maps, lists, text, numbers, booleans and null. Keys that are not text are refused.

yaml_parse_all(text: str) → listor an error

Every document of a YAML stream (separated by ---), as a list.

yaml_stringify(value: any) → stror an error

A value as YAML text.

front_matter(text: str) → anyor an error

A Markdown file's YAML front matter (between --- lines at its top) and the rest: { meta, body }.

Get it

$ quillo install yaml               # yaml.qwer into this folder$ quillo install yaml --example     # and its example, yaml.qwe

Or with curl:

$ curl -O https://quillolang.org/plugins/files/yaml.qwer$ curl -O https://quillolang.org/plugins/files/yaml.qwe

Then, in your program, all of it or only what you call:

main.qwe
use yaml.qweruse yaml_parse, yaml_parse_all from yaml      // Quillo 0.6: only these

It uses these crates, which cargo fetches the first time the plugin is built:

[dependencies]
serde_yaml = "0.9"serde = "1"

Example

yaml.qwe
// A config file in YAML, and a blog post's front matter.////   quillo run yaml.qwe      then open http://localhost:8080/configuse "yaml.qwer"let config_text = `server:  port: 8080  hosts: [quillolang.org, www.quillolang.org]features:  blog: true  search: false`route GET /config = yaml_parse(config_text)?route GET /post = front_matter(`---title: Hellotags: [news, release]---The post's text.`)?route GET /back = yaml_stringify({ name: "Quillo", plugins: ["yaml", "csv"] })?
$ quillo run yaml.qwe$ curl 'localhost:8080/config'$ curl 'localhost:8080/post'$ curl 'localhost:8080/back'

Source

yaml.qwer
//! YAML to values and back: config files, front matter, Kubernetes and OpenAPI documents.//! version: 1.0.0//!//! [dependencies]//! serde_yaml = "0.9"//! serde = "1"use serde::Deserialize;use serde_json::Value;/// YAML text as a value: maps, lists, text, numbers, booleans and null. Keys that are not text are refused.pub fn yaml_parse(text: &str) -> Result<Value, String> {    serde_yaml::from_str::<Value>(text).map_err(|e| e.to_string())}/// Every document of a YAML stream (separated by `---`), as a list.pub fn yaml_parse_all(text: &str) -> Result<Vec<Value>, String> {    serde_yaml::Deserializer::from_str(text).map(|d| Value::deserialize(d).map_err(|e| e.to_string())).collect()}/// A value as YAML text.pub fn yaml_stringify(value: serde_json::Value) -> Result<String, String> {    serde_yaml::to_string(&value).map_err(|e| e.to_string())}/// A Markdown file's YAML front matter (between `---` lines at its top) and the rest: `{ meta, body }`.pub fn front_matter(text: &str) -> Result<Value, String> {    let t = text.trim_start_matches('\u{feff}');    let Some(rest) = t.strip_prefix("---\n").or_else(|| t.strip_prefix("---\r\n")) else {        return Ok(serde_json::json!({ "meta": {}, "body": text }));    };    let end = rest.find("\n---").ok_or("the front matter has no closing `---` line")?;    let meta = if rest[..end].trim().is_empty() { serde_json::json!({}) } else { yaml_parse(&rest[..end])? };    let body = rest[end + 4..].trim_start_matches(['\r', '\n']).to_string();    Ok(serde_json::json!({ "meta": meta, "body": body }))}
Thread
77Useful? Like it, or add to its thread below.

Thread 0 posts

On YAML: questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.