mirror of
https://github.com/NixOS/nix.git
synced 2025-12-10 11:01:03 +01:00
Move /src to /subprojects
This will facilitate breaking up Nix into multiple packages for each component with Meson.
This commit is contained in:
parent
4db9487823
commit
84e2963f8e
737 changed files with 504 additions and 505 deletions
106
subprojects/libutil/suggestions.hh
Normal file
106
subprojects/libutil/suggestions.hh
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
#pragma once
|
||||
///@file
|
||||
|
||||
#include "types.hh"
|
||||
#include <set>
|
||||
|
||||
namespace nix {
|
||||
|
||||
int levenshteinDistance(std::string_view first, std::string_view second);
|
||||
|
||||
/**
|
||||
* A potential suggestion for the cli interface.
|
||||
*/
|
||||
class Suggestion {
|
||||
public:
|
||||
/// The smaller the better
|
||||
int distance;
|
||||
std::string suggestion;
|
||||
|
||||
std::string to_string() const;
|
||||
|
||||
bool operator ==(const Suggestion &) const = default;
|
||||
auto operator <=>(const Suggestion &) const = default;
|
||||
};
|
||||
|
||||
class Suggestions {
|
||||
public:
|
||||
std::set<Suggestion> suggestions;
|
||||
|
||||
std::string to_string() const;
|
||||
|
||||
Suggestions trim(
|
||||
int limit = 5,
|
||||
int maxDistance = 2
|
||||
) const;
|
||||
|
||||
static Suggestions bestMatches (
|
||||
const std::set<std::string> & allMatches,
|
||||
std::string_view query
|
||||
);
|
||||
|
||||
Suggestions& operator+=(const Suggestions & other);
|
||||
};
|
||||
|
||||
std::ostream & operator<<(std::ostream & str, const Suggestion &);
|
||||
std::ostream & operator<<(std::ostream & str, const Suggestions &);
|
||||
|
||||
/**
|
||||
* Either a value of type `T`, or some suggestions
|
||||
*/
|
||||
template<typename T>
|
||||
class OrSuggestions {
|
||||
public:
|
||||
using Raw = std::variant<T, Suggestions>;
|
||||
|
||||
Raw raw;
|
||||
|
||||
T* operator ->()
|
||||
{
|
||||
return &**this;
|
||||
}
|
||||
|
||||
T& operator *()
|
||||
{
|
||||
return std::get<T>(raw);
|
||||
}
|
||||
|
||||
operator bool() const noexcept
|
||||
{
|
||||
return std::holds_alternative<T>(raw);
|
||||
}
|
||||
|
||||
OrSuggestions(T t)
|
||||
: raw(t)
|
||||
{
|
||||
}
|
||||
|
||||
OrSuggestions()
|
||||
: raw(Suggestions{})
|
||||
{
|
||||
}
|
||||
|
||||
static OrSuggestions<T> failed(const Suggestions & s)
|
||||
{
|
||||
auto res = OrSuggestions<T>();
|
||||
res.raw = s;
|
||||
return res;
|
||||
}
|
||||
|
||||
static OrSuggestions<T> failed()
|
||||
{
|
||||
return OrSuggestions<T>::failed(Suggestions{});
|
||||
}
|
||||
|
||||
const Suggestions & getSuggestions()
|
||||
{
|
||||
static Suggestions noSuggestions;
|
||||
if (const auto & suggestions = std::get_if<Suggestions>(&raw))
|
||||
return *suggestions;
|
||||
else
|
||||
return noSuggestions;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue