Version 1.0.0 · 4 functions · 2 crates
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 errorYAML 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 errorEvery document of a YAML stream (separated by ---), as a list.
yaml_stringify(value: any) → stror an errorA value as YAML text.
front_matter(text: str) → anyor an errorA 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.qweOr with curl:
$ curl -O https://quillolang.org/plugins/files/yaml.qwer$ curl -O https://quillolang.org/plugins/files/yaml.qweThen, 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 theseIt 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 }))}Related
.env files
dotenv.qwer · 1.0.0.env files read with their quoting rules and ${VAR} expansion, linted and compared with .env.example, and written or changed one key at a time with comments kept.8 views · 9 downloadsHCL hcl.qwer · 1.0.0HCL, the language of Terraform, Nomad and Packer files: read as values, expressions worked out from your variables, and written back.7 views · 6 downloadsINI ini.qwer · 1.0.0INI files read and written, sections and keys: php.ini, setup.cfg, .gitconfig and the settings of older software.8 views · 7 downloadsJava properties java_properties.qwer · 1.0.0Java .properties files read and written: Spring settings and translation bundles, with continued lines and \u escapes.6 views · 8 downloadsJSON5 json5.qwer · 1.0.0JSON5 and JSON with comments read as values: tsconfig.json and editor settings, with comments, trailing commas and unquoted keys.7 views · 8 downloadsMarkdown TOC markdown_toc.qwer · 1.0.0Tables of contents for Markdown: headings as a list or tree with GitHub-style anchors, a ready TOC, and HTML with heading ids.7 views · 8 downloadsThread
Thread 0 posts
On YAML: questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.