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