Made stringToSeverity(ore robust && Refactored if->swittch

This commit is contained in:
Shikhar Vashistha 2021-09-27 04:59:24 +00:00 committed by Kamil Śliwak
parent 830d740359
commit 053689d25d
2 changed files with 29 additions and 11 deletions

View File

@ -33,6 +33,8 @@
#include <boost/preprocessor/facilities/overload.hpp> #include <boost/preprocessor/facilities/overload.hpp>
#include <string> #include <string>
#include <regex>
#include <optional>
#include <utility> #include <utility>
#include <vector> #include <vector>
#include <memory> #include <memory>
@ -242,9 +244,17 @@ public:
return "Error"; return "Error";
} }
static std::optional<std::string> stringToSeverity(Severity _severity) static std::optional<Severity> stringToSeverity(std::string _severity)
{ {
return _severity ? std::optional<std::string>{"Error, Warning, Info"} : std::nullopt; _severity = tolower(_severity);
_severity = std::regex_replace(_severity, std::regex("^ +| +$|( ) +"), "$1");
_severity.erase(_severity.begin(), std::find_if(_severity.begin(), _severity.end(), std::bind1st(std::not_equal_to<char>(), ' ')));
_severity[0] = toupper(_severity[0]);
return std::make_optional<Severity>(_severity).has_value() ? std::optional<Severity>(_severity).value() : std::nullopt;
} }
static std::string formatErrorSeverityLowercase(Severity _severity) static std::string formatErrorSeverityLowercase(Severity _severity)

View File

@ -55,6 +55,13 @@ std::string SourceReferenceFormatter::formatErrorInformation(Error const& _error
); );
} }
enum class Severity//have to define if used in switch
{
Error,
Warning,
Info
};
AnsiColorized SourceReferenceFormatter::normalColored() const AnsiColorized SourceReferenceFormatter::normalColored() const
{ {
return AnsiColorized(m_stream, m_colored, {WHITE}); return AnsiColorized(m_stream, m_colored, {WHITE});
@ -181,18 +188,19 @@ void SourceReferenceFormatter::printExceptionInformation(SourceReferenceExtracto
// exception header line // exception header line
if (_msg.errorId.has_value()) if (_msg.errorId.has_value())
{ {
if (_msg.severity == "Error") switch(stringToSeverity(_msg.severity))
errorColored() << " (" << _msg.errorId.value().error << ")"; {
case Severity::Error : errorColored() << " (" << _msg.errorId.value().error << ")";
break;
else if (_msg.severity == "Warning") case Severity::Warning : warningColored() << " (" << _msg.errorId.value().error << ")";
warningColored() << " (" << _msg.errorId.value().error << ")"; break;
else if (_msg.severity == "Info") case Severity::Info : infoColored() << " (" << _msg.errorId.value().error << ")";
infoColored() << " (" << _msg.errorId.value().error << ")"; break;
else default : unknownColored() << " (" << _msg.errorId.value().error << ")";
unknownColored() << " (" << _msg.errorId.value().error << ")"; }
}
printSourceLocation(_msg.primary); printSourceLocation(_msg.primary);