Skip to content

Commit c4ea1af

Browse files
committed
Refactor find_and_load_config_file_for_input 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 f478f58 commit c4ea1af

File tree

2 files changed

+50
-32
lines changed

2 files changed

+50
-32
lines changed

src/configuration-loader.cpp

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,52 @@ configuration_or_error configuration_loader::watch_and_load_for_file(
5555
.error = std::string(),
5656
.token = const_cast<void*>(token),
5757
});
58-
configuration_or_error config =
59-
this->find_and_load_config_file_for_input(file_path.c_str());
60-
if (!config.error.empty()) {
61-
watch.error = config.error;
62-
}
63-
return config;
58+
return boost::leaf::try_handle_all(
59+
[&]() -> boost::leaf::result<configuration_or_error> {
60+
return this->find_and_load_config_file_for_input(file_path.c_str());
61+
},
62+
make_canonicalize_path_error_handlers(
63+
[&](std::string&& message) -> configuration_or_error {
64+
watch.error = message;
65+
return configuration_or_error(std::move(message));
66+
}),
67+
make_read_file_error_handlers([&](std::string&& message) {
68+
watch.error = message;
69+
return configuration_or_error(std::move(message));
70+
}),
71+
[&](boost::leaf::e_errno error) {
72+
const char* message = std::strerror(error.value);
73+
watch.error = message;
74+
return configuration_or_error(message);
75+
},
76+
[&]() {
77+
QLJS_ASSERT(false);
78+
const char* message = "unknown error";
79+
watch.error = message;
80+
return configuration_or_error(message);
81+
});
6482
}
6583

6684
configuration_or_error configuration_loader::load_for_file(
6785
const std::string& file_path) {
68-
return this->find_and_load_config_file_for_input(file_path.c_str());
86+
return boost::leaf::try_handle_all(
87+
[&]() -> boost::leaf::result<configuration_or_error> {
88+
return this->find_and_load_config_file_for_input(file_path.c_str());
89+
},
90+
make_canonicalize_path_error_handlers(
91+
[](std::string&& message) -> configuration_or_error {
92+
return configuration_or_error(std::move(message));
93+
}),
94+
make_read_file_error_handlers([](std::string&& message) {
95+
return configuration_or_error(std::move(message));
96+
}),
97+
[](boost::leaf::e_errno error) {
98+
return configuration_or_error(std::strerror(error.value));
99+
},
100+
[]() {
101+
QLJS_ASSERT(false);
102+
return configuration_or_error("unknown error");
103+
});
69104
}
70105

71106
configuration_or_error configuration_loader::load_for_file(
@@ -128,32 +163,15 @@ boost::leaf::result<configuration*> configuration_loader::load_config_file(
128163
QLJS_WARNING_PUSH
129164
QLJS_WARNING_IGNORE_GCC("-Wuseless-cast")
130165

131-
configuration_or_error
166+
boost::leaf::result<configuration*>
132167
configuration_loader::find_and_load_config_file_for_input(
133168
const char* input_path) {
134-
return boost::leaf::try_handle_all(
135-
[&]() -> boost::leaf::result<configuration_or_error> {
136-
boost::leaf::result<canonical_path_result> parent_directory =
137-
this->get_parent_directory(input_path);
138-
if (!parent_directory) return parent_directory.error();
139-
return this->find_and_load_config_file_in_directory_and_ancestors(
140-
std::move(*parent_directory).canonical(),
141-
/*input_path=*/input_path);
142-
},
143-
make_canonicalize_path_error_handlers(
144-
[](std::string&& message) -> configuration_or_error {
145-
return configuration_or_error(std::move(message));
146-
}),
147-
make_read_file_error_handlers([](std::string&& message) {
148-
return configuration_or_error(std::move(message));
149-
}),
150-
[](boost::leaf::e_errno error) {
151-
return configuration_or_error(std::strerror(error.value));
152-
},
153-
[]() {
154-
QLJS_ASSERT(false);
155-
return configuration_or_error("unknown error");
156-
});
169+
boost::leaf::result<canonical_path_result> parent_directory =
170+
this->get_parent_directory(input_path);
171+
if (!parent_directory) return parent_directory.error();
172+
return this->find_and_load_config_file_in_directory_and_ancestors(
173+
std::move(*parent_directory).canonical(),
174+
/*input_path=*/input_path);
157175
}
158176

159177
boost::leaf::result<configuration*>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class configuration_loader {
8181
};
8282

8383
boost::leaf::result<configuration*> load_config_file(const char* config_path);
84-
configuration_or_error find_and_load_config_file_for_input(
84+
boost::leaf::result<configuration*> find_and_load_config_file_for_input(
8585
const char* input_path);
8686
boost::leaf::result<configuration*>
8787
find_and_load_config_file_for_current_directory();

0 commit comments

Comments
 (0)