robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
c668ce02e5
master
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{ 14ErrorType errtype = kErrorTypeValidation ; 15// Treat `name` without whitespace as a parameter (surround in quotes). 16// Treat `name` with whitespace as a description (no quotes). 17const char * has_space = strchr (name ,' ' ); 18 19// No value. 20if (val_s && val_s [0 ]== NUL ) { 21api_set_error (err ,errtype ,has_space ?"Invalid %s" :"Invalid '%s'" ,name ); 22return ; 23 } 24 25// Number value. 26if (val_s == NULL ) { 27api_set_error (err ,errtype ,has_space ?"Invalid %s: %" PRId64 :"Invalid '%s': %" PRId64 , 28name ,val_n ); 29return ; 30 } 31 32// String value. 33if (has_space ) { 34api_set_error (err ,errtype ,quote_val ?"Invalid %s: '%s'" :"Invalid %s: %s" ,name ,val_s ); 35 }else { 36api_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{ 43ErrorType errtype = kErrorTypeValidation ; 44// Treat `name` without whitespace as a parameter (surround in quotes). 45// Treat `name` with whitespace as a description (no quotes). 46const char * has_space = strchr (name ,' ' ); 47 48if (!actual ) { 49api_set_error (err ,errtype , 50has_space ?"Invalid %s: expected %s" :"Invalid '%s': expected %s" , 51name ,expected ); 52return ; 53 } 54 55api_set_error (err ,errtype , 56has_space ?"Invalid %s: expected %s, got %s" :"Invalid '%s': expected %s, got %s" , 57name ,expected ,actual ); 58} 59 60/// Creates "Required: …" message and sets it on `err`. 61void api_err_required (Error * err ,const char * name ) 62{ 63ErrorType errtype = kErrorTypeValidation ; 64// Treat `name` without whitespace as a parameter (surround in quotes). 65// Treat `name` with whitespace as a description (no quotes). 66const char * has_space = strchr (name ,' ' ); 67 68api_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{ 74ErrorType errtype = kErrorTypeValidation ; 75// Treat `name` without whitespace as a parameter (surround in quotes). 76// Treat `name` with whitespace as a description (no quotes). 77const char * has_space2 = strchr (name2 ,' ' ); 78 79api_set_error (err ,errtype ,has_space2 80 ?"Conflict: '%s' not allowed with %s" 81 :"Conflict: '%s' not allowed with '%s'" , 82name ,name2 ); 83} 84 85bool check_string_array (Array arr ,char * name ,bool disallow_nl ,Error * err ) 86{ 87snprintf (IObuff ,sizeof (IObuff ),"'%s' item" ,name ); 88for (size_t i = 0 ;i < arr .size ;i ++ ) { 89VALIDATE_T (IObuff ,kObjectTypeString ,arr .items [i ].type , { 90return false; 91 }); 92// Disallow newlines in the middle of the line. 93if (disallow_nl ) { 94const String l = arr .items [i ].data .string ; 95VALIDATE (!memchr (l .data ,NL ,l .size ),"'%s' item contains newlines" ,name , { 96return false; 97 }); 98 } 99 } 100return true; 101}