Skip to content

Commit

Permalink
Isolating Glib::KeyFile
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Hilst committed Feb 12, 2025
1 parent f202b6b commit fc7149d
Show file tree
Hide file tree
Showing 9 changed files with 283 additions and 173 deletions.
17 changes: 16 additions & 1 deletion include/cloysterhpc/repos.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <cloysterhpc/os.h>
#include <cloysterhpc/runner.h>

#include <concepts>
#include <filesystem>
#include <ranges>
#include <string>
Expand All @@ -20,6 +21,19 @@ namespace cloyster {
extern std::string customRepofilePath;
};

template <typename T>
concept IsRepository = requires(T repo)
{
{ repo.id } -> std::convertible_to<std::string>;
{ repo.enabled } -> std::convertible_to<bool>;
{ repo.name } -> std::convertible_to<std::string>;
{ repo.baseurl } -> std::convertible_to<std::string>;
{ repo.metalink } -> std::convertible_to<std::string>;
{ repo.gpgcheck } -> std::convertible_to<bool>;
{ repo.gpgkey } -> std::convertible_to<std::string>;
{ repo.source } -> std::convertible_to<std::filesystem::path>;
};

struct repository {
std::string id;
bool enabled = true;
Expand All @@ -30,8 +44,9 @@ struct repository {
std::string gpgkey;
std::filesystem::path source;
};
static_assert(IsRepository<repository>);

template <typename Repository, typename Runner>
template <IsRepository Repository, typename Runner>
class /* [[deprecated("refactoring")]] */ RepoManager {
public:
RepoManager(Runner& runner, const OS& osinfo);
Expand Down
44 changes: 44 additions & 0 deletions include/cloysterhpc/services/files.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#ifndef CLOYSTER_SERVICES_FILES_H
#define CLOYSTER_SERVICES_FILES_H

#include <filesystem>
#include <string>
#include <unordered_map>
#include <concepts>
#include <vector>

namespace cloyster::services::files {
template <typename File>

concept IsKeyFileReadable = requires(const File& file, const std::string& group, const std::string& key)
{
{ file.getGroups() } -> std::convertible_to<std::vector<std::string>>;
{ file.getString(group, key) } -> std::same_as<std::string>;
{ file.getBoolean(group, key) } -> std::same_as<bool>;
{ file.getStringOpt(group, key) } -> std::same_as<std::optional<std::string>>;
};


class KeyFile {
struct Impl;

std::unique_ptr<Impl> m_impl;
explicit KeyFile(KeyFile::Impl&& impl);
public:

friend std::unique_ptr<KeyFile> from(const std::filesystem::path& path);
friend std::unique_ptr<KeyFile> from(std::istream istream);

[[nodiscard]] std::vector<std::string> getGroups() const;
[[nodiscard]] std::string getString(const std::string& group, const std::string& key) const;
[[nodiscard]] bool getBoolean(const std::string& group, const std::string& key) const;
[[nodiscard]] std::optional<std::string> getStringOpt(const std::string& group, const std::string& key) const;

static std::unique_ptr<KeyFile> from(const std::filesystem::path& path);
static std::unique_ptr<KeyFile> from(std::istream& istream);
};
static_assert(IsKeyFileReadable<KeyFile>);

};

#endif
9 changes: 4 additions & 5 deletions include/cloysterhpc/services/repo.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <filesystem>
#include <optional>
#include <string_view>
#include <unordered_map>
#include <vector>

#include <glibmm/fileutils.h>
Expand All @@ -20,7 +21,7 @@
* /etc/yum.repos.d/
* @note This class is WIP, it was added as a PoC of glibmm integration
*/
class repo {
class [[deprecated("RepoManager refactoring")]] repo {

public:
std::string_view m_name {};
Expand All @@ -46,13 +47,11 @@ class repo {

std::string_view name() const;
void name(std::string_view name);

static void load_repository(std::filesystem::path path);
};

class repository_exception : public std::runtime_error {
class RepositoryException : public std::runtime_error {
public:
explicit repository_exception(const std::string& msg)
explicit RepositoryException(const std::string& msg)
: std::runtime_error(msg)
{
}
Expand Down
121 changes: 55 additions & 66 deletions include/cloysterhpc/services/repofile.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,131 +8,121 @@

#include <concepts>
#include <expected>
#include <filesystem>
#include <istream>
#include <optional>
#include <ostream>
#include <string>
#include <vector>

// #include <glibmm/fileutils.h>
// #include <glibmm/keyfile.h>
#include <cloysterhpc/repos.h> // @TODO: Remove this header

// #include <cloysterhpc/services/repo.h>

namespace cloysterhpc {
namespace cloyster {
// @TODO: Move this to its own file
template <typename T>
concept NotCopyable = !std::is_copy_constructible_v<T> && !std::is_copy_assignable_v<T>;
concept NotCopyable
= !std::is_copy_constructible_v<T> && !std::is_copy_assignable_v<T>;

template <typename T>
concept NotMoveable = !std::is_move_constructible_v<T> && !std::is_move_assignable_v<T>;
concept NotMoveable
= !std::is_move_constructible_v<T> && !std::is_move_assignable_v<T>;

template <typename T>
concept NotCopiableMoveable = NotMoveable<T> && NotCopyable<T>;
/**
* @brief Parser<P, T> means: P can parse and unparse Ts from streams. The
* parsers are allowed to throw exceptions
*/
template<typename Parser_, typename T>
concept Parser =
requires(Parser_ parser, std::istream& input, std::ostream& output, const T& parsed)
{
{ parser.parse(input) } -> std::same_as<T>;
{ parser.unparse(parsed, output) } -> std::same_as<void>;
template <typename Parser_, typename T>
concept Parser = requires(Parser_ parser, std::istream& parseInput,
std::ostream& unparseOutput, const T& unparseInput, T& parseOutput) {
{ parser.parse(parseInput, parseOutput) } -> std::same_as<void>;
{ parser.unparse(unparseInput, unparseOutput) } -> std::same_as<void>;
};

// @TODO: Move this to its own file
/**
* @brief ParserNoExc<P, T, E> means: P can parse and unparse Ts from streams. The
* parsers are not allowed to throw exceptions, it must return errors of type E
* @brief ParserNoExc<P, T, E> means: P can parse and unparse Ts from streams.
* The parsers are not allowed to throw exceptions, it must return errors of
* type E
*/
template<typename Parser_, typename T, typename E>
concept ParserNoExc =
requires(Parser_ parser, std::istream& input, std::ostream& output, const T& parsed)
{
template <typename Parser_, typename T, typename E>
concept ParserNoExc = requires(Parser_ parser, std::istream& input,
std::ostream& output, const T& parsed) {
{ parser.parse(input) } noexcept -> std::same_as<std::expected<T, E>>;
{ parser.unparse(parsed, output) } noexcept -> std::same_as<std::expected<void, E>>;
{
parser.unparse(parsed, output)
} noexcept -> std::same_as<std::expected<void, E>>;
};

// @TODO: Move this to its own file
/**
* @brief Stored<T> means that T represends data in a disk that need to be
* saved and restored after change.
*/
template<typename File_>
concept Saveable =
requires(File_ file)
{
template <typename File_>
concept Saveable = requires(File_ file) {
{ file.load() } -> std::same_as<void>;
{ file.save() } -> std::same_as<void>;
};

/**
* @brief Represents a generic repository
* @TODO MERGE THIS WITH cloyterhpc/repos.h
*/
template<typename T>
concept Repository = requires(T repo, bool flag, const std::string& url) {
{ repo.name() } -> std::same_as<const std::string&>;
{ repo.enable(flag) } -> std::same_as<void>;
{ repo.enabled() } -> std::same_as<bool>;
{ repo.base_url(url) } -> std::same_as<void>;
{ repo.base_url() } -> std::same_as<std::optional<const std::string&>>;
};

class RPMRepo final {
std::string m_group;
std::string m_name;
std::optional<std::string> m_base_url;
std::optional<std::string> m_metalink;
bool m_enabled = false;
bool m_gpgcheck = false;
std::string m_gpgkey;

public:
[[nodiscard]] const std::string& name() const;
void enable(bool flag);
bool enabled();
void base_url(std::string base_url);
[[nodiscard]] std::optional<const std::string&> base_url() const;
/*
* @TODO MERGE THIS WITH cloyterhpc/repos.h
*/
struct RPMRepository final {
std::string id;
bool enabled = true;
std::string name;
std::string baseurl;
std::string metalink;
bool gpgcheck = true;
std::string gpgkey;
std::filesystem::path source;
std::string group;
};
//static_assert(Repository<RPMRepo>);
// static_assert(Repository<RPMRepository>);
static_assert(IsRepository<RPMRepository>);

// @TODO: Move this to its own file
/**
* @brief Parse/Unparse EL repositories
* @brief Parse/Unparse RPM repositories
*/
class ELRepoParser final {
class RPMRepositoryParser final {
public:
static std::vector<RPMRepo> parse(std::istream& input);
static void parse(std::istream& input, std::vector<RPMRepository>& output);
static void unparse(
const std::vector<RPMRepo>& repos, std::ostream& output);
const std::vector<RPMRepository>& repos, std::ostream& output);
};
static_assert(Parser<ELRepoParser, std::vector<RPMRepo>>);
static_assert(Parser<RPMRepositoryParser, std::vector<RPMRepository>>);

// @TODO: Move this to its own file
/**
* @brief Represents
*/
class RPMRepoFile final {
class RPMRepositoryFile final {
public:
~RPMRepoFile() = default;
RPMRepoFile() = default;
RPMRepoFile(const RPMRepoFile&) = delete;
RPMRepoFile& operator=(const RPMRepoFile&) = delete;
RPMRepoFile(RPMRepoFile&&) = delete;
RPMRepoFile& operator=(RPMRepoFile&&) = delete;
~RPMRepositoryFile() = default;
RPMRepositoryFile() = default;
RPMRepositoryFile(const RPMRepositoryFile&) = delete;
RPMRepositoryFile& operator=(const RPMRepositoryFile&) = delete;
RPMRepositoryFile(RPMRepositoryFile&&) = delete;
RPMRepositoryFile& operator=(RPMRepositoryFile&&) = delete;
void save();
void load();
};
static_assert(Saveable<RPMRepoFile>);
static_assert(NotCopiableMoveable<RPMRepoFile>);
static_assert(Saveable<RPMRepositoryFile>);
static_assert(NotCopiableMoveable<RPMRepositoryFile>);

// @TODO WIP, rename to RepoManager and replace the old RepoManager
/**
* @brief Enable/disable, install/uninstall repositories to/from the filesystem
*/
template <typename Repo, typename RepoFile>
class NewRepoManager final {
template <typename Repo, typename RepoFile> class NewRepoManager final {
public:
~NewRepoManager() = default;
NewRepoManager() = default;
Expand All @@ -149,7 +139,6 @@ class NewRepoManager final {
const std::vector<Repo>& listRepos() const;
};


};
} // namespace cloyster;

#endif
Empty file added src/presenter/CMakeLists.txt
Empty file.
Loading

0 comments on commit fc7149d

Please sign in to comment.