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_LOGGER* parent) {
DC_SCOPED(DC_LOGGER) log = DC_LOGGER_NEW(parent, "%s", __func__);
DC_LOG(log, DC_INFO, "creating success result");
DC_SCOPED(example_result)
success_result = example_result_from_ok((struct example_data){
.value = 42,
.description = strdup("A successful result"),
});
DC_LOG(log, DC_INFO, "creating error results");
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);
DC_LOG(log, DC_INFO, "success: %s", DC_DEBUG(example_result_debug, &success_result));
DC_LOG(log, DC_INFO, "error: %s", DC_DEBUG(example_result_debug, &error_result));
DC_LOG(log, DC_INFO, "invalid: %s", DC_DEBUG(example_result_debug, &invalid_result));
DC_LOG(log, DC_INFO, "permission: %s", DC_DEBUG(example_result_debug, &permission_result));
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);
DC_LOG(log, DC_INFO, "all assertions passed");
}
int main() {
root = NS(DC_LOGGER,
new_global)((NS(DC_LOGGER, global_config)){.stream = stdout, .ansi_colours = true},
(dc_log_id){"result"});
return 0;
}
int main()
Definition alloc.c:120
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
#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 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_result_example(DC_LOGGER *parent)
Definition result.c:56
#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