FlatImage
A configurable Linux containerization system
Loading...
Searching...
No Matches
desktop.hpp
Go to the documentation of this file.
1
8
9#pragma once
10
11#include <string>
12#include <set>
13
14#include "../std/expected.hpp"
15#include "../std/enum.hpp"
16#include "db.hpp"
17
27namespace ns_db::ns_desktop
28{
29
30ENUM(IntegrationItem, ENTRY, MIMETYPE, ICON);
31
33{
34 private:
35 std::string m_name;
36 Value<fs::path> m_path_file_icon;
37 std::set<IntegrationItem> m_set_integrations;
38 std::set<std::string> m_set_categories;
39 public:
40 Desktop();
41 [[maybe_unused]] std::string const& get_name() const { return m_name; }
42 [[maybe_unused]] Value<fs::path> const& get_path_file_icon() const { return m_path_file_icon; }
43 [[maybe_unused]] std::set<IntegrationItem> const& get_integrations() const { return m_set_integrations; }
44 [[maybe_unused]] std::set<std::string> const& get_categories() const { return m_set_categories; }
45 [[maybe_unused]] void set_name(std::string_view str_name) { m_name = str_name; }
46 [[maybe_unused]] void set_integrations(std::set<IntegrationItem> const& set_integrations) { m_set_integrations = set_integrations; }
47 [[maybe_unused]] void set_categories(std::set<std::string> const& set_categories) { m_set_categories = set_categories; }
48 friend Value<Desktop> deserialize(std::string_view raw_json) noexcept;
49 friend Value<std::string> serialize(Desktop const& desktop) noexcept;
50}; // Desktop }}}
51
52
57 : m_name()
58 , m_path_file_icon(std::unexpected("m_path_file_icon is undefined"))
59 , m_set_integrations()
60 , m_set_categories()
61{}
62
69[[maybe_unused]] [[nodiscard]] inline Value<Desktop> deserialize(std::string_view str_raw_json) noexcept
70{
71 Desktop desktop;
72 return_if(str_raw_json.empty(), Error("W::Empty json data"));
73 // Open DB
74 auto db = Pop(ns_db::from_string(str_raw_json));
75 // Parse name (required)
76 desktop.m_name = Pop(db("name").template value<std::string>());
77 // Parse icon path (optional)
78 desktop.m_path_file_icon = db("icon").template value<std::string>();
79 // Parse enabled integrations (optional)
80 for(auto&& item : db("integrations").value<std::vector<std::string>>().or_default())
81 {
82 desktop.m_set_integrations.insert(Pop(IntegrationItem::from_string(item)));
83 }
84 // Parse categories (required)
85 auto db_categories = Pop(db("categories").template value<std::vector<std::string>>());
86 std::ranges::for_each(db_categories, [&](auto&& e){ desktop.m_set_categories.insert(e); });
87 return desktop;
88}
89
96[[maybe_unused]] [[nodiscard]] inline Value<Desktop> deserialize(std::ifstream& stream_raw_json) noexcept
97{
98 std::stringstream ss;
99 ss << stream_raw_json.rdbuf();
100 return deserialize(ss.str());
101}
102
109[[maybe_unused]] [[nodiscard]] inline Value<std::string> serialize(Desktop const& desktop) noexcept
110{
111 auto db = ns_db::Db();
112 db("name") = desktop.m_name;
113 db("integrations") = desktop.m_set_integrations;
114 if (desktop.m_path_file_icon)
115 {
116 db("icon") = Pop(desktop.m_path_file_icon);
117 }
118 db("categories") = desktop.m_set_categories;
119 return db.dump();
120}
121
122} // namespace ns_db::ns_desktop
123
124/* vim: set expandtab fdm=marker ts=2 sw=2 tw=100 et :*/
A type-safe wrapper around nlohmann::json for database operations.
Definition db.hpp:64
friend Value< std::string > serialize(Desktop const &desktop) noexcept
Serializes a Desktop class into a json string.
Definition desktop.hpp:109
Desktop()
Construct a new Desktop:: Desktop object.
Definition desktop.hpp:56
friend Value< Desktop > deserialize(std::string_view raw_json) noexcept
Deserializes JSON string into a Binds object.
Definition bind.hpp:184
A database that interfaces with nlohmann json.
Custom enumeration class.
Enhanced error handling framework built on std::expected.
Desktop integration configuration management.
Value< std::string > serialize(Desktop const &desktop) noexcept
Serializes a Desktop class into a json string.
Definition desktop.hpp:109
Value< Desktop > deserialize(std::string_view str_raw_json) noexcept
Deserializes a string into a Desktop class.
Definition desktop.hpp:69
Value< Db > from_string(S &&s)
Parses a JSON string and creates a Db object.
Definition db.hpp:496
Enhanced expected type with integrated logging capabilities.
Definition expected.hpp:44