feat: allow to remove templates

This commit is contained in:
2025-12-17 02:49:52 +03:00
parent d4c563f585
commit 1c2486d476
10 changed files with 173 additions and 18 deletions

View File

@@ -216,6 +216,32 @@ Result<void> config::update_template(const std::string &key,
return m_file->save_file();
}
Result<void> config::remove_template(const std::string &key)
{
if (!m_file)
return Err<void>(error_code::config_missing, "Configuration not initialized");
auto it = m_themes.find(key);
if (it == m_themes.end())
return Err<void>(error_code::template_not_found, "Template not found", key);
std::filesystem::path template_file = it->second.template_path();
if (std::filesystem::exists(template_file))
{
try {
std::filesystem::remove(template_file);
} catch (const std::exception& e) {
return Err<void>(error_code::file_write_failed, "Failed to delete template file", e.what());
}
}
m_themes.erase(it);
m_file->remove_section("templates." + key);
return m_file->save_file();
}
const std::unordered_map<std::string, clrsync::core::theme_template> config::templates()
{
if (m_themes.empty() && m_file)

View File

@@ -33,6 +33,7 @@ class config
Result<void> update_template(const std::string &key,
const clrsync::core::theme_template &theme_template);
Result<void> remove_template(const std::string &key);
static std::filesystem::path get_data_dir();
private:

View File

@@ -41,6 +41,7 @@ class file
}
virtual void insert_or_update_value(const std::string &section, const std::string &key,
const value_type &value) {};
virtual void remove_section(const std::string &section) {};
virtual Result<void> save_file() { return Ok(); };
};
} // namespace clrsync::core::io

View File

@@ -92,6 +92,25 @@ void toml_file::insert_or_update_value(const std::string &section, const std::st
std::visit([&](auto &&v) { tbl->insert_or_assign(key, v); }, value);
}
void toml_file::remove_section(const std::string &section)
{
toml::table *tbl = m_file.as_table();
auto parts = split(section, '.');
if (parts.empty())
return;
for (size_t i = 0; i < parts.size() - 1; ++i)
{
auto *sub = (*tbl)[parts[i]].as_table();
if (!sub)
return;
tbl = sub;
}
tbl->erase(parts.back());
}
Result<void> toml_file::save_file()
{
try {

View File

@@ -19,6 +19,7 @@ class toml_file : public file
std::map<std::string, value_type> get_table(const std::string &section_path) const override;
void insert_or_update_value(const std::string &section, const std::string &key,
const value_type &value) override;
void remove_section(const std::string &section) override;
Result<void> save_file() override;
private: