aboutsummaryrefslogtreecommitdiff
path: root/src/libutil/suggestions.hh
blob: 3cac1371ed4d8a5136ad988e8a84bb24b3b63347 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#pragma once
///@file

#include "comparator.hh"

#include <set>
#include <string_view>
#include <string>
#include <variant>

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;

    GENERATE_CMP(Suggestion, me->distance, me->suggestion)
};

class Suggestions {
public:
    std::set<Suggestion> suggestions;

    std::string to_string() const;

    Suggestions trim(
        int limit = 5,
        int maxDistance = 2
    ) const;

    static Suggestions bestMatches (
        std::set<std::string> allMatches,
        std::string 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;
    }

};

}