diff options
Diffstat (limited to 'lua/extregs/file.lua')
| -rw-r--r-- | lua/extregs/file.lua | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/lua/extregs/file.lua b/lua/extregs/file.lua new file mode 100644 index 0000000..0dfe88c --- /dev/null +++ b/lua/extregs/file.lua @@ -0,0 +1,26 @@ +local util = require('extregs.util') +local storage = require('extregs.storage') +local M = {} + +function M.get_provider(cfg) + local function path() return util.path(cfg.path) end + return { + read = function() + local fd, err = io.open(path(), 'rb') + if not fd then error('cannot read ' .. path() .. ': ' .. tostring(err)) end + local data = fd:read('*a') + fd:close() + return storage.decode(data) + end, + write = function(_, reg) + local dest = path() + vim.fn.mkdir(vim.fn.fnamemodify(dest, ':h'), 'p') + local fd, err = io.open(dest, 'wb') + if not fd then error('cannot write ' .. dest .. ': ' .. tostring(err)) end + assert(fd:write(storage.encode(reg))) + assert(fd:close()) + return true + end, + } +end +return M |