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