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)
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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

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

View file

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

View file

@ -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

View file

@ -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

View file

@ -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