From 89b53d1aa2db1e55d47761daff4f0c53e04b5834 Mon Sep 17 00:00:00 2001 From: DarkWiiPlayer Date: Tue, 20 Aug 2024 15:18:03 +0200 Subject: [PATCH] Change API to use success,result return for loaders --- .editorconfig | 2 ++ .luarc.json | 3 +++ glass.lua | 4 ++-- glass/cosmo.lua | 4 +--- glass/csv.lua | 2 +- glass/discount.lua | 4 +--- glass/environment.lua | 2 +- glass/json.lua | 6 +++--- glass/lua.lua | 4 +++- glass/moonhtml.lua | 3 +-- glass/raw.lua | 6 ++++-- glass/skooma/html.lua | 2 +- glass/skooma/xml.lua | 2 +- glass/table.lua | 5 ++++- glass/yaml.lua | 9 ++++++--- spec/loaders_spec.moon | 6 ++++-- 16 files changed, 38 insertions(+), 26 deletions(-) create mode 100644 .editorconfig create mode 100644 .luarc.json diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..0b225af --- /dev/null +++ b/.editorconfig @@ -0,0 +1,2 @@ +[*] +indent_style = tab diff --git a/.luarc.json b/.luarc.json new file mode 100644 index 0000000..23b9ee2 --- /dev/null +++ b/.luarc.json @@ -0,0 +1,3 @@ +{ + "workspace.checkThirdParty": false +} \ No newline at end of file diff --git a/glass.lua b/glass.lua index 0838b47..74e89f1 100644 --- a/glass.lua +++ b/glass.lua @@ -21,8 +21,8 @@ function __metatable:__index(index) return config.bind(path, self.__loaders) else for _, loader in ipairs(self.__loaders) do - local result = loader(path) - if result then + local success, result = loader(path) + if success then rawset(self, index, result) return result end diff --git a/glass/cosmo.lua b/glass/cosmo.lua index 9f9f7fb..233b3fc 100644 --- a/glass/cosmo.lua +++ b/glass/cosmo.lua @@ -12,8 +12,6 @@ return function(name) name = tostring(name) .. '.cosmo' local text = raw(name) if text then - return assert(cosmo.compile(text, name)) - else - return nil + return true, assert(cosmo.compile(text, name)) end end diff --git a/glass/csv.lua b/glass/csv.lua index c409a8f..8604e6f 100644 --- a/glass/csv.lua +++ b/glass/csv.lua @@ -10,6 +10,6 @@ local csv = require 'streamcsv' return function(name) local file = io.open(name..'.csv') if file then - return csv.file(file) + return true, csv.file(file) end end diff --git a/glass/discount.lua b/glass/discount.lua index 5b2a109..c135b08 100644 --- a/glass/discount.lua +++ b/glass/discount.lua @@ -14,10 +14,8 @@ return function(name) local file = io.open(name) if file then local html = discount(file:read("*a")) - return function() + return true, function() return html end - else - return nil end end diff --git a/glass/environment.lua b/glass/environment.lua index 6f36860..557e92d 100644 --- a/glass/environment.lua +++ b/glass/environment.lua @@ -4,5 +4,5 @@ --- Loads values from environment variables -- @treturn string The value of the environment variable return function(name) - return os.getenv(name:match("[^/].+$")) + return true, os.getenv(name:match("[^/].+$")) end diff --git a/glass/json.lua b/glass/json.lua index b651d21..2582b5f 100644 --- a/glass/json.lua +++ b/glass/json.lua @@ -11,8 +11,8 @@ local read = require 'glass.raw' -- @treturn table JSON-Data -- @function load return function(file) - local raw = read(file..'.json') - if raw then - return json.decode(raw) + local success, raw = read(file..'.json') + if success then + return true, json.decode(raw) end end diff --git a/glass/lua.lua b/glass/lua.lua index 1b22ce0..c4c4d6b 100644 --- a/glass/lua.lua +++ b/glass/lua.lua @@ -9,5 +9,7 @@ -- @function load return function(name) local f = loadfile(name..'.lua') - return f and f() or nil + if f then + return true, f() + end end diff --git a/glass/moonhtml.lua b/glass/moonhtml.lua index 98b7557..15604d5 100644 --- a/glass/moonhtml.lua +++ b/glass/moonhtml.lua @@ -1,7 +1,6 @@ --- Loader for MoonHTML files -- @module glass.moonhtml - local template = require 'restia.template' --- Loads and compiles a moonhtml template. @@ -11,7 +10,7 @@ return function(name) name = tostring(name) .. '.moonhtml' local file = io.open(name) if file then - return assert(template.loadmoon(file:read("*a"), name)) + return true, assert(template.loadmoon(file:read("*a"), name)) else return nil end diff --git a/glass/raw.lua b/glass/raw.lua index 89d0a06..8a3641a 100644 --- a/glass/raw.lua +++ b/glass/raw.lua @@ -6,8 +6,10 @@ -- @function load return function(path) local f = io.open(path) - if not f then return end + if not f then + return false + end local result = f:read("*a") f:close() - return result + return true, result end diff --git a/glass/skooma/html.lua b/glass/skooma/html.lua index 8d35a2c..8c15ec5 100644 --- a/glass/skooma/html.lua +++ b/glass/skooma/html.lua @@ -24,5 +24,5 @@ return function(name) if setfenv then setfenv(template, env) end - return template + return true, template end diff --git a/glass/skooma/xml.lua b/glass/skooma/xml.lua index 6c0a6b6..6f27266 100644 --- a/glass/skooma/xml.lua +++ b/glass/skooma/xml.lua @@ -24,5 +24,5 @@ return function(name) if setfenv then setfenv(template, env) end - return template + return true, template end diff --git a/glass/table.lua b/glass/table.lua index 9fef0b1..41c7f42 100644 --- a/glass/table.lua +++ b/glass/table.lua @@ -7,6 +7,9 @@ -- @treturn function A loader function to be used with `glass.bind` return function(input) return function(name) - return input[name:match("[^/].+$")] + local result = input[name:match("[^/].+$")] + if result then + return true, result + end end end diff --git a/glass/yaml.lua b/glass/yaml.lua index e863be5..a2e6ee8 100644 --- a/glass/yaml.lua +++ b/glass/yaml.lua @@ -11,8 +11,11 @@ local read = require 'glass.raw' -- @treturn table YAML-Data -- @function load return function(file) - local raw = read(file..'.yml') or read(file..'.yaml') - if raw then - return yaml.load(raw) + local success, raw = read(file..'.yml') + if not success then + success, raw = read(file..'.yaml') + end + if success then + return true, yaml.load(raw) end end diff --git a/spec/loaders_spec.moon b/spec/loaders_spec.moon index 656460d..eec165d 100644 --- a/spec/loaders_spec.moon +++ b/spec/loaders_spec.moon @@ -1,9 +1,11 @@ describe 'raw loader', -> before_each -> export loader = require 'glass.raw' it 'loads files as plain text', -> - assert.same 'plain text\n', loader 'spec/fixtures/test' + assert.same {true, 'plain text\n'}, {loader 'spec/fixtures/test'} describe 'lua loader', -> before_each -> export loader = require 'glass.lua' it 'loads Lua files', -> - assert.same { foo: 'bar', tab: {} }, loader 'spec/fixtures/test' + success, result = loader 'spec/fixtures/test' + assert.true success + assert.same { foo: 'bar', tab: {} }, result