FlatImage
A configurable Linux containerization system
Loading...
Searching...
No Matches
recipe.hpp
Go to the documentation of this file.
1
8
9#pragma once
10
11#include <string>
12#include <vector>
13
14#include "../std/expected.hpp"
15#include "db.hpp"
16#include "desktop.hpp"
17
27namespace ns_db::ns_recipe
28{
29
30class Recipe
31{
32 private:
33 std::string m_description;
34 std::vector<std::string> m_packages;
35 std::vector<std::string> m_dependencies;
37 public:
38 Recipe();
39 [[maybe_unused]] std::string const& get_description() const { return m_description; }
40 [[maybe_unused]] std::vector<std::string> const& get_packages() const { return m_packages; }
41 [[maybe_unused]] std::vector<std::string> const& get_dependencies() const { return m_dependencies; }
42 [[maybe_unused]] Value<ns_desktop::Desktop> const& get_desktop() const { return m_desktop; }
43 [[maybe_unused]] void set_description(std::string_view str_description) { m_description = str_description; }
44 [[maybe_unused]] void set_packages(std::vector<std::string> const& vec_packages) { m_packages = vec_packages; }
45 [[maybe_unused]] void set_dependencies(std::vector<std::string> const& vec_dependencies) { m_dependencies = vec_dependencies; }
46 [[maybe_unused]] void set_desktop(Value<ns_desktop::Desktop> const& desktop) { m_desktop = desktop; }
47 friend Value<Recipe> deserialize(std::string_view raw_json) noexcept;
48 friend Value<std::string> serialize(Recipe const& recipe) noexcept;
49}; // Recipe
50
55 : m_description()
56 , m_packages()
57 , m_dependencies()
58 , m_desktop(std::unexpected("m_desktop is undefined"))
59{}
60
67[[maybe_unused]] [[nodiscard]] inline Value<Recipe> deserialize(std::string_view str_raw_json) noexcept
68{
69 Recipe recipe;
70 return_if(str_raw_json.empty(), Error("D::Empty json data"));
71 // Open DB
72 auto db = Pop(ns_db::from_string(str_raw_json));
73 // Parse description (required)
74 recipe.m_description = Pop(db("description").template value<std::string>(), "E::Missing 'description' field");
75 // Parse packages (required)
76 recipe.m_packages = Pop(db("packages").value<std::vector<std::string>>(), "E::Missing 'packages' field");
77 // Parse dependencies (optional, defaults to empty vector)
78 recipe.m_dependencies = db("dependencies").value<std::vector<std::string>>().or_default();
79 // Parse desktop integration (optional)
80 if(auto desktop_json = db("desktop").dump(); desktop_json)
81 {
82 recipe.m_desktop = ns_desktop::deserialize(Pop(desktop_json));
83 }
84 return recipe;
85}
86
93[[maybe_unused]] [[nodiscard]] inline Value<std::string> serialize(Recipe const& recipe) noexcept
94{
95 auto db = ns_db::Db();
96 db("description") = recipe.m_description;
97 db("packages") = recipe.m_packages;
98 if (!recipe.m_dependencies.empty())
99 {
100 db("dependencies") = recipe.m_dependencies;
101 }
102 if (recipe.m_desktop)
103 {
104 auto desktop_json_str = Pop(ns_desktop::serialize(Pop(recipe.m_desktop)));
105 auto desktop_db = Pop(ns_db::from_string(desktop_json_str));
106 db("desktop") = desktop_db.data();
107 }
108 return db.dump();
109}
110
111} // namespace ns_db::ns_recipe
112
113/* 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
Recipe()
Construct a new Recipe:: Recipe object.
Definition recipe.hpp:54
friend Value< std::string > serialize(Recipe const &recipe) noexcept
Serializes a Recipe class into a json string.
Definition recipe.hpp:93
friend Value< Recipe > deserialize(std::string_view raw_json) noexcept
Deserializes JSON string into a Binds object.
Definition bind.hpp:184
Defines a class that manages FlatImage's desktop integration.
A database that interfaces with nlohmann json.
Enhanced error handling framework built on std::expected.
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
Package recipe configuration management.
Value< Recipe > deserialize(std::string_view str_raw_json) noexcept
Deserializes a string into a Recipe class.
Definition recipe.hpp:67
Value< std::string > serialize(Recipe const &recipe) noexcept
Serializes a Recipe class into a json string.
Definition recipe.hpp:93
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