summaryrefslogtreecommitdiff
path: root/db.lua
diff options
context:
space:
mode:
authorPhilipp Gesang <phg@phi-gamma.net>2018-09-08 23:52:19 +0200
committerPhilipp Gesang <phg@phi-gamma.net>2018-09-08 23:52:22 +0200
commit1aaa67daf4db00bb3a6e494c40b2fbe375ec4f56 (patch)
tree56d7b0752769f07fb1239e6fab4e1a85cdd0ecae /db.lua
parenta5a6a295e07fd47d6f59d663cde38371dd878d89 (diff)
downloadcaldr-1aaa67daf4db00bb3a6e494c40b2fbe375ec4f56.tar.gz
db: add json writer functionality
This introduces a dependency on the cjson library.
Diffstat (limited to 'db.lua')
-rw-r--r--db.lua43
1 files changed, 43 insertions, 0 deletions
diff --git a/db.lua b/db.lua
new file mode 100644
index 0000000..4e7434a
--- /dev/null
+++ b/db.lua
@@ -0,0 +1,43 @@
+--[[--
+
+ JSON file database.
+
+--]]--
+
+local inspect = require "ext.inspect"
+
+local cjson = require "cjson"
+
+local pcall = pcall
+
+local io = require "io"
+local iostdout = io.stdout
+
+local common = require "common"
+local common_status = common.status
+local status_ok = common_status.codes.ok
+local status_error = common_status.codes.error
+local println = common.println
+local errorln = common.errorln
+local noiseln = common.noiseln
+local debugln = common.debugln
+
+-------------------------------------------------------------------------------
+--- API
+-------------------------------------------------------------------------------
+
+local output_calendar = function (cal, fh)
+ fh = fh or iostdout
+ local jsenc = cjson.new ()
+ local ok, str = pcall (jsenc.encode, cal)
+ if ok == false then
+ errorln ("todb: failed to serialize calendar: %s", str)
+ return status_error
+ end
+ fh:write (str)
+ return status_ok
+end
+
+return { output_calendar = output_calendar
+ }
+