summaryrefslogtreecommitdiff
path: root/cli/configuration.cpp
blob: e927c921e6bd6d3f48b476af0dfb7709fdfde22b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
 * 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 value_type = std::pair<std::string, repository_configuration>;
        using reference_type = value_type&;
        using const_reference_type = const value_type&;

        using repositories_t = std::forward_list<value_type>;
        using size_type = repositories_t::size_type;
        using difference_type = repositories_t::difference_type;

    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 begin() const
        {
            return this->repositories.begin();
        }

        repositories_t::const_iterator end() const
        {
            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;
    }
}