Flox Package Database 1
CRUD Operations on Nix Package Metadata
Loading...
Searching...
No Matches
types.hh
Go to the documentation of this file.
1/* ========================================================================== *
2 *
3 * @file flox/core/types.hh
4 *
5 * @brief Miscellaneous typedefs and aliases
6 *
7 *
8 * -------------------------------------------------------------------------- */
9
10#pragma once
11
12#include <string>
13#include <vector>
14
15#include <nix/eval-cache.hh>
16#include <nix/flake/flake.hh>
17#include <nix/ref.hh>
18
19#include <nlohmann/json.hpp>
20
21
22/* -------------------------------------------------------------------------- */
23
25namespace flox {
26
27/* -------------------------------------------------------------------------- */
28
33using AttrPath = std::vector<std::string>;
34
41using AttrPathGlob = std::vector<std::optional<std::string>>;
42
47using MaybeCursor = std::shared_ptr<nix::eval_cache::AttrCursor>;
48
50using Cursor = nix::ref<nix::eval_cache::AttrCursor>;
51
52
53/* -------------------------------------------------------------------------- */
54
55// TODO: Make this a real type/enum.
62using System = std::string;
63
64
65/* -------------------------------------------------------------------------- */
66
68enum subtree_type { ST_NONE = 0, ST_LEGACY = 1, ST_PACKAGES = 2 };
69
77/* Generate `to_json' and `from_json' for enum. */
79 { { ST_NONE, nullptr },
80 { ST_LEGACY, "legacyPackages" },
81 { ST_PACKAGES, "packages" } } )
82
83
86struct Subtree
87{
88
89 subtree_type subtree = ST_NONE;
90
91 constexpr Subtree() = default;
92
93 // NOLINTNEXTLINE
94 constexpr Subtree( subtree_type subtree ) : subtree( subtree ) {}
95
97 constexpr explicit Subtree( std::string_view str ) noexcept
98 : subtree( ( str == "legacyPackages" ) ? ST_LEGACY
99 : ( str == "packages" ) ? ST_PACKAGES
100 : ST_NONE )
101 {}
102
103
105 [[nodiscard]] static Subtree
106 parseSubtree( std::string_view str )
107 {
108 return Subtree { ( str == "legacyPackages" ) ? ST_LEGACY
109 : ( str == "packages" )
110 ? ST_PACKAGES
111 : throw std::invalid_argument(
112 "Invalid subtree '" + std::string( str ) + "'" ) };
113 }
114
115
117 [[nodiscard]] friend constexpr std::string_view
118 to_string( const Subtree & subtree )
119 {
120 switch ( subtree.subtree )
121 {
122 case ST_LEGACY: return "legacyPackages";
123 case ST_PACKAGES: return "packages";
124 default: return "ST_NONE";
125 }
126 }
127
129 constexpr explicit
130 operator std::string_view() const
131 {
132 return to_string( *this );
133 }
134
135 // NOLINTNEXTLINE
136 constexpr
137 operator subtree_type() const
138 {
139 return this->subtree;
140 }
141
143 [[nodiscard]] constexpr bool
144 operator==( const Subtree & other ) const
145 = default;
146
148 [[nodiscard]] constexpr bool
149 operator!=( const Subtree & other ) const
150 = default;
151
153 [[nodiscard]] constexpr bool
154 operator==( const subtree_type & other ) const
155 {
156 return this->subtree == other;
157 }
158
160 [[nodiscard]] constexpr bool
161 operator!=( const subtree_type & other ) const
162 {
163 return this->subtree != other;
164 }
165
166}; /* End struct `Subtree' */
167
168
169/* -------------------------------------------------------------------------- */
170
172inline void
173from_json( const nlohmann::json & jfrom, Subtree & subtree )
174{
175 jfrom.get_to( subtree.subtree );
176}
177
179inline void
180to_json( nlohmann::json & jto, const Subtree & subtree )
181{
182 to_json( jto, subtree.subtree );
183}
184
185
186/* -------------------------------------------------------------------------- */
187
188} // namespace flox
189
190
191/* -------------------------------------------------------------------------- *
192 *
193 *
194 *
195 * ========================================================================== */
Interfaces for use by flox.
Definition: command.cc:29
std::shared_ptr< nix::eval_cache::AttrCursor > MaybeCursor
A std::shared_ptr<nix::eval_cache::AttrCursor> which may be nullptr.
Definition: types.hh:47
subtree_type
A top level key in a nix flake.
Definition: types.hh:68
nix::ref< nix::eval_cache::AttrCursor > Cursor
A non-nullptr std::shared_ptr<nix::eval_cache::AttrCursor>.
Definition: types.hh:50
NLOHMANN_JSON_SERIALIZE_ENUM(subtree_type, { { ST_NONE, nullptr }, { ST_LEGACY, "legacyPackages" }, { ST_PACKAGES, "packages" } }) struct Subtree
A strongly typed wrapper over an attribute path subtree name, which is the first element of an attrib...
Definition: types.hh:78
std::string System
A system pair indicating architecture and platform.
Definition: types.hh:62
std::vector< std::optional< std::string > > AttrPathGlob
An attribute path which may contain null members to represent globs.
Definition: types.hh:41
void from_json(const nlohmann::json &jfrom, RawPackage &pkg)
Convert a JSON object to a flox::RawPackage.
Definition: raw-package.cc:27
void to_json(nlohmann::json &jto, const FloxException &err)
Convert a flox::FloxException to a JSON object.
Definition: exceptions.cc:27
std::vector< std::string > AttrPath
A list of key names addressing a location in a nested JSON-like object.
Definition: types.hh:33