aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/api/private
diff options
context:
space:
mode:
authorJustin M. Keyes <justinkz@gmail.com>2023-02-14 14:19:28 -0500
committerGitHub <noreply@github.com>2023-02-14 11:19:28 -0800
commit556f8646c01d1751cf39fe4df9c622899dceab9d (patch)
tree6262b53753d2f80a3305b76c7d1ec6e6b1f92b37 /src/nvim/api/private
parente03ecb7d31c10aab8a8acbfb3cdd7ec558cd49eb (diff)
downloadrneovim-556f8646c01d1751cf39fe4df9c622899dceab9d.tar.gz
rneovim-556f8646c01d1751cf39fe4df9c622899dceab9d.tar.bz2
rneovim-556f8646c01d1751cf39fe4df9c622899dceab9d.zip
refactor(api): consistent VALIDATE messages #22262
Problem: Validation messages are not consistently formatted. - Parameter names sometimes are NOT quoted. - Descriptive names (non-parameters) sometimes ARE quoted. Solution: Always quote the `name` value passed to a VALIDATE macro _unless_ the value has whitespace.
Diffstat (limited to 'src/nvim/api/private')
-rw-r--r--src/nvim/api/private/validate.c49
-rw-r--r--src/nvim/api/private/validate.h32
2 files changed, 62 insertions, 19 deletions
diff --git a/src/nvim/api/private/validate.c b/src/nvim/api/private/validate.c
index 41c9472a39..c4dd5bcac8 100644
--- a/src/nvim/api/private/validate.c
+++ b/src/nvim/api/private/validate.c
@@ -9,6 +9,55 @@
# include "api/private/validate.c.generated.h"
#endif
+/// Creates "Invalid …" message and sets it on `err`.
+void api_err_invalid(Error *err, const char *name, const char *val_s, int64_t val_n, bool quote_val)
+{
+ ErrorType errtype = kErrorTypeValidation;
+ // Treat `name` without whitespace as a parameter (surround in quotes).
+ // Treat `name` with whitespace as a description (no quotes).
+ char *has_space = strchr(name, ' ');
+
+ // No value.
+ if (val_s && val_s[0] == '\0') {
+ api_set_error(err, errtype, has_space ? "Invalid %s" : "Invalid '%s'", name);
+ return;
+ }
+
+ // Number value.
+ if (val_s == NULL) {
+ api_set_error(err, errtype, has_space ? "Invalid %s: %" PRId64 : "Invalid '%s': %" PRId64,
+ name, val_n);
+ return;
+ }
+
+ // String value.
+ if (has_space) {
+ api_set_error(err, errtype, quote_val ? "Invalid %s: '%s'" : "Invalid %s: %s", name, val_s);
+ } else {
+ api_set_error(err, errtype, quote_val ? "Invalid '%s': '%s'" : "Invalid '%s': %s", name, val_s);
+ }
+}
+
+/// Creates "Invalid …: expected …" message and sets it on `err`.
+void api_err_exp(Error *err, const char *name, const char *expected, const char *actual)
+{
+ ErrorType errtype = kErrorTypeValidation;
+ // Treat `name` without whitespace as a parameter (surround in quotes).
+ // Treat `name` with whitespace as a description (no quotes).
+ char *has_space = strchr(name, ' ');
+
+ if (!actual) {
+ api_set_error(err, errtype,
+ has_space ? "Invalid %s: expected %s" : "Invalid '%s': expected %s",
+ name, expected);
+ return;
+ }
+
+ api_set_error(err, errtype,
+ has_space ? "Invalid %s: expected %s, got %s" : "Invalid '%s': expected %s, got %s",
+ name, expected, actual);
+}
+
bool check_string_array(Array arr, char *name, bool disallow_nl, Error *err)
{
snprintf(IObuff, sizeof(IObuff), "'%s' item", name);
diff --git a/src/nvim/api/private/validate.h b/src/nvim/api/private/validate.h
index 4a1b99408e..469fed0f83 100644
--- a/src/nvim/api/private/validate.h
+++ b/src/nvim/api/private/validate.h
@@ -4,48 +4,42 @@
#include "nvim/api/private/defs.h"
#include "nvim/api/private/helpers.h"
-#define VALIDATE_INT(cond, name, val_, code) \
+#define VALIDATE(cond, fmt_, fmt_arg1, code) \
do { \
if (!(cond)) { \
- api_set_error(err, kErrorTypeValidation, "Invalid " name ": %" PRId64, val_); \
+ api_set_error(err, kErrorTypeValidation, fmt_, fmt_arg1); \
code; \
} \
} while (0)
-#define VALIDATE_S(cond, name, val_, code) \
+#define VALIDATE_INT(cond, name, val_, code) \
do { \
if (!(cond)) { \
- if (strequal(val_, "")) { \
- api_set_error(err, kErrorTypeValidation, "Invalid " name); \
- } else { \
- api_set_error(err, kErrorTypeValidation, "Invalid " name ": '%s'", val_); \
- } \
+ api_err_invalid(err, name, NULL, val_, false); \
code; \
} \
} while (0)
-#define VALIDATE_EXP(cond, name, expected, actual, code) \
+#define VALIDATE_S(cond, name, val_, code) \
do { \
if (!(cond)) { \
- api_set_error(err, kErrorTypeValidation, "Invalid %s: expected %s, got %s", \
- name, expected, actual); \
+ api_err_invalid(err, name, val_, 0, true); \
code; \
} \
} while (0)
-#define VALIDATE_T(name, expected_t, actual_t, code) \
+#define VALIDATE_EXP(cond, name, expected, actual, code) \
do { \
- if (expected_t != actual_t) { \
- api_set_error(err, kErrorTypeValidation, "Invalid %s: expected %s, got %s", \
- name, api_typename(expected_t), api_typename(actual_t)); \
+ if (!(cond)) { \
+ api_err_exp(err, name, expected, actual); \
code; \
} \
} while (0)
-#define VALIDATE(cond, fmt_, fmt_arg1, code) \
+#define VALIDATE_T(name, expected_t, actual_t, code) \
do { \
- if (!(cond)) { \
- api_set_error(err, kErrorTypeValidation, fmt_, fmt_arg1); \
+ if (expected_t != actual_t) { \
+ api_err_exp(err, name, api_typename(expected_t), api_typename(actual_t)); \
code; \
} \
} while (0)
@@ -53,7 +47,7 @@
#define VALIDATE_RANGE(cond, name, code) \
do { \
if (!(cond)) { \
- api_set_error(err, kErrorTypeValidation, "Invalid '%s': out of range", name); \
+ api_err_invalid(err, name, "out of range", 0, false); \
code; \
} \
} while (0)