1
1
/* *****************************************************************************
2
- Copyright: Boaz Segev, 2019-2024
2
+ Copyright: Boaz Segev, 2019-2025
3
3
License: ISC / MIT (choose your license)
4
4
5
5
Feel free to copy, use and enjoy according to the license provided.
@@ -40669,9 +40669,9 @@ RESP Parser Settings
40669
40669
40670
40670
/* RESP's parser type - do not access directly. */
40671
40671
typedef struct {
40672
- void *(*get_null)(void) ;
40673
- void *(*get_true)(void) ;
40674
- void *(*get_false)(void) ;
40672
+ void *set_null ;
40673
+ void *set_true ;
40674
+ void *set_false ;
40675
40675
void *(*get_number)(int64_t i);
40676
40676
void *(*get_float)(double f);
40677
40677
void *(*get_bignum)(char *str, size_t len);
@@ -40684,6 +40684,10 @@ typedef struct {
40684
40684
void *(*map_push)(void *map, void *key, void *value);
40685
40685
/* returns non-zero on error. */
40686
40686
int (*done)(void *udata, void *response);
40687
+ /** Called on error response, NOT on protocol error. */
40688
+ int (*error)(void *udata, void *response);
40689
+ /** Called after either response callbacks or protocol error. */
40690
+ void (*free_response)(void *obj);
40687
40691
} fio_resp3_settings_s;
40688
40692
40689
40693
/* *****************************************************************************
@@ -40692,22 +40696,27 @@ RESP Parser API
40692
40696
40693
40697
/* RESP's parser type - do not access directly. */
40694
40698
typedef struct fio_resp3_s {
40699
+ /** callback settings. */
40700
+ fio_resp3_settings_s settings;
40695
40701
struct {
40696
- /** callback settings. */
40697
- fio_resp3_settings_s *settings;
40698
40702
/** current parsing function. */
40699
40703
size_t (*parse)(struct fio_resp3_s *, uint8_t *, size_t);
40700
40704
/** Stack's depth */
40701
40705
uint16_t depth;
40702
- struct {
40703
- void *data;
40704
- uint32_t expected;
40705
- uint8_t typ;
40706
- } stack[FIO_RESP3_MAX_NESTING];
40707
40706
} private_data;
40707
+ struct {
40708
+ void *data;
40709
+ uint32_t expected;
40710
+ uint8_t typ;
40711
+ } private_stack[FIO_RESP3_MAX_NESTING];
40708
40712
void *udata;
40709
40713
} fio_resp3_s;
40710
40714
40715
+ #define FIO_RESP3_INIT(...) \
40716
+ (fio_resp3_s) { \
40717
+ .settings = {__VA_ARGS__}, .private_data = {0}, .udata = NULL \
40718
+ }
40719
+
40711
40720
/** Returns an initialized parser. */
40712
40721
FIO_IFUNC fio_resp3_s fio_resp3_init(fio_resp3_settings_s *settings,
40713
40722
void *udata);
@@ -40719,18 +40728,69 @@ FIO_IFUNC void fio_resp3_init2(fio_resp3_s *dest,
40719
40728
/** Parse `data`, returning the abount of bytes consumed. */
40720
40729
FIO_IFUNC size_t fio_resp3_parse(fio_resp3_s *parser);
40721
40730
40731
+ /* *****************************************************************************
40732
+ Validating RESP3 Settings.
40733
+ ***************************************************************************** */
40734
+
40735
+ /* clang-format off */
40736
+ static void *fio___resp3_get_number(int64_t i) { (void)i; }
40737
+ static void *fio___resp3_get_float(double f) { (void)f; }
40738
+ static void *fio___resp3_get_bignum(char *str, size_t len) { (void)str, (void)len; }
40739
+ static void *fio___resp3_get_string(char *str, size_t len) { (void)str, (void)len; }
40740
+ static void *fio___resp3_str_start(size_t soft_expected) { (void)soft_expected; }
40741
+ static void *fio___resp3_str(void *d, char *s, size_t l) { (void)d, (void)s, (void)l; }
40742
+ static void *fio___resp3_arr_start(size_t soft_expected) { (void)soft_expected; }
40743
+ static void *fio___resp3_arr_push(void *a, void *v) { (void)a, (void)v; }
40744
+ static void *fio___resp3_map_start(size_t soft_expected) { (void)soft_expected; }
40745
+ static void *fio___resp3_map(void *m, void *k, void *v) { (void)m, (void)k, (void)v; }
40746
+ static int fio___resp3_done(void *u, void *r) { (void)u, (void)r; }
40747
+ static int fio___resp3_err(void *u, void *r) { (void)u, (void)r; }
40748
+ static void fio___resp3_free(void *r) { (void)r; }
40749
+ /* clang-format on */
40750
+
40751
+ static void fio___resp3_validate_settings(fio_resp3_settings_s *settings) {
40752
+ static const fio_resp3_settings_s defaults = {
40753
+ .set_null = NULL,
40754
+ .set_true = (void *)1,
40755
+ .set_false = NULL,
40756
+ .get_number = fio___resp3_get_number,
40757
+ .get_float = fio___resp3_get_float,
40758
+ .get_bignum = fio___resp3_get_bignum,
40759
+ .get_string = fio___resp3_get_string,
40760
+ .string_start = fio___resp3_str_start,
40761
+ .string_write = fio___resp3_str,
40762
+ .array_start = fio___resp3_arr_start,
40763
+ .array_push = fio___resp3_arr_push,
40764
+ .map_start = fio___resp3_map_start,
40765
+ .map_push = fio___resp3_map,
40766
+ .done = fio___resp3_done,
40767
+ .error = fio___resp3_err,
40768
+ .free_response = fio___resp3_free,
40769
+ };
40770
+ union {
40771
+ uintptr_t *ptr;
40772
+ fio_resp3_settings_s *s;
40773
+ } src, dest;
40774
+ src.s = (fio_resp3_settings_s *)&defaults;
40775
+ dest.s = settings;
40776
+ for (int i = 0; i < sizeof(fio_resp3_settings_s) / sizeof(uintptr_t); ++i)
40777
+ if (!dest.ptr[i])
40778
+ dest.ptr[i] = src.ptr[i];
40779
+ }
40780
+
40722
40781
/* *****************************************************************************
40723
40782
RESP Implementation - inline functions.
40724
40783
***************************************************************************** */
40725
40784
40726
- SFUNC size_t fio___resp3_parse_start(fio_resp3_s *parser,
40727
- uint8_t *buf,
40728
- size_t len);
40785
+ FIO_SFUNC size_t fio___resp3_parse_start(fio_resp3_s *parser,
40786
+ uint8_t *buf,
40787
+ size_t len);
40729
40788
/** Returns an initialized parser. */
40730
40789
FIO_IFUNC fio_resp3_s fio_resp3_init(fio_resp3_settings_s *settings,
40731
40790
void *udata) {
40732
40791
fio_resp3_s r;
40733
- r.private_data.settings = settings;
40792
+ fio___resp3_validate_settings(settings);
40793
+ r.settings = *settings;
40734
40794
r.private_data.parse = fio___resp3_parse_start;
40735
40795
r.private_data.depth = 0;
40736
40796
r.udata = udata;
@@ -40741,7 +40801,8 @@ FIO_IFUNC fio_resp3_s fio_resp3_init(fio_resp3_settings_s *settings,
40741
40801
FIO_IFUNC void fio_resp3_init2(fio_resp3_s *dest,
40742
40802
fio_resp3_settings_s *settings,
40743
40803
void *udata) {
40744
- dest->private_data.settings = settings;
40804
+ fio___resp3_validate_settings(settings);
40805
+ dest->settings = *settings;
40745
40806
dest->private_data.parse = fio___resp3_parse_start;
40746
40807
dest->private_data.depth = 0;
40747
40808
dest->udata = udata;
0 commit comments