Skip to content

Commit 2ba513f

Browse files
committed
Refactor find_config_file_in_directory_and_ancestors to return LEAF result
Switch some guts of configuration_loader over to boost LEAF. It isn't pretty, but the ugliness should go away when all of configuration_loader is using boost LEAF. This commit should not change behavior.
1 parent 4f127c7 commit 2ba513f

File tree

2 files changed

+105
-94
lines changed

2 files changed

+105
-94
lines changed

src/configuration-loader.cpp

Lines changed: 103 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -174,39 +174,51 @@ configuration_loader::find_and_load_config_file_for_current_directory() {
174174
configuration_or_error
175175
configuration_loader::find_and_load_config_file_in_directory_and_ancestors(
176176
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+
}
193194

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+
}
197198

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+
});
207219
}
208220

209-
configuration_loader::found_config_file
221+
boost::leaf::result<configuration_loader::found_config_file>
210222
configuration_loader::find_config_file_in_directory_and_ancestors(
211223
canonical_path&& parent_directory) {
212224
// 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(
229241
.path = std::move(config_path),
230242
.already_loaded = config_file,
231243
.file_content = padded_string(),
232-
.error = std::string(),
233244
};
234245
}
235246

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{
255254
.path = std::move(config_path),
256255
.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);
280267
}
281268

282269
// Loop, looking for a different file.
@@ -293,7 +280,6 @@ configuration_loader::find_config_file_in_directory_and_ancestors(
293280
.path = std::nullopt,
294281
.already_loaded = nullptr,
295282
.file_content = padded_string(),
296-
.error = std::string(),
297283
};
298284
}
299285

@@ -364,30 +350,56 @@ std::vector<configuration_change> configuration_loader::refresh() {
364350
if (!parent_directory.has_value()) {
365351
continue;
366352
}
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;
377388
});
378-
}
389+
if (!latest.has_value()) {
379390
continue;
380391
}
381392

382-
if (latest.path != watch.config_path) {
393+
if (latest->path != watch.config_path) {
383394
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);
386397
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);
389401
loaded_config.config.reset();
390-
loaded_config.config.set_config_file_path(*latest.path);
402+
loaded_config.config.set_config_file_path(*latest->path);
391403
loaded_config.config.load_from_json(&loaded_config.file_content);
392404
config = &loaded_config.config;
393405
} else {
@@ -401,7 +413,7 @@ std::vector<configuration_change> configuration_loader::refresh() {
401413
.config = config,
402414
.token = watch.token,
403415
});
404-
watch.config_path = latest.path;
416+
watch.config_path = latest->path;
405417
}
406418
}
407419

src/quick-lint-js/configuration-loader.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ class configuration_loader {
7171
std::optional<canonical_path> path;
7272
loaded_config_file* already_loaded = nullptr;
7373
padded_string file_content{};
74-
std::string error;
7574
};
7675

7776
struct watched_path {
@@ -88,8 +87,8 @@ class configuration_loader {
8887

8988
configuration_or_error find_and_load_config_file_in_directory_and_ancestors(
9089
canonical_path&&, const char* input_path);
91-
found_config_file find_config_file_in_directory_and_ancestors(
92-
canonical_path&&);
90+
boost::leaf::result<found_config_file>
91+
find_config_file_in_directory_and_ancestors(canonical_path&&);
9392

9493
boost::leaf::result<canonical_path_result> get_parent_directory(
9594
const char* input_path);

0 commit comments

Comments
 (0)