|
| 1 | +local plugin_validate = require("crazywall.validate") |
| 2 | + |
| 3 | +---@class PluginContext |
| 4 | +---@field output_style "both"|"planonly"|"textonly" |
| 5 | +---@field on_unsaved "warn"|"write" |
| 6 | +---@field src_path_str string |
| 7 | +---@field dest_path_str string |
| 8 | +local PluginContext = {} |
| 9 | +PluginContext["output_options"] = { "both", "planonly", "textonly" } |
| 10 | +PluginContext["on_unsaved_options"] = { "warn", "write" } |
| 11 | + |
| 12 | +---@param output_style "both"|"planonly"|"textonly" |
| 13 | +---@param on_unsaved "warn"|"write" |
| 14 | +---@param src_path_str string |
| 15 | +---@param dest_path_str string |
| 16 | +---@return PluginContext? ctx |
| 17 | +---@return string? errmsg |
| 18 | +function PluginContext:new(output_style, on_unsaved, src_path_str, dest_path_str) |
| 19 | + self = {} |
| 20 | + setmetatable(self, PluginContext) |
| 21 | + local err = plugin_validate.string_in_list( |
| 22 | + on_unsaved, |
| 23 | + PluginContext["on_unsaved_options"] |
| 24 | + ) or plugin_validate.string_in_list( |
| 25 | + output_style, |
| 26 | + PluginContext["output_options"] |
| 27 | + ) |
| 28 | + if err then |
| 29 | + return nil, err |
| 30 | + end |
| 31 | + ---@cast self PluginContext |
| 32 | + self.output_style = output_style |
| 33 | + self.on_unsaved = on_unsaved |
| 34 | + self.src_path_str = src_path_str |
| 35 | + self.dest_path_str = dest_path_str |
| 36 | + return self |
| 37 | +end |
| 38 | + |
| 39 | +return PluginContext |
0 commit comments