feat: context, not config

This commit is contained in:
Barrett Ruth 2025-09-24 18:21:34 -04:00
parent a0171ee81e
commit 9e84d57b8a
15 changed files with 209 additions and 328 deletions

View file

@ -1,3 +1,26 @@
---@class cp.State
---@field get_platform fun(): string?
---@field set_platform fun(platform: string)
---@field get_contest_id fun(): string?
---@field set_contest_id fun(contest_id: string)
---@field get_problem_id fun(): string?
---@field set_problem_id fun(problem_id: string)
---@field get_test_cases fun(): table[]?
---@field set_test_cases fun(test_cases: table[])
---@field is_run_panel_active fun(): boolean
---@field set_run_panel_active fun(active: boolean)
---@field get_saved_session fun(): table?
---@field set_saved_session fun(session: table)
---@field get_context fun(): {platform: string?, contest_id: string?, problem_id: string?}
---@field has_context fun(): boolean
---@field reset fun()
---@field get_base_name fun(): string?
---@field get_source_file fun(language?: string): string?
---@field get_binary_file fun(): string?
---@field get_input_file fun(): string?
---@field get_output_file fun(): string?
---@field get_expected_file fun(): string?
local M = {}
local state = {
@ -65,6 +88,62 @@ function M.get_context()
}
end
function M.get_base_name()
if not state.contest_id then
return nil
end
local config_module = require('cp.config')
local config = config_module.get_config()
if config.filename then
return config.filename(state.platform or '', state.contest_id, state.problem_id, config)
else
return config_module.default_filename(state.contest_id, state.problem_id)
end
end
function M.get_source_file(language)
local base_name = M.get_base_name()
if not base_name or not state.platform then
return nil
end
local config = require('cp.config').get_config()
local contest_config = config.contests[state.platform]
if not contest_config then
return nil
end
local target_language = language or contest_config.default_language
local language_config = contest_config[target_language]
if not language_config or not language_config.extension then
return nil
end
return base_name .. '.' .. language_config.extension
end
function M.get_binary_file()
local base_name = M.get_base_name()
return base_name and ('build/%s.run'):format(base_name) or nil
end
function M.get_input_file()
local base_name = M.get_base_name()
return base_name and ('io/%s.cpin'):format(base_name) or nil
end
function M.get_output_file()
local base_name = M.get_base_name()
return base_name and ('io/%s.cpout'):format(base_name) or nil
end
function M.get_expected_file()
local base_name = M.get_base_name()
return base_name and ('io/%s.expected'):format(base_name) or nil
end
function M.has_context()
return state.platform and state.contest_id
end