blob: ffdc36b76e82636e94d81b4c52329be5d0e97dd8 [file] [log] [blame]
Chris Frantz32156692022-06-21 22:03:26 -07001// Copyright lowRISC contributors.
2// Licensed under the Apache License, Version 2.0, see LICENSE for details.
3// SPDX-License-Identifier: Apache-2.0
4
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8
9#include "sw/device/lib/base/status.h"
10#include "sw/device/lib/ujson/example.h"
11
12status_t stdio_getc(void *context) {
13 int ch = fgetc(stdin);
14 return ch == EOF ? RESOURCE_EXHAUSTED() : OK_STATUS(ch);
15}
16
17status_t stdio_putbuf(void *context, const char *buf, size_t len) {
18 fwrite(buf, 1, len, stdout);
19 return OK_STATUS();
20}
21
22status_t roundtrip(const char *name) {
23 ujson_t uj = ujson_init(NULL, stdio_getc, stdio_putbuf);
24 if (!strcmp(name, "foo")) {
25 foo x = {0};
26 TRY(ujson_deserialize_foo(&uj, &x));
27 TRY(ujson_serialize_foo(&uj, &x));
28 } else if (!strcmp(name, "rect")) {
29 rect x = {0};
30 TRY(ujson_deserialize_rect(&uj, &x));
31 TRY(ujson_serialize_rect(&uj, &x));
32 } else if (!strcmp(name, "matrix")) {
33 matrix x = {0};
34 TRY(ujson_deserialize_matrix(&uj, &x));
35 TRY(ujson_serialize_matrix(&uj, &x));
36 } else if (!strcmp(name, "direction")) {
37 direction x = {0};
38 TRY(ujson_deserialize_direction(&uj, &x));
39 TRY(ujson_serialize_direction(&uj, &x));
Chris Frantz664c7192022-09-27 11:04:28 -070040 } else if (!strcmp(name, "fuzzy_bool")) {
41 fuzzy_bool x = {0};
42 fprintf(stderr, "-- fuzzy_bool\n");
43 TRY(ujson_deserialize_fuzzy_bool(&uj, &x));
44 fprintf(stderr, "-- %d\n", (int)x);
45 TRY(ujson_serialize_fuzzy_bool(&uj, &x));
46 fprintf(stderr, "-- done\n");
Chris Frantz32156692022-06-21 22:03:26 -070047 } else if (!strcmp(name, "misc")) {
48 misc_t x = {0};
49 TRY(ujson_deserialize_misc_t(&uj, &x));
50 TRY(ujson_serialize_misc_t(&uj, &x));
51 } else {
52 return INVALID_ARGUMENT();
53 }
54 return OK_STATUS();
55}
56
57int main(int argc, char *argv[]) {
58 if (argc < 2) {
59 fprintf(stderr, "%s [struct-name]", argv[0]);
60 return EXIT_FAILURE;
61 }
62 status_t s = roundtrip(argv[1]);
63
64 return status_ok(s) ? EXIT_SUCCESS : EXIT_FAILURE;
65}