@@ -174,39 +174,51 @@ configuration_loader::find_and_load_config_file_for_current_directory() {
174
174
configuration_or_error
175
175
configuration_loader::find_and_load_config_file_in_directory_and_ancestors (
176
176
canonical_path&& parent_directory, const char * input_path) {
177
- found_config_file found = this ->find_config_file_in_directory_and_ancestors (
178
- std::move (parent_directory));
179
- if (!found.error .empty ()) {
180
- return configuration_or_error (std::move (found.error ));
181
- }
182
- if (!found.path .has_value ()) {
183
- return configuration_or_error (&this ->default_config_ );
184
- }
185
- canonical_path& config_path = *found.path ;
186
- if (input_path) {
187
- for (watched_path& watch : this ->watched_paths_ ) {
188
- if (watch.input_path == input_path) {
189
- watch.config_path = config_path;
190
- }
191
- }
192
- }
177
+ return boost::leaf::try_handle_all (
178
+ [&]() -> boost::leaf::result<configuration_or_error> {
179
+ boost::leaf::result<found_config_file> found =
180
+ this ->find_config_file_in_directory_and_ancestors (
181
+ std::move (parent_directory));
182
+ if (!found) return found.error ();
183
+ if (!found->path .has_value ()) {
184
+ return configuration_or_error (&this ->default_config_ );
185
+ }
186
+ canonical_path& config_path = *found->path ;
187
+ if (input_path) {
188
+ for (watched_path& watch : this ->watched_paths_ ) {
189
+ if (watch.input_path == input_path) {
190
+ watch.config_path = config_path;
191
+ }
192
+ }
193
+ }
193
194
194
- if (found. already_loaded ) {
195
- return configuration_or_error (&found. already_loaded ->config );
196
- }
195
+ if (found-> already_loaded ) {
196
+ return configuration_or_error (&found-> already_loaded ->config );
197
+ }
197
198
198
- auto [config_it, inserted] = this ->loaded_config_files_ .emplace (
199
- std::piecewise_construct, std::forward_as_tuple (config_path),
200
- std::forward_as_tuple ());
201
- QLJS_ASSERT (inserted);
202
- loaded_config_file* config_file = &config_it->second ;
203
- config_file->file_content = std::move (found.file_content );
204
- config_file->config .set_config_file_path (std::move (config_path));
205
- config_file->config .load_from_json (&config_file->file_content );
206
- return configuration_or_error (&config_file->config );
199
+ auto [config_it, inserted] = this ->loaded_config_files_ .emplace (
200
+ std::piecewise_construct, std::forward_as_tuple (config_path),
201
+ std::forward_as_tuple ());
202
+ QLJS_ASSERT (inserted);
203
+ loaded_config_file* config_file = &config_it->second ;
204
+ config_file->file_content = std::move (found->file_content );
205
+ config_file->config .set_config_file_path (std::move (config_path));
206
+ config_file->config .load_from_json (&config_file->file_content );
207
+ return configuration_or_error (&config_file->config );
208
+ },
209
+ make_read_file_error_handlers ([](std::string&& message) {
210
+ return configuration_or_error (std::move (message));
211
+ }),
212
+ [](boost::leaf::e_errno error) {
213
+ return configuration_or_error (std::strerror (error.value ));
214
+ },
215
+ [&]() {
216
+ QLJS_ASSERT (false );
217
+ return configuration_or_error (" unknown error" );
218
+ });
207
219
}
208
220
209
- configuration_loader::found_config_file
221
+ boost::leaf::result< configuration_loader::found_config_file>
210
222
configuration_loader::find_config_file_in_directory_and_ancestors (
211
223
canonical_path&& parent_directory) {
212
224
// TODO(strager): Cache directory->config to reduce lookups in cases like the
@@ -229,54 +241,29 @@ configuration_loader::find_config_file_in_directory_and_ancestors(
229
241
.path = std::move (config_path),
230
242
.already_loaded = config_file,
231
243
.file_content = padded_string (),
232
- .error = std::string (),
233
244
};
234
245
}
235
246
236
- std::optional<found_config_file> found = boost::leaf::try_handle_all (
237
- [&]() -> boost::leaf::result<std::optional<found_config_file>> {
238
- boost::leaf::result<padded_string> config_json =
239
- this ->fs_ ->read_file (config_path);
240
- if (!config_json) return config_json.error ();
241
- return found_config_file{
242
- .path = std::move (config_path),
243
- .already_loaded = nullptr ,
244
- .file_content = std::move (*config_json),
245
- .error = std::string (),
246
- };
247
- },
248
- make_file_not_found_handler ([]() -> std::optional<found_config_file> {
249
- // Loop, looking for a different file.
250
- return std::nullopt;
251
- }),
252
- make_read_file_error_handlers (
253
- [&](std::string&& message) -> std::optional<found_config_file> {
254
- return found_config_file{
247
+ boost::leaf::result<std::optional<found_config_file>> found =
248
+ boost::leaf::try_handle_some (
249
+ [&]() -> boost::leaf::result<std::optional<found_config_file>> {
250
+ boost::leaf::result<padded_string> config_json =
251
+ this ->fs_ ->read_file (config_path);
252
+ if (!config_json) return config_json.error ();
253
+ return std::optional<found_config_file>(found_config_file{
255
254
.path = std::move (config_path),
256
255
.already_loaded = nullptr ,
257
- .file_content = padded_string (),
258
- .error = std::move (message),
259
- };
260
- }),
261
- [&](boost::leaf::e_errno error) -> std::optional<found_config_file> {
262
- return found_config_file{
263
- .path = std::move (config_path),
264
- .already_loaded = nullptr ,
265
- .file_content = padded_string (),
266
- .error = std::strerror (error.value ),
267
- };
268
- },
269
- [&]() {
270
- QLJS_ASSERT (false );
271
- return found_config_file{
272
- .path = std::move (config_path),
273
- .already_loaded = nullptr ,
274
- .file_content = padded_string (),
275
- .error = " unknown error" ,
276
- };
277
- });
278
- if (found.has_value ()) {
279
- return std::move (*found);
256
+ .file_content = std::move (*config_json),
257
+ });
258
+ },
259
+ make_file_not_found_handler (
260
+ []() -> std::optional<found_config_file> {
261
+ // Loop, looking for a different file.
262
+ return std::nullopt;
263
+ }));
264
+ if (!found) return found.error ();
265
+ if (found->has_value ()) {
266
+ return std::move (**found);
280
267
}
281
268
282
269
// Loop, looking for a different file.
@@ -293,7 +280,6 @@ configuration_loader::find_config_file_in_directory_and_ancestors(
293
280
.path = std::nullopt,
294
281
.already_loaded = nullptr ,
295
282
.file_content = padded_string (),
296
- .error = std::string (),
297
283
};
298
284
}
299
285
@@ -364,30 +350,56 @@ std::vector<configuration_change> configuration_loader::refresh() {
364
350
if (!parent_directory.has_value ()) {
365
351
continue ;
366
352
}
367
- found_config_file latest =
368
- this ->find_config_file_in_directory_and_ancestors (
369
- std::move (*parent_directory).canonical ());
370
- if (!latest.error .empty ()) {
371
- if (watch.error != latest.error ) {
372
- watch.error = std::move (latest.error );
373
- changes.emplace_back (configuration_change{
374
- .watched_path = &input_path,
375
- .config = &this ->default_config_ ,
376
- .token = watch.token ,
353
+ std::optional<found_config_file> latest = boost::leaf::try_handle_all (
354
+ [&]() -> boost::leaf::result<std::optional<found_config_file>> {
355
+ boost::leaf::result<found_config_file> found =
356
+ this ->find_config_file_in_directory_and_ancestors (
357
+ std::move (*parent_directory).canonical ());
358
+ if (!found) return found.error ();
359
+ return std::optional<found_config_file>(std::move (*found));
360
+ },
361
+ make_read_file_error_handlers (
362
+ [&](std::string&& message) -> std::optional<found_config_file> {
363
+ if (watch.error != message) {
364
+ watch.error = std::move (message);
365
+ changes.emplace_back (configuration_change{
366
+ .watched_path = &input_path,
367
+ .config = &this ->default_config_ ,
368
+ .token = watch.token ,
369
+ });
370
+ }
371
+ return std::nullopt;
372
+ }),
373
+ [&](boost::leaf::e_errno error) -> std::optional<found_config_file> {
374
+ const char * message = std::strerror (error.value );
375
+ if (watch.error != message) {
376
+ watch.error = message;
377
+ changes.emplace_back (configuration_change{
378
+ .watched_path = &input_path,
379
+ .config = &this ->default_config_ ,
380
+ .token = watch.token ,
381
+ });
382
+ }
383
+ return std::nullopt;
384
+ },
385
+ []() -> std::optional<found_config_file> {
386
+ QLJS_ASSERT (false );
387
+ return std::nullopt;
377
388
});
378
- }
389
+ if (!latest. has_value ()) {
379
390
continue ;
380
391
}
381
392
382
- if (latest. path != watch.config_path ) {
393
+ if (latest-> path != watch.config_path ) {
383
394
configuration* config;
384
- if (latest. path .has_value ()) {
385
- auto loaded_config_it = loaded_config_files.find (*latest. path );
395
+ if (latest-> path .has_value ()) {
396
+ auto loaded_config_it = loaded_config_files.find (*latest-> path );
386
397
if (loaded_config_it == loaded_config_files.end ()) {
387
- loaded_config_file& loaded_config = loaded_config_files[*latest.path ];
388
- loaded_config.file_content = std::move (latest.file_content );
398
+ loaded_config_file& loaded_config =
399
+ loaded_config_files[*latest->path ];
400
+ loaded_config.file_content = std::move (latest->file_content );
389
401
loaded_config.config .reset ();
390
- loaded_config.config .set_config_file_path (*latest. path );
402
+ loaded_config.config .set_config_file_path (*latest-> path );
391
403
loaded_config.config .load_from_json (&loaded_config.file_content );
392
404
config = &loaded_config.config ;
393
405
} else {
@@ -401,7 +413,7 @@ std::vector<configuration_change> configuration_loader::refresh() {
401
413
.config = config,
402
414
.token = watch.token ,
403
415
});
404
- watch.config_path = latest. path ;
416
+ watch.config_path = latest-> path ;
405
417
}
406
418
}
407
419
0 commit comments