-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtier.lua
More file actions
49 lines (38 loc) · 1.19 KB
/
tier.lua
File metadata and controls
49 lines (38 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
local format = require"format"
local tier = require"tier.core"
local util = require"tier.util"
tier.primitive = require"tier.primitive"
tier.standard = require"tier.standard"
--Convinience function for tier single value
tier.writer = format.writer
function tier.encode(stream, value, mapping, ...)
if mapping == nil then
mapping = tier.standard.dynamic
end
assert(util.ismapping(mapping))
assert(util.isoutputstream(stream))
local encoder = tier.encoder(tier.writer(stream))
encoder:encode(mapping, value, ...)
encoder:close()
end
function tier.encodestring(value, mapping)
local out = format.outmemorystream()
tier.encode(out, value, mapping)
return out:getdata()
end
tier.reader = format.reader
function tier.decode(stream, mapping, ...)
if mapping == nil then
mapping = tier.standard.dynamic
end
if type(stream) == "string" then
stream = format.inmemorystream(stream)
end
assert(util.isinputstream(stream))
assert(util.ismapping(mapping))
local decoder = tier.decoder(tier.reader(stream))
local val = decoder:decode(mapping, ...)
decoder:close()
return val
end
return tier