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
56static void example_result_example(DC_LOGGER* parent) {
57 DC_SCOPED(DC_LOGGER) log = DC_LOGGER_NEW(parent, "%s", __func__);
58
59 DC_LOG(log, DC_INFO, "creating success result");
60 DC_SCOPED(example_result)
61 success_result = example_result_from_ok((struct example_data){
62 .value = 42,
63 .description = strdup("A successful result"),
64 });
65
66 DC_LOG(log, DC_INFO, "creating error results");
67 DC_SCOPED(example_result) error_result = example_result_from_error(ERROR_KIND_NOT_FOUND);
68 DC_SCOPED(example_result) invalid_result = example_result_from_error(ERROR_KIND_INVALID);
69 DC_SCOPED(example_result)
70 permission_result = example_result_from_error(ERROR_KIND_PERMISSION_DENIED);
71
72 DC_LOG(log, DC_INFO, "success: %s", DC_DEBUG(example_result_debug, &success_result));
73 DC_LOG(log, DC_INFO, "error: %s", DC_DEBUG(example_result_debug, &error_result));
74 DC_LOG(log, DC_INFO, "invalid: %s", DC_DEBUG(example_result_debug, &invalid_result));
75 DC_LOG(log, DC_INFO, "permission: %s", DC_DEBUG(example_result_debug, &permission_result));
76
77 DC_ASSERT(!example_result_is_error(&success_result));
78 DC_ASSERT(example_result_is_error(&error_result));
79 DC_ASSERT(example_result_is_error(&invalid_result));
80 DC_ASSERT(example_result_is_error(&permission_result));
81
82 struct example_data const* ok_data = example_result_get_okay(&success_result);
83 DC_ASSERT(ok_data != NULL);
84 DC_ASSERT(ok_data->value == 42);
85 DC_ASSERT(strcmp(ok_data->description, "A successful result") == 0);
86
87 enum error_kind const* error_data = example_result_get_error(&error_result);
88 DC_ASSERT(error_data != NULL);
89 DC_ASSERT(*error_data == ERROR_KIND_NOT_FOUND);
90
91 enum error_kind const* invalid_data = example_result_get_error(&invalid_result);
92 DC_ASSERT(invalid_data != NULL);
93 DC_ASSERT(*invalid_data == ERROR_KIND_INVALID);
94
95 enum error_kind const* permission_data = example_result_get_error(&permission_result);
96 DC_ASSERT(permission_data != NULL);
97 DC_ASSERT(*permission_data == ERROR_KIND_PERMISSION_DENIED);
98
99 DC_ASSERT(example_result_get_okay(&error_result) == NULL);
100 DC_ASSERT(example_result_get_error(&success_result) == NULL);
101
102 DC_LOG(log, DC_INFO, "all assertions passed");
103}
104
105int main() {
107 root = NS(DC_LOGGER,
108 new_global)((NS(DC_LOGGER, global_config)){.stream = stdout, .ansi_colours = true},
109 (dc_log_id){"result"});
110
112 return 0;
113}
#define DC_DEBUG(DEBUG_FN, DEBUG_PTR)
Definition dump.h:92
#define DC_LOGGER
Definition file.h:168
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_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 NS(pre, post)
Definition namespace.h:14
#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
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
static void example_result_example(DC_LOGGER *parent)
Definition result.c:56
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_LOGGER_NEW(...)
Definition prelude.h:21
#define DC_LOG(...)
Definition prelude.h:20
@ DC_INFO
Definition trait.h:8
static DC_PUBLIC FILE * stream(SELF *self)
Definition template.h:108