libutil: refactor Pos::getSource to Origin
Change-Id: I1501fceb9ff1e768c8f5c45b65d5638568babf63
This commit is contained in:
+30
-23
@@ -2,6 +2,35 @@
|
||||
|
||||
namespace nix {
|
||||
|
||||
std::optional<std::string> Pos::Origin::getSource() const
|
||||
{
|
||||
using OptStr = std::optional<std::string>;
|
||||
|
||||
// clang-format off
|
||||
return std::visit(overloaded {
|
||||
[](std::monostate const &) -> OptStr {
|
||||
return std::nullopt;
|
||||
},
|
||||
[](Pos::Stdin const & s) -> OptStr {
|
||||
return *s.source;
|
||||
},
|
||||
[](Pos::String const & s) -> OptStr {
|
||||
return *s.source;
|
||||
},
|
||||
[](CheckedSourcePath const & path) -> OptStr {
|
||||
try {
|
||||
return path.readFile();
|
||||
} catch (Error const &) {
|
||||
return std::nullopt;
|
||||
}
|
||||
},
|
||||
[](Hidden const &) -> OptStr {
|
||||
return std::nullopt;
|
||||
},
|
||||
}, *this);
|
||||
// clang-format on
|
||||
}
|
||||
|
||||
Pos::Pos(const Pos * other)
|
||||
{
|
||||
if (!other) {
|
||||
@@ -44,29 +73,7 @@ std::optional<LinesOfCode> Pos::getCodeLines() const
|
||||
|
||||
std::optional<std::string> Pos::getSource() const
|
||||
{
|
||||
return std::visit(overloaded {
|
||||
[](const std::monostate &) -> std::optional<std::string> {
|
||||
return std::nullopt;
|
||||
},
|
||||
[](const Pos::Stdin & s) -> std::optional<std::string> {
|
||||
// Get rid of the null terminators added by the parser.
|
||||
return std::string(s.source->c_str());
|
||||
},
|
||||
[](const Pos::String & s) -> std::optional<std::string> {
|
||||
// Get rid of the null terminators added by the parser.
|
||||
return std::string(s.source->c_str());
|
||||
},
|
||||
[](const CheckedSourcePath & path) -> std::optional<std::string> {
|
||||
try {
|
||||
return path.readFile();
|
||||
} catch (Error &) {
|
||||
return std::nullopt;
|
||||
}
|
||||
},
|
||||
[](Hidden) -> std::optional<std::string> {
|
||||
return std::nullopt;
|
||||
}
|
||||
}, origin);
|
||||
return this->origin.getSource();
|
||||
}
|
||||
|
||||
void Pos::print(std::ostream & out, bool showOrigin) const
|
||||
|
||||
+13
-1
@@ -38,7 +38,19 @@ struct Pos
|
||||
auto operator<=>(const Hidden &) const = default;
|
||||
};
|
||||
|
||||
typedef std::variant<std::monostate, Stdin, String, CheckedSourcePath, Hidden> Origin;
|
||||
struct Origin : std::variant<std::monostate, Stdin, String, CheckedSourcePath, Hidden>
|
||||
{
|
||||
using variant = variant;
|
||||
|
||||
// Forward all construction to std::variant.
|
||||
template<typename... Args>
|
||||
constexpr Origin(Args &&... rhs) noexcept(noexcept(variant(std::forward<Args>(rhs)...)))
|
||||
: variant(std::forward<Args>(rhs)...)
|
||||
{
|
||||
}
|
||||
|
||||
std::optional<std::string> getSource() const;
|
||||
};
|
||||
|
||||
Origin origin = std::monostate();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user