Nix 2.26.3
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 "types.hh"
5#include <set>
6
7namespace nix {
8
9int levenshteinDistance(std::string_view first, std::string_view second);
10
15public:
18 std::string suggestion;
19
20 std::string to_string() const;
21
22 bool operator ==(const Suggestion &) const = default;
23 auto operator <=>(const Suggestion &) const = default;
24};
25
27public:
28 std::set<Suggestion> suggestions;
29
30 std::string to_string() const;
31
32 Suggestions trim(
33 int limit = 5,
34 int maxDistance = 2
35 ) const;
36
37 static Suggestions bestMatches (
38 const std::set<std::string> & allMatches,
39 std::string_view query
40 );
41
42 Suggestions& operator+=(const Suggestions & other);
43};
44
45std::ostream & operator<<(std::ostream & str, const Suggestion &);
46std::ostream & operator<<(std::ostream & str, const Suggestions &);
47
51template<typename T>
52class OrSuggestions {
53public:
54 using Raw = std::variant<T, Suggestions>;
55
56 Raw raw;
57
58 T* operator ->()
59 {
60 return &**this;
61 }
62
63 T& operator *()
64 {
65 return std::get<T>(raw);
66 }
67
68 operator bool() const noexcept
69 {
70 return std::holds_alternative<T>(raw);
71 }
72
73 OrSuggestions(T t)
74 : raw(t)
75 {
76 }
77
78 OrSuggestions()
79 : raw(Suggestions{})
80 {
81 }
82
83 static OrSuggestions<T> failed(const Suggestions & s)
84 {
85 auto res = OrSuggestions<T>();
86 res.raw = s;
87 return res;
88 }
89
90 static OrSuggestions<T> failed()
91 {
92 return OrSuggestions<T>::failed(Suggestions{});
93 }
94
95 const Suggestions & getSuggestions()
96 {
97 static Suggestions noSuggestions;
98 if (const auto & suggestions = std::get_if<Suggestions>(&raw))
99 return *suggestions;
100 else
101 return noSuggestions;
102 }
103
104};
105
106}
Definition suggestions.hh:14
int distance
The smaller the better.
Definition suggestions.hh:17
Definition suggestions.hh:26
return s
Definition lexer.l:459
std::ostream & str
Definition lexer.l:1728
Strings res
Definition lexer.l:2566
T t
Definition lexer.l:154