92 lines
2.1 KiB
C++
92 lines
2.1 KiB
C++
/*
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
*/
|
|
module;
|
|
|
|
#include <string>
|
|
#include <forward_list>
|
|
#include <stdexcept>
|
|
|
|
#include <toml.hpp>
|
|
|
|
import katja.database;
|
|
import katja.repository;
|
|
|
|
export module katja.configuration;
|
|
|
|
export namespace katja
|
|
{
|
|
struct repository_configuration
|
|
{
|
|
std::string path;
|
|
};
|
|
|
|
class configuration
|
|
{
|
|
public:
|
|
using repositories_t = std::forward_list<std::pair<std::string, repository_configuration>>;
|
|
|
|
private:
|
|
repositories_t repositories;
|
|
|
|
public:
|
|
void add(const std::string& name, repository_configuration&& repository)
|
|
{
|
|
this->repositories.emplace_front(name, std::move(repository));
|
|
}
|
|
|
|
repositories_t::iterator begin()
|
|
{
|
|
return this->repositories.begin();
|
|
}
|
|
|
|
repositories_t::iterator end()
|
|
{
|
|
return this->repositories.end();
|
|
}
|
|
|
|
repositories_t::const_iterator cbegin()
|
|
{
|
|
return this->repositories.cbegin();
|
|
}
|
|
|
|
repositories_t::const_iterator cend()
|
|
{
|
|
return this->repositories.cend();
|
|
}
|
|
};
|
|
|
|
class configuration_error : public std::runtime_error
|
|
{
|
|
public:
|
|
configuration_error()
|
|
: std::runtime_error("Configuration is expected to be a table of repositories.")
|
|
{
|
|
}
|
|
};
|
|
}
|
|
|
|
TOML11_DEFINE_CONVERSION_NON_INTRUSIVE(katja::repository_configuration, path);
|
|
|
|
export namespace katja
|
|
{
|
|
configuration read_configuration(const std::string& configuration_file)
|
|
{
|
|
auto raw_root = toml::parse(configuration_file);
|
|
configuration result;
|
|
|
|
if (!raw_root.is_table())
|
|
{
|
|
throw configuration_error();
|
|
}
|
|
for (const auto& [root_name, root_value] : raw_root.as_table())
|
|
{
|
|
auto repository_value = toml::get<repository_configuration>(root_value);
|
|
result.add(root_name, std::move(repository_value));
|
|
}
|
|
return result;
|
|
}
|
|
}
|