aboutsummaryrefslogtreecommitdiff
path: root/src/libutil
diff options
context:
space:
mode:
authorBen Burdette <bburdette@gmail.com>2020-03-22 12:25:47 -0600
committerBen Burdette <bburdette@gmail.com>2020-03-22 12:25:47 -0600
commitf694f43d7ddd270706e6e3a5f034336a5a83fb86 (patch)
treede1181b10ac533a048738887b7b197a643785667 /src/libutil
parentef74fafc0368944e6cfc3b804b4bcdddd6bcf9c0 (diff)
straightforward port of rust mockup code
Diffstat (limited to 'src/libutil')
-rw-r--r--src/libutil/error.cc137
-rw-r--r--src/libutil/error.hh57
2 files changed, 194 insertions, 0 deletions
diff --git a/src/libutil/error.cc b/src/libutil/error.cc
new file mode 100644
index 000000000..4af4691d4
--- /dev/null
+++ b/src/libutil/error.cc
@@ -0,0 +1,137 @@
+#include "error.hh"
+
+#include <iostream>
+
+namespace nix {
+
+using std::cout;
+using std::endl;
+
+// return basic_format?
+string showErrLine(ErrLine &errLine)
+{
+ if (errLine.columnRange.has_value())
+ {
+ return (format("(%1%:%2%)") % errLine.lineNumber % errLine.columnRange->start).str();
+ }
+ else
+ {
+ return (format("(%1%)") % errLine.lineNumber).str();
+ };
+}
+
+void print_code_lines(string &prefix, NixCode &nix_code)
+{
+ if (nix_code.errLine.has_value())
+ {
+ // previous line of code.
+ if (nix_code.errLine->prevLineOfCode.has_value()) {
+ cout << format("%1% %2$+5d%| %3%") % prefix % (nix_code.errLine->lineNumber - 1) % *nix_code.errLine->prevLineOfCode;
+ }
+
+ // line of code containing the error.
+ cout << format("%1% %2$+5d%| %3%") % prefix % (nix_code.errLine->lineNumber) % nix_code.errLine->errLineOfCode;
+
+ // error arrows for the column range.
+ if (nix_code.errLine->columnRange.has_value())
+ {
+ int start = nix_code.errLine->columnRange->start;
+ std::string spaces;
+ for (int i = 0; i < start; ++i)
+ {
+ spaces.append(" ");
+ }
+
+ int len = nix_code.errLine->columnRange->len;
+ std::string arrows;
+ for (int i = 0; i < len; ++i)
+ {
+ arrows.append("^");
+ }
+
+ cout << format("%1% |%2%%3%") % prefix % spaces % arrows;
+ }
+
+
+ // next line of code.
+ if (nix_code.errLine->nextLineOfCode.has_value()) {
+ cout << format("%1% %2$+5d%| %3%") % prefix % (nix_code.errLine->lineNumber + 1) % *nix_code.errLine->nextLineOfCode;
+ }
+
+ }
+ else
+ {
+ }
+
+}
+
+void print_error(ErrorInfo &einfo)
+{
+ int errwidth = 80;
+ string prefix = " ";
+
+ string level_string;
+ switch (einfo.level)
+ {
+ case ErrLevel::elError:
+ {
+ level_string = "error:"; // TODO make red.
+ break;
+ }
+ case ErrLevel::elWarning:
+ {
+ level_string = "warning:"; // TODO make yellow.
+ break;
+ }
+ }
+
+ int ndl = level_string.length() + 3 + einfo.errName.length() + einfo.toolName.length();
+ int dashwidth = errwidth - 3 ? 3 : 80 - ndl;
+
+ string dashes;
+ for (int i = 0; i < dashwidth; ++i)
+ dashes.append("-");
+
+ // divider.
+ cout << format("%1%%2% %3% %4% %5% %6%")
+ % prefix
+ % level_string
+ % "---"
+ % einfo.errName
+ % dashes
+ % einfo.toolName;
+
+ // filename.
+ if (einfo.nixCode.has_value())
+ {
+ if (einfo.nixCode->nixFile.has_value())
+ {
+ string eline = einfo.nixCode->errLine.has_value()
+ ? string(" ") + showErrLine(*einfo.nixCode->errLine)
+ : "";
+
+ cout << format("%1%in file: %2%%3%") % prefix % *einfo.nixCode->nixFile % eline;
+ cout << prefix << endl;
+ }
+ else
+ {
+ cout << format("%1%from command line argument") % prefix << endl;
+ cout << prefix << endl;
+ }
+ }
+
+ // description
+ cout << prefix << einfo.description << endl;
+ cout << prefix << endl;
+
+ // lines of code.
+ if (einfo.nixCode.has_value())
+ print_code_lines(prefix, *einfo.nixCode);
+
+ // hint
+ cout << prefix << einfo.hint << endl;
+ cout << prefix << endl;
+
+}
+
+}
diff --git a/src/libutil/error.hh b/src/libutil/error.hh
new file mode 100644
index 000000000..0bc1f4e24
--- /dev/null
+++ b/src/libutil/error.hh
@@ -0,0 +1,57 @@
+#pragma once
+
+#include "types.hh"
+#include <string>
+#include <optional>
+// #include <boost/format.hpp>
+
+using std::string;
+using std::optional;
+// using boost::format;
+
+namespace nix {
+
+enum ErrLevel
+ { elWarning
+ , elError
+ };
+
+class ColumnRange {
+ public:
+ unsigned int start;
+ unsigned int len;
+};
+
+class ErrLine {
+ public:
+ int lineNumber;
+ optional<ColumnRange> columnRange;
+ optional<string> prevLineOfCode;
+ string errLineOfCode;
+ optional<string> nextLineOfCode;
+};
+
+class NixCode {
+ public:
+ optional<string> nixFile;
+ optional<ErrLine> errLine;
+};
+
+
+class ErrorInfo {
+ public:
+ ErrLevel level;
+ string errName;
+ string description;
+ string toolName;
+ optional<NixCode> nixCode;
+ string hint;
+};
+
+string showErrLine(ErrLine &errLine);
+
+void print_code_lines(string &prefix, NixCode &nix_code);
+
+void print_error(ErrorInfo &einfo);
+}
+