Derive-C
Loading...
Searching...
No Matches
result.c
Go to the documentation of this file.
1
4
5#include <derive-c/prelude.h>
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9
15
16static char const* error_kind_to_str(enum error_kind self) {
17 switch (self) {
19 return "NOT_FOUND";
21 return "INVALID";
23 return "PERMISSION_DENIED";
24 }
25 return "UNKNOWN";
26}
27
28static void error_kind_debug(enum error_kind const* self, dc_debug_fmt /* fmt */, FILE* stream) {
29 fprintf(stream, "error_kind::%s", error_kind_to_str(*self));
30}
31
32struct example_data {
33 int value;
34 char* description;
35};
36
37static void example_data_delete(struct example_data* self) { free(self->description); }
38
39static void example_data_debug(struct example_data const* self, dc_debug_fmt fmt, FILE* stream) {
40 fprintf(stream, "example_data@%p {\n", (void*)self);
41 fmt = dc_debug_fmt_scope_begin(fmt);
42 dc_debug_fmt_print(fmt, stream, "value: %d,\n", self->value);
43 dc_debug_fmt_print(fmt, stream, "description: %s,\n", self->description);
44 fmt = dc_debug_fmt_scope_end(fmt);
45 dc_debug_fmt_print(fmt, stream, "}");
46}
47
48#define OK struct example_data
49#define OK_DELETE example_data_delete
50#define OK_DEBUG example_data_debug
51#define ERROR enum error_kind
52#define ERROR_DEBUG error_kind_debug
53#define NAME example_result
55
58
59 DC_SCOPED(example_result)
60 success_result = example_result_from_ok((struct example_data){
61 .value = 42,
62 .description = strdup("A successful result"),
63 });
64
65 DC_SCOPED(example_result) error_result = example_result_from_error(ERROR_KIND_NOT_FOUND);
66 DC_SCOPED(example_result) invalid_result = example_result_from_error(ERROR_KIND_INVALID);
67 DC_SCOPED(example_result)
68 permission_result = example_result_from_error(ERROR_KIND_PERMISSION_DENIED);
69
70 example_result_debug(&success_result, dc_debug_fmt_new(), stdout);
71 fprintf(stdout, "\n");
72 example_result_debug(&error_result, dc_debug_fmt_new(), stdout);
73 fprintf(stdout, "\n");
74 example_result_debug(&invalid_result, dc_debug_fmt_new(), stdout);
75 fprintf(stdout, "\n");
76 example_result_debug(&permission_result, dc_debug_fmt_new(), stdout);
77 fprintf(stdout, "\n");
78
79 DC_ASSERT(!example_result_is_error(&success_result));
80 DC_ASSERT(example_result_is_error(&error_result));
81 DC_ASSERT(example_result_is_error(&invalid_result));
82 DC_ASSERT(example_result_is_error(&permission_result));
83
84 struct example_data const* ok_data = example_result_get_okay(&success_result);
85 DC_ASSERT(ok_data != NULL);
86 DC_ASSERT(ok_data->value == 42);
87 DC_ASSERT(strcmp(ok_data->description, "A successful result") == 0);
88
89 enum error_kind const* error_data = example_result_get_error(&error_result);
90 DC_ASSERT(error_data != NULL);
91 DC_ASSERT(*error_data == ERROR_KIND_NOT_FOUND);
92
93 enum error_kind const* invalid_data = example_result_get_error(&invalid_result);
94 DC_ASSERT(invalid_data != NULL);
95 DC_ASSERT(*invalid_data == ERROR_KIND_INVALID);
96
97 enum error_kind const* permission_data = example_result_get_error(&permission_result);
98 DC_ASSERT(permission_data != NULL);
99 DC_ASSERT(*permission_data == ERROR_KIND_PERMISSION_DENIED);
100
101 DC_ASSERT(example_result_get_okay(&error_result) == NULL);
102 DC_ASSERT(example_result_get_error(&success_result) == NULL);
103}
104
105int main() {
107 return 0;
108}
static DC_PUBLIC void dc_debug_fmt_print(dc_debug_fmt fmt, FILE *stream, const char *format,...)
Definition fmt.h:32
static DC_PUBLIC dc_debug_fmt dc_debug_fmt_new()
Definition fmt.h:15
static DC_PUBLIC dc_debug_fmt dc_debug_fmt_scope_end(dc_debug_fmt fmt)
Definition fmt.h:57
static DC_PUBLIC dc_debug_fmt dc_debug_fmt_scope_begin(dc_debug_fmt fmt)
Definition fmt.h:50
#define DC_ASSERT(expr,...)
Definition panic.h:37
static void example_data_debug(struct example_data const *self, dc_debug_fmt fmt, FILE *stream)
Definition result.c:39
static void error_kind_debug(enum error_kind const *self, dc_debug_fmt, FILE *stream)
Definition result.c:28
static char const * error_kind_to_str(enum error_kind self)
Definition result.c:16
static void example_result_example()
Definition result.c:56
error_kind
Definition result.c:10
@ ERROR_KIND_INVALID
Definition result.c:12
@ ERROR_KIND_NOT_FOUND
Definition result.c:11
@ ERROR_KIND_PERMISSION_DENIED
Definition result.c:13
static void example_data_delete(struct example_data *self)
Definition result.c:37
int main()
Definition result.c:105
#define DC_SCOPED(type,...)
RAII in C. Call the destructor when the variable goes out of scope.
Definition scope.h:5
Debug format helpers for debug printin data structures.
Definition fmt.h:11
int value
Definition arena.c:14
char * description
Definition arena.c:13
#define DC_DEBUG_TRACE
Definition debug.h:17
static DC_PUBLIC FILE * stream(SELF *self)
Definition template.h:108