Nix
2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Toggle main menu visibility
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
7
namespace
nix {
8
9
int
levenshteinDistance(std::string_view first, std::string_view second);
10
14
class
Suggestion
15
{
16
public
:
18
int
distance
;
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
27
class
Suggestions
28
{
29
public
:
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
41
std::ostream & operator<<(std::ostream & str,
const
Suggestion
&);
42
std::ostream & operator<<(std::ostream & str,
const
Suggestions
&);
43
47
template
<
typename
T>
48
class
OrSuggestions
49
{
50
public
:
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
nix::Suggestion
Definition
suggestions.hh:15
nix::Suggestion::distance
int distance
The smaller the better.
Definition
suggestions.hh:18
nix::Suggestions
Definition
suggestions.hh:28
types.hh
nix::StringSet
std::set< std::string, std::less<> > StringSet
Definition
types.hh:45
libutil
include
nix
util
suggestions.hh
Generated by
1.18.0