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

71 lines
3.0 KiB
Luau

--!strict
-- data/toml: TOML 1.0.0 (https://toml.io/en/v1.0.0). New code. Supports: decode of the whole 1.0 grammar (tables,
-- inline tables, arrays of tables, dotted keys, every string kind, integers in every base with the 64-bit range
-- check, floats incl. inf / nan, booleans, offset / local datetimes, local dates and times) with the validity rules
-- (redefinitions, UTF-8, control characters, number grammar), tested against toml-test; encode from plain tables /
-- Types.object (key order kept) / Types.Date. Tables decode to Types.object values (key order kept), datetimes to
-- Types.Date. Not supported: TOML 1.1 additions; the integer / float distinction is lost (Luau numbers: integral
-- values are written as integers).
-- ------------------------------------------------------------------------------------------------ requires
local Err = require("@formats/core/Err")
local Bytes = require("@formats/core/Bytes")
local Decode = require("@formats/data/toml/Decode")
local Encode = require("@formats/data/toml/Encode")
-- ------------------------------------------------------------------------------------------------ contract
local Toml = {
name = "toml",
category = "data",
extensions = { "toml" },
mime = { "application/toml" },
deps = { "core/Err", "core/Bytes", "Types", "codecs/Json" },
caps = { decode = true, encode = true, ordered = true, dates = true },
}
export type DecodeOptions = Decode.Options
export type EncodeOptions = Encode.Options
-- ------------------------------------------------------------------------------------------------ sniff
-- text that starts (after comments / blank lines) with a [table] header or a key = value line; INI looks the same,
-- so this stays low and the extension decides
function Toml.sniff(r: Bytes.Reader): number
local head = r:peek(512)
if string.find(head, "%z") then
return 0
end
for line in string.gmatch(head, "[^\n]+") do
local l = string.match(line, "^[ \t]*(.-)[ \t\r]*$") :: string
if l ~= "" and string.sub(l, 1, 1) ~= "#" then
if string.match(l, "^%[%[?[%w_%-%.\"' ]+%]%]?$") or string.match(l, "^[%w_%-%.\"']+[ \t]*=[ \t]*[\"'%[{%dtf%+%-in]") then
return 0.2
end
return 0
end
end
return 0
end
-- ------------------------------------------------------------------------------------------------ decode
function Toml.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
local s = buffer.readstring(r.buf, r.base, r.limit - r.base)
local v, msg, at = Decode.decode(s, opts)
if v == nil then
local m = tostring(msg)
local code = if string.find(m, "unterminated", 1, true) then Err.TRUNCATED else Err.CORRUPT
return nil, Err.new(code, m, at)
end
return v, nil
end
-- ------------------------------------------------------------------------------------------------ encode
function Toml.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
local s, msg = Encode.encode(value, opts)
if not s then
return nil, Err.new(Err.ARG, msg or "cannot encode")
end
return buffer.fromstring(s), nil
end
return Toml