robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #38309 from justinmk/doc2c668ce02e5

master
3.4 KiB101 linesraw
1#include <inttypes.h>
2#include <stdio.h>
3#include <string.h>
4
5#include "nvim/api/private/defs.h"
6#include "nvim/api/private/helpers.h"
7#include "nvim/api/private/validate.h"
8#include "nvim/ascii_defs.h"
9#include "nvim/globals.h"
10
11/// Creates "Invalid …" message and sets it on `err`.
12void api_err_invalid(Error *err, const char *name, const char *val_s, int64_t val_n, bool quote_val)
13{
14  ErrorType errtype = kErrorTypeValidation;
15  // Treat `name` without whitespace as a parameter (surround in quotes).
16  // Treat `name` with whitespace as a description (no quotes).
17  const char *has_space = strchr(name, ' ');
18
19  // No value.
20  if (val_s && val_s[0] == NUL) {
21    api_set_error(err, errtype, has_space ? "Invalid %s" : "Invalid '%s'", name);
22    return;
23  }
24
25  // Number value.
26  if (val_s == NULL) {
27    api_set_error(err, errtype, has_space ? "Invalid %s: %" PRId64 : "Invalid '%s': %" PRId64,
28                  name, val_n);
29    return;
30  }
31
32  // String value.
33  if (has_space) {
34    api_set_error(err, errtype, quote_val ? "Invalid %s: '%s'" : "Invalid %s: %s", name, val_s);
35  } else {
36    api_set_error(err, errtype, quote_val ? "Invalid '%s': '%s'" : "Invalid '%s': %s", name, val_s);
37  }
38}
39
40/// Creates "Invalid …: expected …" message and sets it on `err`.
41void api_err_exp(Error *err, const char *name, const char *expected, const char *actual)
42{
43  ErrorType errtype = kErrorTypeValidation;
44  // Treat `name` without whitespace as a parameter (surround in quotes).
45  // Treat `name` with whitespace as a description (no quotes).
46  const char *has_space = strchr(name, ' ');
47
48  if (!actual) {
49    api_set_error(err, errtype,
50                  has_space ? "Invalid %s: expected %s" : "Invalid '%s': expected %s",
51                  name, expected);
52    return;
53  }
54
55  api_set_error(err, errtype,
56                has_space ? "Invalid %s: expected %s, got %s" : "Invalid '%s': expected %s, got %s",
57                name, expected, actual);
58}
59
60/// Creates "Required: …" message and sets it on `err`.
61void api_err_required(Error *err, const char *name)
62{
63  ErrorType errtype = kErrorTypeValidation;
64  // Treat `name` without whitespace as a parameter (surround in quotes).
65  // Treat `name` with whitespace as a description (no quotes).
66  const char *has_space = strchr(name, ' ');
67
68  api_set_error(err, errtype, has_space ? "Required: %s" : "Required: '%s'", name);
69}
70
71/// Creates "Conflict: … not allowed with …" message and sets it on `err`.
72void api_err_conflict(Error *err, const char *name, const char *name2)
73{
74  ErrorType errtype = kErrorTypeValidation;
75  // Treat `name` without whitespace as a parameter (surround in quotes).
76  // Treat `name` with whitespace as a description (no quotes).
77  const char *has_space2 = strchr(name2, ' ');
78
79  api_set_error(err, errtype, has_space2
80                ? "Conflict: '%s' not allowed with %s"
81                : "Conflict: '%s' not allowed with '%s'",
82                name, name2);
83}
84
85bool check_string_array(Array arr, char *name, bool disallow_nl, Error *err)
86{
87  snprintf(IObuff, sizeof(IObuff), "'%s' item", name);
88  for (size_t i = 0; i < arr.size; i++) {
89    VALIDATE_T(IObuff, kObjectTypeString, arr.items[i].type, {
90      return false;
91    });
92    // Disallow newlines in the middle of the line.
93    if (disallow_nl) {
94      const String l = arr.items[i].data.string;
95      VALIDATE(!memchr(l.data, NL, l.size), "'%s' item contains newlines", name, {
96        return false;
97      });
98    }
99  }
100  return true;
101}