mirror of
https://github.com/obsqrbtz/clrsync.git
synced 2026-04-09 04:29:04 +03:00
feat: allow to remove templates
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -41,6 +41,7 @@ class file
|
||||
}
|
||||
virtual void insert_or_update_value(const std::string §ion, const std::string &key,
|
||||
const value_type &value) {};
|
||||
virtual void remove_section(const std::string §ion) {};
|
||||
virtual Result<void> save_file() { return Ok(); };
|
||||
};
|
||||
} // namespace clrsync::core::io
|
||||
|
||||
@@ -92,6 +92,25 @@ void toml_file::insert_or_update_value(const std::string §ion, const std::st
|
||||
std::visit([&](auto &&v) { tbl->insert_or_assign(key, v); }, value);
|
||||
}
|
||||
|
||||
void toml_file::remove_section(const std::string §ion)
|
||||
{
|
||||
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 {
|
||||
|
||||
@@ -19,6 +19,7 @@ class toml_file : public file
|
||||
std::map<std::string, value_type> get_table(const std::string §ion_path) const override;
|
||||
void insert_or_update_value(const std::string §ion, const std::string &key,
|
||||
const value_type &value) override;
|
||||
void remove_section(const std::string §ion) override;
|
||||
Result<void> save_file() override;
|
||||
|
||||
private:
|
||||
|
||||
Reference in New Issue
Block a user