client/Response: refactor FormatError() to use libfmt
This commit is contained in:
parent
18efda719e
commit
0d97eba7a5
|
@ -70,19 +70,17 @@ Response::WriteBinary(ConstBuffer<void> payload) noexcept
|
|||
void
|
||||
Response::Error(enum ack code, const char *msg) noexcept
|
||||
{
|
||||
FormatError(code, "%s", msg);
|
||||
FmtError(code, FMT_STRING("{}"), msg);
|
||||
}
|
||||
|
||||
void
|
||||
Response::FormatError(enum ack code, const char *fmt, ...) noexcept
|
||||
Response::VFmtError(enum ack code,
|
||||
fmt::string_view format_str, fmt::format_args args) noexcept
|
||||
{
|
||||
Fmt(FMT_STRING("ACK [{}@{}] {{{}}} "),
|
||||
(int)code, list_index, command);
|
||||
|
||||
std::va_list args;
|
||||
va_start(args, fmt);
|
||||
FormatV(fmt, args);
|
||||
va_end(args);
|
||||
VFmt(format_str, std::move(args));
|
||||
|
||||
Write("\n");
|
||||
}
|
||||
|
|
|
@ -105,7 +105,23 @@ public:
|
|||
bool WriteBinary(ConstBuffer<void> payload) noexcept;
|
||||
|
||||
void Error(enum ack code, const char *msg) noexcept;
|
||||
void FormatError(enum ack code, const char *fmt, ...) noexcept;
|
||||
|
||||
void VFmtError(enum ack code,
|
||||
fmt::string_view format_str, fmt::format_args args) noexcept;
|
||||
|
||||
template<typename S, typename... Args>
|
||||
void FmtError(enum ack code,
|
||||
const S &format_str, Args&&... args) noexcept {
|
||||
#if FMT_VERSION >= 70000
|
||||
return VFmtError(code, fmt::to_string_view(format_str),
|
||||
fmt::make_args_checked<Args...>(format_str,
|
||||
args...));
|
||||
#else
|
||||
/* expensive fallback for older libfmt versions */
|
||||
const auto result = fmt::format(format_str, args...);
|
||||
return Error(code, result.c_str());
|
||||
#endif
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -325,9 +325,9 @@ command_check_request(const struct command *cmd, Response &r,
|
|||
unsigned permission, Request args) noexcept
|
||||
{
|
||||
if (cmd->permission != (permission & cmd->permission)) {
|
||||
r.FormatError(ACK_ERROR_PERMISSION,
|
||||
"you don't have permission for \"%s\"",
|
||||
cmd->cmd);
|
||||
r.FmtError(ACK_ERROR_PERMISSION,
|
||||
FMT_STRING("you don't have permission for \"{}\""),
|
||||
cmd->cmd);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -338,17 +338,19 @@ command_check_request(const struct command *cmd, Response &r,
|
|||
return true;
|
||||
|
||||
if (min == max && unsigned(max) != args.size) {
|
||||
r.FormatError(ACK_ERROR_ARG,
|
||||
"wrong number of arguments for \"%s\"",
|
||||
cmd->cmd);
|
||||
r.FmtError(ACK_ERROR_ARG,
|
||||
FMT_STRING("wrong number of arguments for \"{}\""),
|
||||
cmd->cmd);
|
||||
return false;
|
||||
} else if (args.size < unsigned(min)) {
|
||||
r.FormatError(ACK_ERROR_ARG,
|
||||
"too few arguments for \"%s\"", cmd->cmd);
|
||||
r.FmtError(ACK_ERROR_ARG,
|
||||
FMT_STRING("too few arguments for \"{}\""),
|
||||
cmd->cmd);
|
||||
return false;
|
||||
} else if (max >= 0 && args.size > unsigned(max)) {
|
||||
r.FormatError(ACK_ERROR_ARG,
|
||||
"too many arguments for \"%s\"", cmd->cmd);
|
||||
r.FmtError(ACK_ERROR_ARG,
|
||||
FMT_STRING("too many arguments for \"{}\""),
|
||||
cmd->cmd);
|
||||
return false;
|
||||
} else
|
||||
return true;
|
||||
|
@ -360,8 +362,8 @@ command_checked_lookup(Response &r, unsigned permission,
|
|||
{
|
||||
const struct command *cmd = command_lookup(cmd_name);
|
||||
if (cmd == nullptr) {
|
||||
r.FormatError(ACK_ERROR_UNKNOWN,
|
||||
"unknown command \"%s\"", cmd_name);
|
||||
r.FmtError(ACK_ERROR_UNKNOWN,
|
||||
FMT_STRING("unknown command \"{}\""), cmd_name);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
|
|
@ -34,6 +34,8 @@
|
|||
#include "util/ASCII.hxx"
|
||||
#include "song/Filter.hxx"
|
||||
|
||||
#include <fmt/format.h>
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
|
@ -183,8 +185,8 @@ handle_count(Client &client, Request args, Response &r)
|
|||
const char *s = args[args.size - 1];
|
||||
group = tag_name_parse_i(s);
|
||||
if (group == TAG_NUM_OF_ITEM_TYPES) {
|
||||
r.FormatError(ACK_ERROR_ARG,
|
||||
"Unknown tag type: %s", s);
|
||||
r.FmtError(ACK_ERROR_ARG,
|
||||
FMT_STRING("Unknown tag type: {}"), s);
|
||||
return CommandResult::ERROR;
|
||||
}
|
||||
|
||||
|
@ -252,8 +254,8 @@ handle_list(Client &client, Request args, Response &r)
|
|||
|
||||
const auto tagType = tag_name_parse_i(tag_name);
|
||||
if (tagType == TAG_NUM_OF_ITEM_TYPES) {
|
||||
r.FormatError(ACK_ERROR_ARG,
|
||||
"Unknown tag type: %s", tag_name);
|
||||
r.FmtError(ACK_ERROR_ARG,
|
||||
FMT_STRING("Unknown tag type: {}"), tag_name);
|
||||
return CommandResult::ERROR;
|
||||
}
|
||||
|
||||
|
@ -266,9 +268,9 @@ handle_list(Client &client, Request args, Response &r)
|
|||
args.front()[0] != '(') {
|
||||
/* for compatibility with < 0.12.0 */
|
||||
if (tagType != TAG_ALBUM) {
|
||||
r.FormatError(ACK_ERROR_ARG,
|
||||
"should be \"%s\" for 3 arguments",
|
||||
tag_item_names[TAG_ALBUM]);
|
||||
r.FmtError(ACK_ERROR_ARG,
|
||||
FMT_STRING("should be \"{}\" for 3 arguments"),
|
||||
tag_item_names[TAG_ALBUM]);
|
||||
return CommandResult::ERROR;
|
||||
}
|
||||
|
||||
|
@ -281,8 +283,8 @@ handle_list(Client &client, Request args, Response &r)
|
|||
const char *s = args[args.size - 1];
|
||||
const auto group = tag_name_parse_i(s);
|
||||
if (group == TAG_NUM_OF_ITEM_TYPES) {
|
||||
r.FormatError(ACK_ERROR_ARG,
|
||||
"Unknown tag type: %s", s);
|
||||
r.FmtError(ACK_ERROR_ARG,
|
||||
FMT_STRING("Unknown tag type: {}"), s);
|
||||
return CommandResult::ERROR;
|
||||
}
|
||||
|
||||
|
|
|
@ -407,8 +407,9 @@ handle_idle(Client &client, Request args, Response &r)
|
|||
for (const char *i : args) {
|
||||
unsigned event = idle_parse_name(i);
|
||||
if (event == 0) {
|
||||
r.FormatError(ACK_ERROR_ARG,
|
||||
"Unrecognized idle event: %s", i);
|
||||
r.FmtError(ACK_ERROR_ARG,
|
||||
FMT_STRING("Unrecognized idle event: {}"),
|
||||
i);
|
||||
return CommandResult::ERROR;
|
||||
}
|
||||
|
||||
|
|
|
@ -25,6 +25,8 @@
|
|||
#include "queue/Playlist.hxx"
|
||||
#include "util/ConstBuffer.hxx"
|
||||
|
||||
#include <fmt/format.h>
|
||||
|
||||
CommandResult
|
||||
handle_addtagid(Client &client, Request args, Response &r)
|
||||
{
|
||||
|
@ -33,7 +35,8 @@ handle_addtagid(Client &client, Request args, Response &r)
|
|||
const char *const tag_name = args[1];
|
||||
const TagType tag_type = tag_name_parse_i(tag_name);
|
||||
if (tag_type == TAG_NUM_OF_ITEM_TYPES) {
|
||||
r.FormatError(ACK_ERROR_ARG, "Unknown tag type: %s", tag_name);
|
||||
r.FmtError(ACK_ERROR_ARG, FMT_STRING("Unknown tag type: {}"),
|
||||
tag_name);
|
||||
return CommandResult::ERROR;
|
||||
}
|
||||
|
||||
|
@ -53,8 +56,9 @@ handle_cleartagid(Client &client, Request args, Response &r)
|
|||
const char *const tag_name = args[1];
|
||||
tag_type = tag_name_parse_i(tag_name);
|
||||
if (tag_type == TAG_NUM_OF_ITEM_TYPES) {
|
||||
r.FormatError(ACK_ERROR_ARG,
|
||||
"Unknown tag type: %s", tag_name);
|
||||
r.FmtError(ACK_ERROR_ARG,
|
||||
FMT_STRING("Unknown tag type: {}"),
|
||||
tag_name);
|
||||
return CommandResult::ERROR;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue