Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
suggestions.hh
Go to the documentation of this file.
1#pragma once
3
4#include "nix/util/types.hh"
5#include <set>
6
7namespace nix {
8
9int levenshteinDistance(std::string_view first, std::string_view second);
10
15{
16public:
19 std::string suggestion;
20
21 std::string to_string() const;
22
23 bool operator==(const Suggestion &) const = default;
24 auto operator<=>(const Suggestion &) const = default;
25};
26
28{
29public:
30 std::set<Suggestion> suggestions;
31
32 std::string to_string() const;
33
34 Suggestions trim(int limit = 5, int maxDistance = 2) const;
35
36 static Suggestions bestMatches(const StringSet & allMatches, std::string_view query);
37
38 Suggestions & operator+=(const Suggestions & other);
39};
40
41std::ostream & operator<<(std::ostream & str, const Suggestion &);
42std::ostream & operator<<(std::ostream & str, const Suggestions &);
43
47template<typename T>
48class OrSuggestions
49{
50public:
51 using Raw = std::variant<T, Suggestions>;
52
53 Raw raw;
54
55 T * operator->()
56 {
57 return &**this;
58 }
59
60 T & operator*()
61 {
62 return std::get<T>(raw);
63 }
64
65 operator bool() const noexcept
66 {
67 return std::holds_alternative<T>(raw);
68 }
69
70 OrSuggestions(T t)
71 : raw(t)
72 {
73 }
74
75 OrSuggestions()
76 : raw(Suggestions{})
77 {
78 }
79
80 static OrSuggestions<T> failed(const Suggestions & s)
81 {
82 auto res = OrSuggestions<T>();
83 res.raw = s;
84 return res;
85 }
86
87 static OrSuggestions<T> failed()
88 {
89 return OrSuggestions<T>::failed(Suggestions{});
90 }
91
92 const Suggestions & getSuggestions()
93 {
94 static Suggestions noSuggestions;
95 if (const auto & suggestions = std::get_if<Suggestions>(&raw))
96 return *suggestions;
97 else
98 return noSuggestions;
99 }
100};
101
102} // namespace nix
Definition suggestions.hh:15
int distance
The smaller the better.
Definition suggestions.hh:18
Definition suggestions.hh:28
std::set< std::string, std::less<> > StringSet
Definition types.hh:45