Skip to content

Commit

Permalink
filter: retain const variant of get_int_elemnet
Browse files Browse the repository at this point in the history
otherwise rust gets bitchy at as

Signed-off-by: William Casarin <[email protected]>
  • Loading branch information
jb55 committed Aug 19, 2024
1 parent 49428e8 commit 9936d15
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
16 changes: 11 additions & 5 deletions src/nostrdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -650,11 +650,17 @@ ndb_filter_get_string_element(const struct ndb_filter *filter, const struct ndb_
}

uint64_t *
ndb_filter_get_int_element(struct ndb_filter_elements *els, int index)
ndb_filter_get_int_element_ptr(struct ndb_filter_elements *els, int index)
{
return &els->elements[index];
}

uint64_t
ndb_filter_get_int_element(const struct ndb_filter_elements *els, int index)
{
return els->elements[index];
}

int ndb_filter_init(struct ndb_filter *filter)
{
struct cursor cur;
Expand Down Expand Up @@ -4839,7 +4845,7 @@ static int cursor_push_json_elem_array(struct cursor *cur,
return 0;
break;
case NDB_ELEMENT_INT:
val = *ndb_filter_get_int_element(elems, i);
val = ndb_filter_get_int_element(elems, i);
if (!cursor_push_int_str(cur, val))
return 0;
break;
Expand Down Expand Up @@ -4910,19 +4916,19 @@ int ndb_filter_json(const struct ndb_filter *filter, char *buf, int buflen)
case NDB_FILTER_SINCE:
if (!cursor_push_str(c, "\"since\":"))
return 0;
if (!cursor_push_int_str(c, *ndb_filter_get_int_element(elems, 0)))
if (!cursor_push_int_str(c, ndb_filter_get_int_element(elems, 0)))
return 0;
break;
case NDB_FILTER_UNTIL:
if (!cursor_push_str(c, "\"until\":"))
return 0;
if (!cursor_push_int_str(c, *ndb_filter_get_int_element(elems, 0)))
if (!cursor_push_int_str(c, ndb_filter_get_int_element(elems, 0)))
return 0;
break;
case NDB_FILTER_LIMIT:
if (!cursor_push_str(c, "\"limit\":"))
return 0;
if (!cursor_push_int_str(c, *ndb_filter_get_int_element(elems, 0)))
if (!cursor_push_int_str(c, ndb_filter_get_int_element(elems, 0)))
return 0;
break;
}
Expand Down
3 changes: 2 additions & 1 deletion src/nostrdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,8 @@ int ndb_filter_from_json(const char *, int len, struct ndb_filter *filter, unsig
// getting field elements
unsigned char *ndb_filter_get_id_element(const struct ndb_filter *, const struct ndb_filter_elements *, int index);
const char *ndb_filter_get_string_element(const struct ndb_filter *, const struct ndb_filter_elements *, int index);
uint64_t *ndb_filter_get_int_element(struct ndb_filter_elements *, int index);
uint64_t ndb_filter_get_int_element(const struct ndb_filter_elements *, int index);
uint64_t *ndb_filter_get_int_element_ptr(struct ndb_filter_elements *, int index);

struct ndb_filter_elements *ndb_filter_current_element(const struct ndb_filter *);
struct ndb_filter_elements *ndb_filter_get_elements(const struct ndb_filter *, int);
Expand Down
8 changes: 4 additions & 4 deletions test.c
Original file line number Diff line number Diff line change
Expand Up @@ -862,17 +862,17 @@ static void test_parse_filter_json()
case 1:
assert(elems->field.type == NDB_FILTER_KINDS);
assert(elems->count == 3);
val = *ndb_filter_get_int_element(elems, 0);
val = ndb_filter_get_int_element(elems, 0);
assert(val == 1);
val = *ndb_filter_get_int_element(elems, 1);
val = ndb_filter_get_int_element(elems, 1);
assert(val == 2);
val = *ndb_filter_get_int_element(elems, 2);
val = ndb_filter_get_int_element(elems, 2);
assert(val == 3);
break;

case 2:
assert(elems->field.type == NDB_FILTER_LIMIT);
val = *ndb_filter_get_int_element(elems, 0);
val = ndb_filter_get_int_element(elems, 0);
assert(val == 10);
break;

Expand Down

0 comments on commit 9936d15

Please sign in to comment.