mirror of
https://github.com/obsqrbtz/clrsync.git
synced 2026-04-09 04:29:04 +03:00
refactor: error handling with err objects
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
#include "config.hpp"
|
||||
#include "core/utils.hpp"
|
||||
#include "core/error.hpp"
|
||||
|
||||
#include <core/palette/color.hpp>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <stdexcept>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include "windows.h"
|
||||
@@ -19,14 +19,18 @@ config &config::instance()
|
||||
return inst;
|
||||
}
|
||||
|
||||
void config::initialize(std::unique_ptr<clrsync::core::io::file> file)
|
||||
Result<void> config::initialize(std::unique_ptr<clrsync::core::io::file> file)
|
||||
{
|
||||
copy_default_configs();
|
||||
m_file = std::move(file);
|
||||
if (!m_file)
|
||||
throw std::runtime_error{"Config file is missing"};
|
||||
if (!m_file->parse())
|
||||
throw std::runtime_error{"Could not parse config file"};
|
||||
return Err<void>(error_code::config_missing, "Config file is missing");
|
||||
|
||||
auto parse_result = m_file->parse();
|
||||
if (!parse_result)
|
||||
return Err<void>(error_code::config_invalid, parse_result.error().message, parse_result.error().context);
|
||||
|
||||
return Ok();
|
||||
}
|
||||
|
||||
std::filesystem::path config::get_user_config_dir()
|
||||
@@ -65,19 +69,13 @@ void config::copy_file(const std::filesystem::path &src, const std::filesystem::
|
||||
return;
|
||||
|
||||
if (!std::filesystem::exists(src))
|
||||
{
|
||||
std::cerr << "Warning: Source file does not exist: " << src << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
std::ifstream in(src, std::ios::binary);
|
||||
std::ofstream out(dst, std::ios::binary);
|
||||
|
||||
if (!in || !out)
|
||||
{
|
||||
std::cerr << "Warning: Failed to copy file from " << src << " to " << dst << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
out << in.rdbuf();
|
||||
}
|
||||
@@ -85,10 +83,7 @@ void config::copy_file(const std::filesystem::path &src, const std::filesystem::
|
||||
void config::copy_dir(const std::filesystem::path &src, const std::filesystem::path &dst)
|
||||
{
|
||||
if (!std::filesystem::exists(src))
|
||||
{
|
||||
std::cerr << "Warning: Source directory does not exist: " << src << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
for (auto const &entry : std::filesystem::recursive_directory_iterator(src))
|
||||
{
|
||||
@@ -114,10 +109,7 @@ void config::copy_default_configs()
|
||||
std::filesystem::create_directories(user_dir);
|
||||
|
||||
if (system_dir.empty())
|
||||
{
|
||||
std::cerr << "Warning: No system data directory found, skipping default config copy\n";
|
||||
return;
|
||||
}
|
||||
|
||||
{
|
||||
auto src = system_dir / "config.toml";
|
||||
@@ -175,50 +167,53 @@ const uint32_t config::font_size() const
|
||||
return 14;
|
||||
}
|
||||
|
||||
void config::set_default_theme(const std::string &theme)
|
||||
Result<void> config::set_default_theme(const std::string &theme)
|
||||
{
|
||||
if (m_file)
|
||||
{
|
||||
m_file->set_value("general", "default_theme", theme);
|
||||
m_file->save_file();
|
||||
}
|
||||
if (!m_file)
|
||||
return Err<void>(error_code::config_missing, "Configuration not initialized");
|
||||
|
||||
m_file->set_value("general", "default_theme", theme);
|
||||
return m_file->save_file();
|
||||
}
|
||||
|
||||
void config::set_palettes_path(const std::string &path)
|
||||
Result<void> config::set_palettes_path(const std::string &path)
|
||||
{
|
||||
if (m_file)
|
||||
{
|
||||
m_file->set_value("general", "palettes_path", path);
|
||||
m_file->save_file();
|
||||
}
|
||||
if (!m_file)
|
||||
return Err<void>(error_code::config_missing, "Configuration not initialized");
|
||||
|
||||
m_file->set_value("general", "palettes_path", path);
|
||||
return m_file->save_file();
|
||||
}
|
||||
|
||||
void config::set_font(const std::string &font)
|
||||
Result<void> config::set_font(const std::string &font)
|
||||
{
|
||||
if (m_file)
|
||||
{
|
||||
m_file->set_value("general", "font", font);
|
||||
m_file->save_file();
|
||||
}
|
||||
if (!m_file)
|
||||
return Err<void>(error_code::config_missing, "Configuration not initialized");
|
||||
|
||||
m_file->set_value("general", "font", font);
|
||||
return m_file->save_file();
|
||||
}
|
||||
void config::set_font_size(int font_size)
|
||||
Result<void> config::set_font_size(int font_size)
|
||||
{
|
||||
if (m_file)
|
||||
{
|
||||
m_file->set_value("general", "font_size", font_size);
|
||||
m_file->save_file();
|
||||
}
|
||||
if (!m_file)
|
||||
return Err<void>(error_code::config_missing, "Configuration not initialized");
|
||||
|
||||
m_file->set_value("general", "font_size", font_size);
|
||||
return m_file->save_file();
|
||||
}
|
||||
|
||||
void config::update_template(const std::string &key,
|
||||
Result<void> config::update_template(const std::string &key,
|
||||
const clrsync::core::theme_template &theme_template)
|
||||
{
|
||||
if (!m_file)
|
||||
return Err<void>(error_code::config_missing, "Configuration not initialized");
|
||||
|
||||
m_themes[key] = theme_template;
|
||||
m_file->set_value("templates." + key, "input_path", theme_template.template_path());
|
||||
m_file->set_value("templates." + key, "output_path", theme_template.output_path());
|
||||
m_file->set_value("templates." + key, "enabled", theme_template.enabled());
|
||||
m_file->set_value("templates." + key, "reload_cmd", theme_template.reload_command());
|
||||
m_file->save_file();
|
||||
return m_file->save_file();
|
||||
}
|
||||
|
||||
const std::unordered_map<std::string, clrsync::core::theme_template> config::templates()
|
||||
@@ -241,21 +236,21 @@ const std::unordered_map<std::string, clrsync::core::theme_template> config::tem
|
||||
theme.set_enabled(false);
|
||||
}
|
||||
theme.set_reload_command(std::get<std::string>(current["reload_cmd"]));
|
||||
theme.load_template();
|
||||
(void)theme.load_template();
|
||||
m_themes.insert({theme.name(), theme});
|
||||
}
|
||||
}
|
||||
return m_themes;
|
||||
}
|
||||
|
||||
const clrsync::core::theme_template &config::template_by_name(const std::string &name) const
|
||||
Result<const clrsync::core::theme_template*> config::template_by_name(const std::string &name) const
|
||||
{
|
||||
auto it = m_themes.find(name);
|
||||
if (it != m_themes.end())
|
||||
{
|
||||
return it->second;
|
||||
return Ok(&it->second);
|
||||
}
|
||||
throw std::runtime_error("Template not found: " + name);
|
||||
return Err<const clrsync::core::theme_template*>(error_code::template_not_found, "Template not found", name);
|
||||
}
|
||||
|
||||
} // namespace clrsync::core
|
||||
|
||||
Reference in New Issue
Block a user