@@ -4,22 +4,22 @@ local types = require('rustaceanvim.types.internal')
44
55local M = {}
66
7- --- @param path string
8- --- @param msg string | nil
9- --- @return string
10- local function mk_error_msg (path , msg )
11- return msg and path .. ' .' .. msg or path
12- end
13-
14- --- @param path string The config path
15- --- @param tbl table The table to validate
7+ --- @param name string Argument name
8+ --- @param value unknown Argument value
9+ --- @param validator vim.validate.Validator
10+ --- - (`string|string[]`): Any value that can be returned from |lua-type()| in addition to
11+ --- `'callable'`: `'boolean'`, `'callable'`, `'function'`, `'nil'`, `'number'`, `'string'`, `'table'`,
12+ --- `'thread'`, `'userdata'`.
13+ --- - (`fun(val:any): boolean, string?`) A function that returns a boolean and an optional
14+ --- string message.
15+ --- @param optional ? boolean Argument is optional (may be omitted )
16+ --- @param message ? string message when validation fails
1617--- @see vim.validate
1718--- @return boolean is_valid
1819--- @return string | nil error_message
19- local function validate (path , tbl )
20- local prefix = ' Invalid config: '
21- local ok , err = pcall (vim .validate , tbl )
22- return ok or false , prefix .. mk_error_msg (path , err )
20+ local function validate (name , value , validator , optional , message )
21+ local ok , err = pcall (vim .validate , name , value , validator , optional , message )
22+ return ok or false , ' Rocks: Invalid config' .. (err and ' : ' .. err or ' ' )
2323end
2424
2525--- Validates the config.
@@ -28,118 +28,210 @@ end
2828--- @return string | nil error_message
2929function M .validate (cfg )
3030 local ok , err
31- ok , err = validate (' rustaceanvim' , {
32- tools = { cfg .tools , ' table' },
33- server = { cfg .server , ' table' },
34- dap = { cfg .dap , ' table' },
35- })
31+ local tools = cfg .tools
32+ ok , err = validate (' rustaceanvim.tools' , tools , ' table' )
3633 if not ok then
3734 return false , err
3835 end
39- local tools = cfg . tools
36+
4037 local crate_graph = tools .crate_graph
41- ok , err = validate (' tools.crate_graph' , {
42- backend = { crate_graph .backend , ' string' , true },
43- enabled_graphviz_backends = { crate_graph .enabled_graphviz_backends , ' table' , true },
44- full = { crate_graph .full , ' boolean' },
45- output = { crate_graph .output , ' string' , true },
46- pipe = { crate_graph .pipe , ' string' , true },
47- })
38+ ok , err = validate (' rustaceanvim.tools.crate_graph' , crate_graph , ' table' )
39+ if not ok then
40+ return false , err
41+ end
42+ ok , err = validate (' rustaceanvim.tools.crate_graph.backend' , crate_graph .backend , ' string' , true )
43+ if not ok then
44+ return false , err
45+ end
46+ ok , err = validate (
47+ ' rustaceanvim.tools.crate_graph.enabled_graphviz_backends' ,
48+ crate_graph .enabled_graphviz_backends ,
49+ ' table' ,
50+ true
51+ )
52+ if not ok then
53+ return false , err
54+ end
55+ ok , err = validate (' rustaceanvim.tools.crate_graph.full' , crate_graph .full , ' boolean' )
4856 if not ok then
4957 return false , err
5058 end
59+ ok , err = validate (' rustaceanvim.tools.crate_graph.output' , crate_graph .output , ' string' , true )
60+ if not ok then
61+ return false , err
62+ end
63+ ok , err = validate (' rustaceanvim.tools.crate_graph.pipe' , crate_graph .pipe , ' string' , true )
64+ if not ok then
65+ return false , err
66+ end
67+
5168 local code_actions = tools .code_actions
52- ok , err = validate (' tools.code_actions' , {
53- group_icon = { code_actions .group_icon , ' string' },
54- ui_select_fallback = { code_actions .ui_select_fallback , ' boolean' },
55- keys = { code_actions .keys , ' table' },
56- })
69+ ok , err = validate (' rustaceanvim.tools.code_actions' , code_actions , ' table' )
70+ if not ok then
71+ return false , err
72+ end
73+ ok , err = validate (' rustaceanvim.tools.code_actions.group_icon' , code_actions .group_icon , ' string' )
74+ if not ok then
75+ return false , err
76+ end
77+ ok , err = validate (' rustaceanvim.tools.code_actions.ui_select_fallback' , code_actions .ui_select_fallback , ' boolean' )
5778 if not ok then
5879 return false , err
5980 end
6081 local keys = code_actions .keys
61- ok , err = validate (' tools.code_actions.keys' , {
62- confirm = { keys .confirm , { ' table' , ' string' } },
63- quit = { keys .quit , { ' table' , ' string' } },
64- })
82+ ok , err = validate (' rustaceanvim.tools.code_actions.keys' , keys , ' table' )
83+ if not ok then
84+ return false , err
85+ end
86+ ok , err = validate (' rustaceanvim.tools.code_actions.keys.confirm' , keys .confirm , { ' table' , ' string' })
87+ if not ok then
88+ return false , err
89+ end
90+ ok , err = validate (' rustaceanvim.tools.code_actions.keys.quit' , keys .quit , { ' table' , ' string' })
6591 if not ok then
6692 return false , err
6793 end
6894 local float_win_config = tools .float_win_config
69- ok , err = validate (' tools.float_win_config' , {
70- auto_focus = { float_win_config .auto_focus , ' boolean' },
71- open_split = { float_win_config .open_split , ' string' },
72- })
95+ ok , err = validate (' rustaceanvim.tools.float_win_config' , float_win_config , ' table' )
96+ if not ok then
97+ return false , err
98+ end
99+ ok , err = validate (' rustaceanvim.tools.float_win_config.auto_focus' , float_win_config .auto_focus , ' boolean' )
100+ if not ok then
101+ return false , err
102+ end
103+ ok , err = validate (' rustaceanvim.tools.float_win_config.open_split' , float_win_config .open_split , ' string' )
73104 if not ok then
74105 return false , err
75106 end
76107 local rustc = tools .rustc
77- ok , err = validate (' tools.rustc' , {
78- default_edition = { rustc .default_edition , ' string' },
79- })
108+ ok , err = validate (' rustaceanvim.tools.rustc' , rustc , ' table' )
109+ if not ok then
110+ return false , err
111+ end
112+ ok , err = validate (' rustaceanvim.tools.rustc.default_edition' , rustc .default_edition , ' string' )
80113 if not ok then
81114 return false , err
82115 end
83- ok , err = validate (' tools' , {
84- executor = { tools .executor , { ' table' , ' string' } },
85- test_executor = { tools .test_executor , { ' table' , ' string' } },
86- crate_test_executor = { tools .crate_test_executor , { ' table' , ' string' } },
87- cargo_override = { tools .cargo_override , ' string' , true },
88- enable_nextest = { tools .enable_nextest , ' boolean' },
89- enable_clippy = { tools .enable_clippy , ' boolean' },
90- on_initialized = { tools .on_initialized , ' function' , true },
91- reload_workspace_from_cargo_toml = { tools .reload_workspace_from_cargo_toml , ' boolean' },
92- open_url = { tools .open_url , ' function' },
93- })
116+ ok , err = validate (' rustaceanvim.tools.executor' , tools .executor , { ' table' , ' string' })
117+ if not ok then
118+ return false , err
119+ end
120+ ok , err = validate (' rustaceanvim.tools.test_executor' , tools .test_executor , { ' table' , ' string' })
121+ if not ok then
122+ return false , err
123+ end
124+ ok , err = validate (' rustaceanvim.tools.crate_test_executor' , tools .crate_test_executor , { ' table' , ' string' })
125+ if not ok then
126+ return false , err
127+ end
128+ ok , err = validate (' rustaceanvim.tools.cargo_override' , tools .cargo_override , ' string' , true )
129+ if not ok then
130+ return false , err
131+ end
132+ ok , err = validate (' rustaceanvim.tools.enable_nextest' , tools .enable_nextest , ' boolean' )
133+ if not ok then
134+ return false , err
135+ end
136+ ok , err = validate (' rustaceanvim.tools.enable_clippy' , tools .enable_clippy , ' boolean' )
137+ if not ok then
138+ return false , err
139+ end
140+ ok , err = validate (' rustaceanvim.tools.on_initialized' , tools .on_initialized , ' function' , true )
141+ if not ok then
142+ return false , err
143+ end
144+ ok , err =
145+ validate (' rustaceanvim.tools.reload_workspace_from_cargo_toml' , tools .reload_workspace_from_cargo_toml , ' boolean' )
146+ if not ok then
147+ return false , err
148+ end
149+ ok , err = validate (' rustaceanvim.tools.open_url' , tools .open_url , ' function' , true )
94150 if not ok then
95151 return false , err
96152 end
97153 local server = cfg .server
98- ok , err = validate (' server' , {
99- cmd = { server .cmd , { ' function' , ' table' } },
100- standalone = { server .standalone , ' boolean' },
101- settings = { server .settings , { ' function' , ' table' }, true },
102- root_dir = { server .root_dir , { ' nil' , ' function' , ' string' } },
103- })
154+ ok , err = validate (' rustaceanvim.server' , server , ' table' )
155+ if not ok then
156+ return false , err
157+ end
158+ ok , err = validate (' rustaceanvim.server.cmd' , server .cmd , { ' function' , ' table' })
159+ if not ok then
160+ return false , err
161+ end
162+ ok , err = validate (' rustaceanvim.server.standalone' , server .standalone , ' boolean' )
163+ if not ok then
164+ return false , err
165+ end
166+ ok , err = validate (' rustaceanvim.server.settings' , server .settings , { ' function' , ' table' }, true )
167+ if not ok then
168+ return false , err
169+ end
170+ ok , err = validate (' rustaceanvim.server.root_dir' , server .root_dir , { ' function' , ' string' }, true )
104171 if not ok then
105172 return false , err
106173 end
107174 if type (server .settings ) == ' table' then
108- ok , err = validate (' server.settings' , {
109- [' rust-analyzer' ] = { server .settings [' rust-analyzer' ], ' table' , true },
110- })
175+ ok , err = validate (' rustaceanvim.server.settings[rust-analyzer]' , server .settings [' rust-analyzer' ], ' table' , true )
111176 if not ok then
112177 return false , err
113178 end
114179 end
180+
115181 local dap = cfg .dap
182+ ok , err = validate (' rustaceanvim.dap' , dap , ' table' )
183+ if not ok then
184+ return false , err
185+ end
186+
116187 local adapter = types .evaluate (dap .adapter )
117188 if adapter == false then
118189 ok = true
119190 elseif adapter .type == ' executable' then
120191 --- @cast adapter rustaceanvim.dap.executable.Config
121- ok , err = validate (' dap.adapter' , {
122- command = { adapter .command , ' string' },
123- name = { adapter .name , ' string' , true },
124- args = { adapter .args , ' table' , true },
125- })
192+ ok , err = validate (' rustaceanvim.dap.adapter.command [executable]' , adapter .command , ' string' )
193+ if not ok then
194+ return false , err
195+ end
196+ ok , err = validate (' rustaceanvim.dap.adapter.name [executable]' , adapter .name , ' string' , true )
197+ if not ok then
198+ return false , err
199+ end
200+ ok , err = validate (' rustaceanvim.dap.adapter.args [executable]' , adapter .args , ' table' , true )
201+ if not ok then
202+ return false , err
203+ end
126204 elseif adapter .type == ' server' then
127205 --- @cast adapter rustaceanvim.dap.server.Config
128- ok , err = validate (' dap.adapter' , {
129- command = { adapter .executable , ' table' },
130- name = { adapter .name , ' string' , true },
131- host = { adapter .host , ' string' , true },
132- port = { adapter .port , ' string' },
133- })
134- if ok then
135- ok , err = validate (' dap.adapter.executable' , {
136- command = { adapter .executable .command , ' string' },
137- args = { adapter .executable .args , ' table' , true },
138- })
206+ local executable = adapter .executable
207+ ok , err = validate (' rustaceanvim.dap.adapter.executable [server]' , executable , ' table' )
208+ if not ok then
209+ return false , err
210+ end
211+ ok , err = validate (' rustaceanvim.dap.adapter.executable.command [server]' , executable .command , ' string' )
212+ if not ok then
213+ return false , err
214+ end
215+ ok , err = validate (' rustaceanvim.dap.adapter.executable.args [server]' , executable .args , ' table' , true )
216+ if not ok then
217+ return false , err
218+ end
219+ ok , err = validate (' rustaceanvim.dap.adapter.name [server]' , adapter .name , ' string' , true )
220+ if not ok then
221+ return false , err
222+ end
223+ ok , err = validate (' rustaceanvim.dap.adapter.host [server]' , adapter .host , ' string' , true )
224+ if not ok then
225+ return false , err
226+ end
227+ ok , err = validate (' rustaceanvim.dap.adapter.port [server]' , adapter .port , ' string' )
228+ if not ok then
229+ return false , err
139230 end
140231 else
141232 ok = false
142- err = ' dap.adapter: Expected DapExecutableConfig, DapServerConfig or false'
233+ err =
234+ ' rustaceanvim.dap.adapter:\n Expected rustaceanvim.dap.executable.Config, rustaceanvim.dap.server.Config or false'
143235 end
144236 if not ok then
145237 return false , err
0 commit comments