Files
rosetta/src/Formats/data/yaml/init.luau
T

78 lines
3.2 KiB
Luau

--!strict
-- data/yaml: YAML 1.2 (core schema), new code. Supports: block and flow styles, compact nested collections, plain /
-- quoted / literal / folded scalars (indentation and chomping indicators), comments, anchors and aliases (decoded as
-- shared references; an expansion-size limit guards against alias bombs), tags !!str !!int !!float !!bool !!null
-- !!map !!seq !!binary (-> buffer) !!timestamp (-> Types.Date) and verbatim / %TAG forms, multi-document streams.
-- Maps decode to Types.object (key order kept), null to Types.null. Writes block style (anchors for shared tables).
-- Not supported (Err.UNSUPPORTED): complex keys "? ", collections as keys, other tags unless opts.tags = "ignore";
-- merge keys "<<" stay ordinary keys; YAML 1.1 types (yes / no, sexagesimal, ...) are strings.
-- ------------------------------------------------------------------------------------------------ requires
local Err = require("@formats/core/Err")
local Bytes = require("@formats/core/Bytes")
local Types = require("@formats/Types")
local Parse = require("@formats/data/yaml/Parse")
local Emit = require("@formats/data/yaml/Emit")
-- ------------------------------------------------------------------------------------------------ contract
local Yaml = {
name = "yaml",
category = "data",
extensions = { "yaml", "yml" },
mime = { "application/yaml", "application/x-yaml", "text/yaml" },
deps = { "core/Err", "core/Bytes", "Types" },
caps = { decode = true, encode = true, ordered = true, multiDocument = true, anchors = true },
}
export type DecodeOptions = Parse.Options
export type EncodeOptions = Emit.Options
-- ------------------------------------------------------------------------------------------------ sniff
function Yaml.sniff(r: Bytes.Reader): number
local head = r:peek(256)
if string.find(head, "%z") then
return 0
end
if string.match(head, "^%%YAML ") then
return 0.9
end
if string.match(head, "^%-%-%-[ \r\n]") then
return 0.5
end
local line = string.match(head, "^[ \t\r\n]*([^\r\n]*)")
if line and (string.match(line, "^[%w_%-\"']+[%w_%- \"']*:[ ]") or string.match(line, "^[%w_%-]+:$") or string.match(line, "^%- ")) then
return 0.2
end
return 0
end
-- ------------------------------------------------------------------------------------------------ decode
-- decode(r, opts?) -> the first document (Types.null when there is none) | every document (opts.all) | nil, Err
function Yaml.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
local text = buffer.readstring(r.buf, r.base, r.limit - r.base)
local docs, code, msg, at = Parse.run(text, opts)
if not docs then
return nil, Err.new(code, msg, at)
end
if opts and opts.all then
return docs, nil
end
local first = docs[1]
if first == nil then
return if opts and opts.null ~= nil then opts.null else Types.null, nil
end
return first, nil
end
-- ------------------------------------------------------------------------------------------------ encode
-- encode(value, opts?) -> buffer | nil, Err (opts.all: value is an array of documents)
function Yaml.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
local s, err = Emit.encode(value, opts)
if not s then
return nil, Err.new(Err.ARG, err or "cannot encode")
end
return buffer.fromstring(s), nil
end
return Yaml