robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
c668ce02e5
master
1#pragma once 2 3#include <stdbool.h> 4#include <stddef.h> 5 6#include "nvim/api/private/defs.h" // IWYU pragma: keep 7#include "nvim/api/private/helpers.h" 8#include "nvim/assert_defs.h" 9#include "nvim/macros_defs.h" 10 11#define VALIDATE (cond ,fmt_ ,fmt_arg1 ,code ) \ 12 do { \ 13 if (!(cond)) { \ 14 api_set_error(err, kErrorTypeValidation, fmt_, fmt_arg1); \ 15 code; \ 16 } \ 17 } while (0) 18 19#define VALIDATE_R (cond ,name ,code ) \ 20 do { \ 21 if (!(cond)) { \ 22 api_err_required(err, name); \ 23 code; \ 24 } \ 25 } while (0) 26 27/// Shows a "not allowed with" message, for mutually-exclusive args. 28#define VALIDATE_CON (cond ,name ,name2 ,code ) \ 29 do { \ 30 if (!(cond)) { \ 31 api_err_conflict(err, name, name2); \ 32 code; \ 33 } \ 34 } while (0) 35 36#define VALIDATE_INT (cond ,name ,val_ ,code ) \ 37 do { \ 38 if (!(cond)) { \ 39 api_err_invalid(err, name, NULL, val_, false); \ 40 code; \ 41 } \ 42 } while (0) 43 44#define VALIDATE_S (cond ,name ,val_ ,code ) \ 45 do { \ 46 if (!(cond)) { \ 47 api_err_invalid(err, name, val_, 0, true); \ 48 code; \ 49 } \ 50 } while (0) 51 52#define VALIDATE_EXP (cond ,name ,expected ,actual ,code ) \ 53 do { \ 54 if (!(cond)) { \ 55 api_err_exp(err, name, expected, actual); \ 56 code; \ 57 } \ 58 } while (0) 59 60#define VALIDATE_T (name ,expected_t ,actual_t ,code ) \ 61 do { \ 62 STATIC_ASSERT(expected_t != kObjectTypeDict, "use VALIDATE_T_DICT"); \ 63 if (expected_t != actual_t) { \ 64 api_err_exp(err, name, api_typename(expected_t), api_typename(actual_t)); \ 65 code; \ 66 } \ 67 } while (0) 68 69/// Checks that `obj_` has type `expected_t`. 70#define VALIDATE_T2 (obj_ ,expected_t ,code ) \ 71 do { \ 72 STATIC_ASSERT(expected_t != kObjectTypeDict, "use VALIDATE_T_DICT"); \ 73 if ((obj_).type != expected_t) { \ 74 api_err_exp(err, STR(obj_), api_typename(expected_t), api_typename((obj_).type)); \ 75 code; \ 76 } \ 77 } while (0) 78 79/// Checks that `obj_` has Dict type. Also allows empty Array in a Lua context. 80#define VALIDATE_T_DICT (name ,obj_ ,code ) \ 81 do { \ 82 if ((obj_).type != kObjectTypeDict \ 83 && !(channel_id == LUA_INTERNAL_CALL \ 84 && (obj_).type == kObjectTypeArray \ 85 && (obj_).data.array.size == 0)) { \ 86 api_err_exp(err, name, api_typename(kObjectTypeDict), api_typename((obj_).type)); \ 87 code; \ 88 } \ 89 } while (0) 90 91/// Checks that actual_t is either the correct handle type or a type erased handle (integer) 92#define VALIDATE_T_HANDLE (name ,expected_t ,actual_t ,code ) \ 93 do { \ 94 if (expected_t != actual_t && kObjectTypeInteger != actual_t) { \ 95 api_err_exp(err, name, api_typename(expected_t), api_typename(actual_t)); \ 96 code; \ 97 } \ 98 } while (0) 99 100#define VALIDATE_RANGE (cond ,name ,code ) \ 101 do { \ 102 if (!(cond)) { \ 103 api_err_invalid(err, name, "out of range", 0, false); \ 104 code; \ 105 } \ 106 } while (0) 107 108#include "api/private/validate.h.generated.h"