Derive-C
Loading...
Searching...
No Matches
utils/result.c

Examples for using the result type.

Examples for using the result type.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum error_kind {
};
static char const* error_kind_to_str(enum error_kind self) {
switch (self) {
return "NOT_FOUND";
return "INVALID";
return "PERMISSION_DENIED";
}
return "UNKNOWN";
}
static void error_kind_debug(enum error_kind const* self, dc_debug_fmt /* fmt */, FILE* stream) {
fprintf(stream, "error_kind::%s", error_kind_to_str(*self));
}
struct example_data {
int value;
char* description;
};
static void example_data_delete(struct example_data* self) { free(self->description); }
static void example_data_debug(struct example_data const* self, dc_debug_fmt fmt, FILE* stream) {
fprintf(stream, "example_data@%p {\n", (void*)self);
dc_debug_fmt_print(fmt, stream, "value: %d,\n", self->value);
dc_debug_fmt_print(fmt, stream, "description: %s,\n", self->description);
}
#define OK struct example_data
#define OK_DELETE example_data_delete
#define OK_DEBUG example_data_debug
#define ERROR enum error_kind
#define ERROR_DEBUG error_kind_debug
#define NAME example_result
static void example_result_example() {
DC_SCOPED(example_result)
success_result = example_result_from_ok((struct example_data){
.value = 42,
.description = strdup("A successful result"),
});
DC_SCOPED(example_result) error_result = example_result_from_error(ERROR_KIND_NOT_FOUND);
DC_SCOPED(example_result) invalid_result = example_result_from_error(ERROR_KIND_INVALID);
DC_SCOPED(example_result)
permission_result = example_result_from_error(ERROR_KIND_PERMISSION_DENIED);
example_result_debug(&success_result, dc_debug_fmt_new(), stdout);
fprintf(stdout, "\n");
example_result_debug(&error_result, dc_debug_fmt_new(), stdout);
fprintf(stdout, "\n");
example_result_debug(&invalid_result, dc_debug_fmt_new(), stdout);
fprintf(stdout, "\n");
example_result_debug(&permission_result, dc_debug_fmt_new(), stdout);
fprintf(stdout, "\n");
DC_ASSERT(!example_result_is_error(&success_result));
DC_ASSERT(example_result_is_error(&error_result));
DC_ASSERT(example_result_is_error(&invalid_result));
DC_ASSERT(example_result_is_error(&permission_result));
struct example_data const* ok_data = example_result_get_okay(&success_result);
DC_ASSERT(ok_data != NULL);
DC_ASSERT(ok_data->value == 42);
DC_ASSERT(strcmp(ok_data->description, "A successful result") == 0);
enum error_kind const* error_data = example_result_get_error(&error_result);
DC_ASSERT(error_data != NULL);
enum error_kind const* invalid_data = example_result_get_error(&invalid_result);
DC_ASSERT(invalid_data != NULL);
DC_ASSERT(*invalid_data == ERROR_KIND_INVALID);
enum error_kind const* permission_data = example_result_get_error(&permission_result);
DC_ASSERT(permission_data != NULL);
DC_ASSERT(example_result_get_okay(&error_result) == NULL);
DC_ASSERT(example_result_get_error(&success_result) == NULL);
}
int main() {
return 0;
}
int main()
Definition alloc.c:109
static void example_data_debug(struct example_data const *self, dc_debug_fmt fmt, FILE *stream)
Definition arena.c:19
static void example_data_delete(struct example_data *self)
Definition arena.c:17
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 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
#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