1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-07-06 13:21:47 +02:00

Rename hintfmt to HintFmt

This commit is contained in:
Rebecca Turner 2024-02-03 20:35:19 -08:00
parent 149bd63afb
commit c0e7f50c1a
No known key found for this signature in database
29 changed files with 460 additions and 464 deletions

View file

@ -63,7 +63,7 @@ void setStackSize(rlim_t stackSize)
if (setrlimit(RLIMIT_STACK, &limit) != 0) {
logger->log(
lvlError,
hintfmt(
HintFmt(
"Failed to increase stack size from %1% to %2% (maximum allowed stack size: %3%): %4%",
savedStackSize,
stackSize,

View file

@ -11,7 +11,7 @@
namespace nix {
void BaseError::addTrace(std::shared_ptr<Pos> && e, hintfmt hint, bool frame)
void BaseError::addTrace(std::shared_ptr<Pos> && e, HintFmt hint, bool frame)
{
err.traces.push_front(Trace { .pos = std::move(e), .hint = hint, .frame = frame });
}
@ -37,7 +37,7 @@ const std::string & BaseError::calcWhat() const
std::optional<std::string> ErrorInfo::programName = std::nullopt;
std::ostream & operator <<(std::ostream & os, const hintfmt & hf)
std::ostream & operator <<(std::ostream & os, const HintFmt & hf)
{
return os << hf.str();
}

View file

@ -63,7 +63,7 @@ void printCodeLines(std::ostream & out,
struct Trace {
std::shared_ptr<Pos> pos;
hintfmt hint;
HintFmt hint;
bool frame;
};
@ -74,7 +74,7 @@ inline bool operator>=(const Trace& lhs, const Trace& rhs);
struct ErrorInfo {
Verbosity level;
hintfmt msg;
HintFmt msg;
std::shared_ptr<Pos> pos;
std::list<Trace> traces;
@ -113,20 +113,20 @@ public:
template<typename... Args>
BaseError(unsigned int status, const Args & ... args)
: err { .level = lvlError, .msg = hintfmt(args...), .status = status }
: err { .level = lvlError, .msg = HintFmt(args...), .status = status }
{ }
template<typename... Args>
explicit BaseError(const std::string & fs, const Args & ... args)
: err { .level = lvlError, .msg = hintfmt(fs, args...) }
: err { .level = lvlError, .msg = HintFmt(fs, args...) }
{ }
template<typename... Args>
BaseError(const Suggestions & sug, const Args & ... args)
: err { .level = lvlError, .msg = hintfmt(args...), .suggestions = sug }
: err { .level = lvlError, .msg = HintFmt(args...), .suggestions = sug }
{ }
BaseError(hintfmt hint)
BaseError(HintFmt hint)
: err { .level = lvlError, .msg = hint }
{ }
@ -159,10 +159,10 @@ public:
template<typename... Args>
void addTrace(std::shared_ptr<Pos> && e, std::string_view fs, const Args & ... args)
{
addTrace(std::move(e), hintfmt(std::string(fs), args...));
addTrace(std::move(e), HintFmt(std::string(fs), args...));
}
void addTrace(std::shared_ptr<Pos> && e, hintfmt hint, bool frame = false);
void addTrace(std::shared_ptr<Pos> && e, HintFmt hint, bool frame = false);
bool hasTrace() const { return !err.traces.empty(); }
@ -214,8 +214,8 @@ public:
SysError(int errNo, const Args & ... args)
: SystemError(""), errNo(errNo)
{
auto hf = hintfmt(args...);
err.msg = hintfmt("%1%: %2%", Uncolored(hf.str()), strerror(errNo));
auto hf = HintFmt(args...);
err.msg = HintFmt("%1%: %2%", Uncolored(hf.str()), strerror(errNo));
}
/**

View file

@ -31,6 +31,17 @@ inline void formatHelper(F & f, const T & x, const Args & ... args)
// Interpolate one argument and then recurse.
formatHelper(f % x, args...);
}
/**
* Set the correct exceptions for `fmt`.
*/
void setExceptions(boost::format & fmt)
{
fmt.exceptions(
boost::io::all_error_bits ^
boost::io::too_many_args_bit ^
boost::io::too_few_args_bit);
}
}
/**
@ -74,7 +85,7 @@ template<typename... Args>
inline std::string fmt(const std::string & fs, const Args & ... args)
{
boost::format f(fs);
f.exceptions(boost::io::all_error_bits ^ boost::io::too_many_args_bit);
setExceptions(f);
formatHelper(f, args...);
return f.str();
}
@ -82,9 +93,9 @@ inline std::string fmt(const std::string & fs, const Args & ... args)
/**
* Values wrapped in this struct are printed in magenta.
*
* By default, arguments to `hintfmt` are printed in magenta. To avoid this,
* By default, arguments to `HintFmt` are printed in magenta. To avoid this,
* either wrap the argument in `Uncolored` or add a specialization of
* `hintfmt::operator%`.
* `HintFmt::operator%`.
*/
template <class T>
struct Magenta
@ -102,7 +113,7 @@ std::ostream & operator<<(std::ostream & out, const Magenta<T> & y)
/**
* Values wrapped in this class are printed without coloring.
*
* By default, arguments to `hintfmt` are printed in magenta (see `Magenta`).
* By default, arguments to `HintFmt` are printed in magenta (see `Magenta`).
*/
template <class T>
struct Uncolored
@ -121,65 +132,49 @@ std::ostream & operator<<(std::ostream & out, const Uncolored<T> & y)
* A wrapper around `boost::format` which colors interpolated arguments in
* magenta by default.
*/
class hintfmt
class HintFmt
{
private:
boost::format fmt;
public:
/**
* Construct a `hintfmt` from a format string, with values to be
* interpolated later with `%`.
*
* This isn't exposed as a single-argument constructor to avoid
* accidentally constructing `hintfmt`s with user-controlled strings. See
* the note on `fmt` for more information.
*/
static hintfmt interpolate(const std::string & formatString)
{
hintfmt result((boost::format(formatString)));
result.fmt.exceptions(
boost::io::all_error_bits ^
boost::io::too_many_args_bit ^
boost::io::too_few_args_bit);
return result;
}
/**
* Format the given string literally, without interpolating format
* placeholders.
*/
hintfmt(const std::string & literal)
: hintfmt("%s", Uncolored(literal))
HintFmt(const std::string & literal)
: HintFmt("%s", Uncolored(literal))
{ }
/**
* Interpolate the given arguments into the format string.
*/
template<typename... Args>
hintfmt(const std::string & format, const Args & ... args)
: fmt(format)
{
formatHelper(*this, args...);
}
HintFmt(const std::string & format, const Args & ... args)
: HintFmt(boost::format(format), args...)
{ }
hintfmt(const hintfmt & hf)
HintFmt(const HintFmt & hf)
: fmt(hf.fmt)
{ }
hintfmt(boost::format && fmt)
template<typename... Args>
HintFmt(boost::format && fmt, const Args & ... args)
: fmt(std::move(fmt))
{ }
{
setExceptions(fmt);
formatHelper(*this, args...);
}
template<class T>
hintfmt & operator%(const T & value)
HintFmt & operator%(const T & value)
{
fmt % Magenta(value);
return *this;
}
template<class T>
hintfmt & operator%(const Uncolored<T> & value)
HintFmt & operator%(const Uncolored<T> & value)
{
fmt % value.value;
return *this;
@ -191,6 +186,6 @@ public:
}
};
std::ostream & operator<<(std::ostream & os, const hintfmt & hf);
std::ostream & operator<<(std::ostream & os, const HintFmt & hf);
}

View file

@ -448,7 +448,7 @@ Error readError(Source & source)
auto msg = readString(source);
ErrorInfo info {
.level = level,
.msg = hintfmt(msg),
.msg = HintFmt(msg),
};
auto havePos = readNum<size_t>(source);
assert(havePos == 0);
@ -457,7 +457,7 @@ Error readError(Source & source)
havePos = readNum<size_t>(source);
assert(havePos == 0);
info.traces.push_back(Trace {
.hint = hintfmt(readString(source))
.hint = HintFmt(readString(source))
});
}
return Error(std::move(info));