Change API to use success,result return for loaders

This commit is contained in:
Talia 2024-08-20 15:18:03 +02:00
parent 62183de78c
commit 89b53d1aa2
16 changed files with 38 additions and 26 deletions

2
.editorconfig Normal file
View file

@ -0,0 +1,2 @@
[*]
indent_style = tab

3
.luarc.json Normal file
View file

@ -0,0 +1,3 @@
{
"workspace.checkThirdParty": false
}

View file

@ -21,8 +21,8 @@ function __metatable:__index(index)
return config.bind(path, self.__loaders) return config.bind(path, self.__loaders)
else else
for _, loader in ipairs(self.__loaders) do for _, loader in ipairs(self.__loaders) do
local result = loader(path) local success, result = loader(path)
if result then if success then
rawset(self, index, result) rawset(self, index, result)
return result return result
end end

View file

@ -12,8 +12,6 @@ return function(name)
name = tostring(name) .. '.cosmo' name = tostring(name) .. '.cosmo'
local text = raw(name) local text = raw(name)
if text then if text then
return assert(cosmo.compile(text, name)) return true, assert(cosmo.compile(text, name))
else
return nil
end end
end end

View file

@ -10,6 +10,6 @@ local csv = require 'streamcsv'
return function(name) return function(name)
local file = io.open(name..'.csv') local file = io.open(name..'.csv')
if file then if file then
return csv.file(file) return true, csv.file(file)
end end
end end

View file

@ -14,10 +14,8 @@ return function(name)
local file = io.open(name) local file = io.open(name)
if file then if file then
local html = discount(file:read("*a")) local html = discount(file:read("*a"))
return function() return true, function()
return html return html
end end
else
return nil
end end
end end

View file

@ -4,5 +4,5 @@
--- Loads values from environment variables --- Loads values from environment variables
-- @treturn string The value of the environment variable -- @treturn string The value of the environment variable
return function(name) return function(name)
return os.getenv(name:match("[^/].+$")) return true, os.getenv(name:match("[^/].+$"))
end end

View file

@ -11,8 +11,8 @@ local read = require 'glass.raw'
-- @treturn table JSON-Data -- @treturn table JSON-Data
-- @function load -- @function load
return function(file) return function(file)
local raw = read(file..'.json') local success, raw = read(file..'.json')
if raw then if success then
return json.decode(raw) return true, json.decode(raw)
end end
end end

View file

@ -9,5 +9,7 @@
-- @function load -- @function load
return function(name) return function(name)
local f = loadfile(name..'.lua') local f = loadfile(name..'.lua')
return f and f() or nil if f then
return true, f()
end
end end

View file

@ -1,7 +1,6 @@
--- Loader for MoonHTML files --- Loader for MoonHTML files
-- @module glass.moonhtml -- @module glass.moonhtml
local template = require 'restia.template' local template = require 'restia.template'
--- Loads and compiles a moonhtml template. --- Loads and compiles a moonhtml template.
@ -11,7 +10,7 @@ return function(name)
name = tostring(name) .. '.moonhtml' name = tostring(name) .. '.moonhtml'
local file = io.open(name) local file = io.open(name)
if file then if file then
return assert(template.loadmoon(file:read("*a"), name)) return true, assert(template.loadmoon(file:read("*a"), name))
else else
return nil return nil
end end

View file

@ -6,8 +6,10 @@
-- @function load -- @function load
return function(path) return function(path)
local f = io.open(path) local f = io.open(path)
if not f then return end if not f then
return false
end
local result = f:read("*a") local result = f:read("*a")
f:close() f:close()
return result return true, result
end end

View file

@ -24,5 +24,5 @@ return function(name)
if setfenv then if setfenv then
setfenv(template, env) setfenv(template, env)
end end
return template return true, template
end end

View file

@ -24,5 +24,5 @@ return function(name)
if setfenv then if setfenv then
setfenv(template, env) setfenv(template, env)
end end
return template return true, template
end end

View file

@ -7,6 +7,9 @@
-- @treturn function A loader function to be used with `glass.bind` -- @treturn function A loader function to be used with `glass.bind`
return function(input) return function(input)
return function(name) return function(name)
return input[name:match("[^/].+$")] local result = input[name:match("[^/].+$")]
if result then
return true, result
end
end end
end end

View file

@ -11,8 +11,11 @@ local read = require 'glass.raw'
-- @treturn table YAML-Data -- @treturn table YAML-Data
-- @function load -- @function load
return function(file) return function(file)
local raw = read(file..'.yml') or read(file..'.yaml') local success, raw = read(file..'.yml')
if raw then if not success then
return yaml.load(raw) success, raw = read(file..'.yaml')
end
if success then
return true, yaml.load(raw)
end end
end end

View file

@ -1,9 +1,11 @@
describe 'raw loader', -> describe 'raw loader', ->
before_each -> export loader = require 'glass.raw' before_each -> export loader = require 'glass.raw'
it 'loads files as plain text', -> 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', -> describe 'lua loader', ->
before_each -> export loader = require 'glass.lua' before_each -> export loader = require 'glass.lua'
it 'loads Lua files', -> 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