Skip to content

Commit f502382

Browse files
committed
refactor: use tl::expected
1 parent 95dbc52 commit f502382

21 files changed

Lines changed: 213 additions & 323 deletions

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ find_package(SDL3_image CONFIG REQUIRED)
3030
find_package(Freetype REQUIRED)
3131
find_package(glad CONFIG REQUIRED)
3232
find_package(GTest CONFIG REQUIRED)
33+
find_package(tl-expected CONFIG REQUIRED)
3334

3435
# source files
3536
file(GLOB_RECURSE COMMON_SOURCES "src/common/*.cpp")
@@ -61,7 +62,7 @@ function(setup_target target)
6162
target_include_directories(${target} PRIVATE src)
6263
target_link_libraries(
6364
${target} PRIVATE nlohmann_json::nlohmann_json cpr::cpr Boost::system
64-
Boost::filesystem Threads::Threads)
65+
Boost::filesystem Threads::Threads tl::expected)
6566
target_compile_definitions(${target} PRIVATE NOMINMAX BOOST_FILESYSTEM_NO_LIB
6667
BOOST_FILESYSTEM_STATIC_LINK=1)
6768
if(WIN32)

src/cli/cli.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,17 @@ bool cli::run(
99
bool verbose,
1010
bool disable_update_check
1111
) {
12-
auto res = blur.initialise(verbose, preview);
13-
14-
if (!res.success) { // todo: preview in cli
12+
auto init_res = blur.initialise(verbose, preview);
13+
if (!init_res) { // todo: preview in cli
1514
u::log(L"Blur failed to initialise");
16-
u::log("Reason: {}", res.error_message);
15+
u::log("Reason: {}", init_res.error());
1716
return false;
1817
}
1918

2019
if (!disable_update_check) {
2120
auto update_res = Blur::check_updates();
22-
if (update_res.success && !update_res.is_latest) {
23-
u::log("There's a newer version ({}) available at {}!", update_res.latest_tag, update_res.latest_tag_url);
21+
if (update_res && !update_res->is_latest) {
22+
u::log("There's a newer version ({}) available at {}!", update_res->latest_tag, update_res->latest_tag_url);
2423
}
2524
}
2625

src/common/blur.cpp

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "config_app.h"
88
#include "config_presets.h"
99

10-
Blur::InitialisationResponse Blur::initialise(bool _verbose, bool _using_preview) {
10+
tl::expected<void, std::string> Blur::initialise(bool _verbose, bool _using_preview) {
1111
resources_path = u::get_resources_path();
1212
settings_path = u::get_settings_path();
1313

@@ -51,24 +51,15 @@ Blur::InitialisationResponse Blur::initialise(bool _verbose, bool _using_preview
5151

5252
// didn't use installer, check if dependencies are installed
5353
if (!std::filesystem::exists(ffmpeg_path)) {
54-
return {
55-
.success = false,
56-
.error_message = "FFmpeg could not be found. " + manual_troubleshooting_info,
57-
};
54+
return tl::unexpected("FFmpeg could not be found. " + manual_troubleshooting_info);
5855
}
5956

6057
if (!std::filesystem::exists(ffprobe_path)) {
61-
return {
62-
.success = false,
63-
.error_message = "FFprobe could not be found. " + manual_troubleshooting_info,
64-
};
58+
return tl::unexpected("FFprobe could not be found. " + manual_troubleshooting_info);
6559
}
6660

6761
if (!std::filesystem::exists(vspipe_path)) {
68-
return {
69-
.success = false,
70-
.error_message = "VapourSynth could not be found. " + manual_troubleshooting_info,
71-
};
62+
return tl::unexpected("VapourSynth could not be found. " + manual_troubleshooting_info);
7263
}
7364
}
7465
else {
@@ -80,30 +71,21 @@ Blur::InitialisationResponse Blur::initialise(bool _verbose, bool _using_preview
8071
ffmpeg_path = *_ffmpeg_path;
8172
}
8273
else {
83-
return {
84-
.success = false,
85-
.error_message = "FFmpeg could not be found. " + manual_troubleshooting_info,
86-
};
74+
return tl::unexpected("FFmpeg could not be found. " + manual_troubleshooting_info);
8775
}
8876

8977
if (auto _ffprobe_path = u::get_program_path("ffprobe")) {
9078
ffprobe_path = *_ffprobe_path;
9179
}
9280
else {
93-
return {
94-
.success = false,
95-
.error_message = "FFprobe could not be found. " + manual_troubleshooting_info,
96-
};
81+
return tl::unexpected("FFprobe could not be found. " + manual_troubleshooting_info);
9782
}
9883

9984
if (auto _vspipe_path = u::get_program_path("vspipe")) {
10085
vspipe_path = *_vspipe_path;
10186
}
10287
else {
103-
return {
104-
.success = false,
105-
.error_message = "VapourSynth could not be found. " + manual_troubleshooting_info,
106-
};
88+
return tl::unexpected("VapourSynth could not be found. " + manual_troubleshooting_info);
10789
}
10890
}
10991

@@ -125,10 +107,6 @@ Blur::InitialisationResponse Blur::initialise(bool _verbose, bool _using_preview
125107
std::thread([this] {
126108
initialise_rife_gpus();
127109
}).detach();
128-
129-
return {
130-
.success = true,
131-
};
132110
}
133111

134112
void Blur::initialise_base_temp_path() {
@@ -187,10 +165,10 @@ bool Blur::remove_temp_path(const std::filesystem::path& temp_path) {
187165
}
188166
}
189167

190-
updates::UpdateCheckRes Blur::check_updates() {
168+
tl::expected<updates::UpdateCheckRes, std::string> Blur::check_updates() {
191169
auto config = config_app::get_app_config();
192170
if (!config.check_updates)
193-
return { .success = false };
171+
return updates::UpdateCheckRes{};
194172

195173
return updates::is_latest_version(config.check_beta);
196174
}

src/common/blur.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,7 @@ class Blur { // todo: switch all the classes which could be namespaces into name
2323
std::filesystem::path ffprobe_path;
2424
std::filesystem::path vspipe_path;
2525

26-
struct InitialisationResponse {
27-
bool success;
28-
std::string error_message;
29-
};
30-
31-
InitialisationResponse initialise(bool _verbose, bool _using_preview);
26+
tl::expected<void, std::string> initialise(bool _verbose, bool _using_preview);
3227

3328
void cleanup() const;
3429

@@ -37,7 +32,7 @@ class Blur { // todo: switch all the classes which could be namespaces into name
3732
[[nodiscard]] std::optional<std::filesystem::path> create_temp_path(const std::string& folder_name) const;
3833
static bool remove_temp_path(const std::filesystem::path& temp_path);
3934

40-
static updates::UpdateCheckRes check_updates();
35+
static tl::expected<updates::UpdateCheckRes, std::string> check_updates();
4136
static void update(
4237
const std::string& tag,
4338
const std::optional<std::function<void(const std::string& text, bool done)>>& progress_callback = {}

src/common/common_pch.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
// libs
2727
#include <nlohmann/json.hpp>
2828
#include <cpr/cpr.h>
29+
#include <tl/expected.hpp>
2930

3031
#include <boost/process.hpp>
3132
#include <boost/asio.hpp>

src/common/config_blur.cpp

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ void config_blur::create(const std::filesystem::path& filepath, const BlurSettin
111111
output << "blur amount tied to fps: " << (current_settings.blur_amount_tied_to_fps ? "true" : "false") << "\n";
112112
}
113113

114-
config_blur::ConfigValidationResponse config_blur::validate(BlurSettings& config, bool fix) {
114+
tl::expected<void, std::string> config_blur::validate(BlurSettings& config, bool fix) {
115115
std::set<std::string> errors;
116116

117117
if (!u::contains(SVP_INTERPOLATION_PRESETS, config.advanced.svp_interpolation_preset)) {
@@ -141,10 +141,10 @@ config_blur::ConfigValidationResponse config_blur::validate(BlurSettings& config
141141
config.advanced.interpolation_blocksize = DEFAULT_CONFIG.advanced.interpolation_blocksize;
142142
}
143143

144-
return ConfigValidationResponse{
145-
.success = errors.empty(),
146-
.error = u::join(errors, " "),
147-
};
144+
if (!errors.empty())
145+
return tl::unexpected(u::join(errors, " "));
146+
147+
// ok:)
148148
}
149149

150150
BlurSettings config_blur::parse(const std::filesystem::path& config_filepath) {
@@ -294,7 +294,7 @@ BlurSettings config_blur::get_config(const std::filesystem::path& config_filepat
294294
return parse(cfg_path);
295295
}
296296

297-
BlurSettings::ToJsonResult BlurSettings::to_json() const {
297+
tl::expected<nlohmann::json, std::string> BlurSettings::to_json() const {
298298
nlohmann::json j;
299299

300300
j["blur"] = this->blur;
@@ -358,23 +358,17 @@ BlurSettings::ToJsonResult BlurSettings::to_json() const {
358358
j["interpolation_mask_area"] = this->advanced.interpolation_mask_area;
359359

360360
auto rife_model_path = get_rife_model_path();
361-
if (!rife_model_path.success) {
362-
return {
363-
.success = false,
364-
.error_message = rife_model_path.error_message,
365-
};
366-
}
367-
j["rife_model"] = *rife_model_path.rife_model_path;
361+
if (!rife_model_path)
362+
return tl::unexpected(rife_model_path.error());
363+
364+
j["rife_model"] = *rife_model_path;
368365

369366
j["manual_svp"] = this->advanced.manual_svp;
370367
j["super_string"] = this->advanced.super_string;
371368
j["vectors_string"] = this->advanced.vectors_string;
372369
j["smooth_string"] = this->advanced.smooth_string;
373370

374-
return {
375-
.success = true,
376-
.json = j,
377-
};
371+
return j;
378372
}
379373

380374
BlurSettings::BlurSettings() {
@@ -405,7 +399,7 @@ void BlurSettings::verify_gpu_encoding() {
405399
}
406400

407401
// NOLINTBEGIN(readability-convert-member-functions-to-static) other platforms need it
408-
BlurSettings::GetRifeModelResult BlurSettings::get_rife_model_path() const {
402+
tl::expected<std::filesystem::path, std::string> BlurSettings::get_rife_model_path() const {
409403
// NOLINTEND(readability-convert-member-functions-to-static)
410404
std::filesystem::path rife_model_path;
411405

@@ -419,16 +413,10 @@ BlurSettings::GetRifeModelResult BlurSettings::get_rife_model_path() const {
419413
# endif
420414

421415
if (!std::filesystem::exists(rife_model_path))
422-
return {
423-
.success = false,
424-
.error_message = std::format("RIFE model '{}' could not be found", this->advanced.rife_model),
425-
};
416+
return tl::unexpected(std::format("RIFE model '{}' could not be found", this->advanced.rife_model));
426417
#endif
427418

428-
return {
429-
.success = true,
430-
.rife_model_path = rife_model_path,
431-
};
419+
return rife_model_path;
432420
}
433421

434422
void BlurSettings::set_fastest_rife_gpu() {
@@ -440,9 +428,10 @@ void BlurSettings::set_fastest_rife_gpu() {
440428

441429
if (sample_video_exists) {
442430
auto rife_model_path = BlurSettings::get_rife_model_path();
443-
if (rife_model_path.success) {
431+
432+
if (rife_model_path) {
444433
int fastest_gpu_index =
445-
u::get_fastest_rife_gpu_index(blur_copy.rife_gpus, *rife_model_path.rife_model_path, sample_video_path);
434+
u::get_fastest_rife_gpu_index(blur_copy.rife_gpus, *rife_model_path, sample_video_path);
446435

447436
this->rife_gpu_index = fastest_gpu_index;
448437
u::log("set rife_gpu_index to the fastest gpu ({})", fastest_gpu_index);

src/common/config_blur.h

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -77,21 +77,9 @@ struct BlurSettings {
7777

7878
void verify_gpu_encoding();
7979

80-
struct ToJsonResult {
81-
bool success;
82-
std::optional<nlohmann::json> json;
83-
std::string error_message;
84-
};
85-
86-
[[nodiscard]] ToJsonResult to_json() const;
87-
88-
struct GetRifeModelResult {
89-
bool success;
90-
std::optional<std::filesystem::path> rife_model_path;
91-
std::string error_message;
92-
};
80+
[[nodiscard]] tl::expected<nlohmann::json, std::string> to_json() const;
9381

94-
[[nodiscard]] GetRifeModelResult get_rife_model_path() const;
82+
[[nodiscard]] tl::expected<std::filesystem::path, std::string> get_rife_model_path() const;
9583
void set_fastest_rife_gpu();
9684
};
9785

@@ -112,12 +100,7 @@ namespace config_blur {
112100

113101
void create(const std::filesystem::path& filepath, const BlurSettings& current_settings = BlurSettings());
114102

115-
struct ConfigValidationResponse {
116-
bool success;
117-
std::string error;
118-
};
119-
120-
ConfigValidationResponse validate(BlurSettings& config, bool fix);
103+
tl::expected<void, std::string> validate(BlurSettings& config, bool fix);
121104

122105
BlurSettings parse(const std::filesystem::path& config_filepath);
123106
BlurSettings parse_global_config();

0 commit comments

Comments
 (0)