From 0b49bec8ba705111264d87fd7156e3b02df7ae7d Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Fri, 12 Sep 2025 17:45:12 -0500 Subject: [PATCH] fix(ci): cleanup typing and validation --- lua/cp/config.lua | 87 ++++++----------------------------------------- 1 file changed, 11 insertions(+), 76 deletions(-) diff --git a/lua/cp/config.lua b/lua/cp/config.lua index 8a88495..ea78176 100644 --- a/lua/cp/config.lua +++ b/lua/cp/config.lua @@ -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 ----@field snippets table ----@field hooks cp.HooksConfig - ----@class cp.PartialConfig ----@field contests? table ----@field snippets? table ----@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