13#include <initializer_list>
20#include <nix/attrs.hh>
21#include <nix/flake/flakeref.hh>
22#include <nlohmann/json.hpp>
41 using Ts::operator()...;
59template<
typename A,
typename B>
60struct adl_serializer<std::variant<A, B>>
65 to_json( json & jto,
const std::variant<A, B> & var )
67 if ( std::holds_alternative<A>( var ) ) { jto = std::get<A>( var ); }
68 else { jto = std::get<B>( var ); }
73 from_json(
const json & jfrom, std::variant<A, B> & var )
77 var = jfrom.template get<A>();
81 var = jfrom.template get<B>();
103template<
typename A,
typename... Types>
104struct adl_serializer<std::variant<A, Types...>>
109 to_json( json & jto,
const std::variant<A, Types...> & var )
113 std::visit( [&](
auto unwrapped ) ->
void { jto = unwrapped; }, var );
118 from_json(
const json & jfrom, std::variant<A, Types...> & var )
123 var = jfrom.template get<A>();
128 using next_variant = std::variant<Types...>;
131 next_variant next = jfrom.template get<next_variant>();
132 std::visit( [&](
auto unwrapped ) ->
void { var = unwrapped; }, next );
143struct adl_serializer<nix::fetchers::Attrs>
148 to_json( json & jto,
const nix::fetchers::Attrs & attrs )
151 jto = nix::fetchers::attrsToJSON( attrs );
156 from_json(
const json & jfrom, nix::fetchers::Attrs & attrs )
159 attrs = nix::fetchers::jsonToAttrs( jfrom );
169struct adl_serializer<nix::FlakeRef>
174 to_json( json & jto,
const nix::FlakeRef & ref )
177 jto = nix::fetchers::attrsToJSON( ref.toAttrs() );
184 if ( jfrom.is_object() )
186 return { nix::FlakeRef::fromAttrs(
187 nix::fetchers::jsonToAttrs( jfrom ) ) };
189 return { nix::parseFlakeRef( jfrom.get<std::string>() ) };
207inline static const std::vector<std::string> &
210 static const std::vector<std::string> defaultSystems
211 = {
"x86_64-linux",
"aarch64-linux",
"x86_64-darwin",
"aarch64-darwin" };
212 return defaultSystems;
217inline static const std::vector<std::string> &
220 static const std::vector<std::string> defaultSubtrees
221 = {
"packages",
"legacyPackages" };
222 return defaultSubtrees;
293std::vector<std::string>
305isUInt( std::string_view str );
317hasPrefix( std::string_view prefix, std::string_view str );
324ltrim( std::string & str );
328rtrim( std::string & str );
332trim( std::string & str );
336[[nodiscard]] std::string
340[[nodiscard]] std::string
344[[nodiscard]] std::string
363template<
typename Exception = FloxException>
366 const std::string & who =
"JSON value" )
368 if ( ! value.is_object() )
370 std::stringstream oss;
371 oss <<
"expected " << who <<
" to be an object, but found "
372 << ( value.is_array() ?
"an" :
"a" ) <<
' ' << value.type_name()
374 throw Exception( oss.str() );
393 std::vector<T> merged = higher;
394 for (
const auto & value : lower )
396 if ( std::find( merged.begin(), merged.end(), value ) == merged.end() )
398 merged.emplace_back( value );
412[[nodiscard]] std::vector<std::optional<T>>
415 std::vector<std::optional<T>> rsl;
416 for (
const T & val : orig )
418 rsl.emplace_back( std::make_optional<T>( val ) );
Definitions of various std::exception children used for throwing errors with nice messages and typed ...
Interfaces for use by flox.
Definition: command.cc:29
std::string & trim(std::string &str)
trim from both ends ( in place ).
Definition: util.cc:259
std::string & ltrim(std::string &str)
trim from start ( in place ).
Definition: util.cc:236
std::vector< std::string > splitAttrPath(std::string_view path)
Split an attribute path string.
Definition: util.cc:140
bool hasPrefix(std::string_view prefix, std::string_view str)
Does the string @str have the prefix prefix?
Definition: util.cc:226
std::string & rtrim(std::string &str)
trim from end ( in place ).
Definition: util.cc:247
std::string trim_copy(std::string_view str)
trim from both ends ( copying ).
Definition: util.cc:284
nix::FlakeRef parseFlakeRef(const std::string &flakeRef)
Parse a flake reference from either a JSON attrset or URI string.
Definition: util.cc:76
static const std::vector< std::string > & getDefaultSubtrees()
‘flake’ subtrees to resolve/search in.
Definition: util.hh:218
std::vector< T > merge_vectors(const std::vector< T > &lower, const std::vector< T > &higher)
Merge two std::vector containers by putting all elements of the higher prioirty vector first,...
Definition: util.hh:391
nlohmann::json tomlToJSON(std::string_view toml)
Convert a TOML string to JSON.
static const std::vector< std::string > & getDefaultSystems()
Systems to resolve/search in.
Definition: util.hh:208
std::string displayableGlobbedPath(const flox::AttrPathGlob &attrs)
Convert a AttrPathGlob to a string for display.
Definition: util.cc:309
std::vector< std::optional< T > > vectorMapOptional(const std::vector< T > &orig)
Constructs a std::vector<std::optional<T>> from a std::vector<T>.
Definition: util.hh:413
bool isUInt(std::string_view str)
Is the string @str a positive natural number?
Definition: util.cc:212
std::vector< std::optional< std::string > > AttrPathGlob
An attribute path which may contain null members to represent globs.
Definition: types.hh:41
bool isSQLiteDb(const std::string &dbPath)
Detect if a path is a SQLite3 database file.
Definition: util.cc:35
std::string rtrim_copy(std::string_view str)
trim from end ( copying ).
Definition: util.cc:276
std::string ltrim_copy(std::string_view str)
trim from start ( copying ).
Definition: util.cc:268
static void assertIsJSONObject(const nlohmann::json &value, const std::string &who="JSON value")
Assert that a JSON value is an object, or throw an exception.
Definition: util.hh:365
nlohmann::json parseOrReadJSONObject(const std::string &jsonOrPath)
Parse a JSON object from an inline string or a path to a JSON file.
Definition: util.cc:88
std::string extract_json_errmsg(nlohmann::json::exception &err)
Extract the user-friendly portion of a nlohmann::json::exception.
Definition: util.cc:295
nlohmann::json readAndCoerceJSON(const std::filesystem::path &path)
Read a file and coerce its contents to JSON based on its extension.
Definition: util.cc:102
nlohmann::json yamlToJSON(std::string_view yaml)
Convert a YAML string to JSON.
Extension to the ‘nlohmann::json’ serializer to support additional Argument Dependent Lookup (ADL) ty...
Definition: util.hh:54
static nix::FlakeRef from_json(const json &jfrom)
Move-only conversion of a JSON object to a nix::FlakeRef.
Definition: util.hh:182
static void to_json(json &jto, const nix::FlakeRef &ref)
Convert a nix::FlakeRef to a JSON object.
Definition: util.hh:174
static void to_json(json &jto, const nix::fetchers::Attrs &attrs)
Convert a nix::fetchers::Attrs to a JSON object.
Definition: util.hh:148
static void from_json(const json &jfrom, nix::fetchers::Attrs &attrs)
Convert a JSON object to a nix::fetchers::Attrs.
Definition: util.hh:156
static void from_json(const json &jfrom, std::variant< A, B > &var)
Convert a JSON type to a std::variant<A, B>.
Definition: util.hh:73
static void to_json(json &jto, const std::variant< A, B > &var)
Convert a std::variant<A, B> to a JSON type.
Definition: util.hh:65
static void to_json(json &jto, const std::variant< A, Types... > &var)
Convert a std::variant<A, Types...> to a JSON type.
Definition: util.hh:109
static void from_json(const json &jfrom, std::variant< A, Types... > &var)
Convert a JSON type to a std::variant<Types...>.
Definition: util.hh:118
Helper type for std::visit( overloaded { ... }, x ); pattern.
Definition: util.hh:40
Miscellaneous typedefs and aliases.