diff --git a/lix/libutil/position.cc b/lix/libutil/position.cc index bdce67001..3f35884ab 100644 --- a/lix/libutil/position.cc +++ b/lix/libutil/position.cc @@ -2,6 +2,35 @@ namespace nix { +std::optional Pos::Origin::getSource() const +{ + using OptStr = std::optional; + + // 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 Pos::getCodeLines() const std::optional Pos::getSource() const { - return std::visit(overloaded { - [](const std::monostate &) -> std::optional { - return std::nullopt; - }, - [](const Pos::Stdin & s) -> std::optional { - // Get rid of the null terminators added by the parser. - return std::string(s.source->c_str()); - }, - [](const Pos::String & s) -> std::optional { - // Get rid of the null terminators added by the parser. - return std::string(s.source->c_str()); - }, - [](const CheckedSourcePath & path) -> std::optional { - try { - return path.readFile(); - } catch (Error &) { - return std::nullopt; - } - }, - [](Hidden) -> std::optional { - return std::nullopt; - } - }, origin); + return this->origin.getSource(); } void Pos::print(std::ostream & out, bool showOrigin) const diff --git a/lix/libutil/position.hh b/lix/libutil/position.hh index ce2e8f925..966e7f84c 100644 --- a/lix/libutil/position.hh +++ b/lix/libutil/position.hh @@ -38,7 +38,19 @@ struct Pos auto operator<=>(const Hidden &) const = default; }; - typedef std::variant