fix(ci): cleanup typing and validation

This commit is contained in:
Barrett Ruth 2025-09-12 17:45:12 -05:00
parent b116a9b4f3
commit 0b49bec8ba

View file

@ -1,28 +1,7 @@
---@class cp.ContestConfig
---@field cpp_version number
---@field compile_flags string[]
---@field debug_flags string[]
---@field timeout_ms number
---@class cp.PartialContestConfig
---@field cpp_version? number
---@field compile_flags? string[]
---@field debug_flags? string[]
---@field timeout_ms? number
---@class cp.HooksConfig
---@field before_run? function
---@field before_debug? function
---@class cp.Config
---@field contests table<string, cp.ContestConfig>
---@field snippets table<string, any>
---@field hooks cp.HooksConfig
---@class cp.PartialConfig
---@field contests? table<string, cp.PartialContestConfig>
---@field snippets? table<string, any>
---@field hooks? cp.HooksConfig
---@field contests table
---@field snippets table
---@field hooks table
local M = {}
@ -52,9 +31,9 @@ M.defaults = {
},
}
---@param base_config cp.ContestConfig
---@param contest_config cp.PartialContestConfig
---@return cp.ContestConfig
---@param base_config table
---@param contest_config table
---@return table
local function extend_contest_config(base_config, contest_config)
local result = vim.tbl_deep_extend("force", base_config, contest_config)
@ -65,69 +44,25 @@ local function extend_contest_config(base_config, contest_config)
return result
end
---@param path string
---@param tbl table
---@return boolean is_valid
---@return string|nil error_message
local function validate_path(path, tbl)
local ok, err = pcall(vim.validate, tbl)
return ok, err and path .. "." .. err
end
---@param path string
---@param contest_config table|nil
---@return boolean is_valid
---@return string|nil error_message
local function validate_contest_config(path, contest_config)
if not contest_config then
return true, nil
end
return validate_path(path, {
cpp_version = { contest_config.cpp_version, "number", true },
compile_flags = { contest_config.compile_flags, "table", true },
debug_flags = { contest_config.debug_flags, "table", true },
timeout_ms = { contest_config.timeout_ms, "number", true },
})
end
---@param user_config cp.PartialConfig|nil
---@return cp.Config
---@param user_config table|nil
---@return table
function M.setup(user_config)
local ok, err = validate_path("config", {
vim.validate({
user_config = { user_config, { "table", "nil" }, true },
})
if not ok then
error(err)
end
if user_config then
ok, err = validate_path("config", {
vim.validate({
contests = { user_config.contests, { "table", "nil" }, true },
snippets = { user_config.snippets, { "table", "nil" }, true },
hooks = { user_config.hooks, { "table", "nil" }, true },
})
if not ok then
error(err)
end
if user_config.hooks then
ok, err = validate_path("config.hooks", {
vim.validate({
before_run = { user_config.hooks.before_run, { "function", "nil" }, true },
before_debug = { user_config.hooks.before_debug, { "function", "nil" }, true },
})
if not ok then
error(err)
end
end
if user_config.contests then
for contest_name, contest_config in pairs(user_config.contests) do
ok, err = validate_contest_config("config.contests." .. contest_name, contest_config)
if not ok then
error(err)
end
end
end
end