Derive-C
Loading...
Searching...
No Matches
result.c File Reference
#include <derive-c/prelude.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <derive-c/utils/result/template.h>

Go to the source code of this file.

Data Structures

struct  example_data

Macros

#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

Enumerations

enum  error_kind { ERROR_KIND_NOT_FOUND , ERROR_KIND_INVALID , ERROR_KIND_PERMISSION_DENIED }

Functions

static char const * error_kind_to_str (enum error_kind self)
static void error_kind_debug (enum error_kind const *self, dc_debug_fmt, FILE *stream)
static void example_data_delete (struct example_data *self)
static void example_data_debug (struct example_data const *self, dc_debug_fmt fmt, FILE *stream)
static void example_result_example (DC_LOGGER *parent)
int main ()

Macro Definition Documentation

◆ ERROR

#define ERROR   enum error_kind

Definition at line 51 of file result.c.

◆ ERROR_DEBUG

#define ERROR_DEBUG   error_kind_debug

Definition at line 52 of file result.c.

◆ NAME

#define NAME   example_result

Definition at line 53 of file result.c.

◆ OK

#define OK   struct example_data

Definition at line 48 of file result.c.

◆ OK_DEBUG

#define OK_DEBUG   example_data_debug

Definition at line 50 of file result.c.

◆ OK_DELETE

#define OK_DELETE   example_data_delete

Definition at line 49 of file result.c.

Enumeration Type Documentation

◆ error_kind

enum error_kind
Enumerator
ERROR_KIND_NOT_FOUND 
ERROR_KIND_INVALID 
ERROR_KIND_PERMISSION_DENIED 
Examples
utils/result.c.

Definition at line 10 of file result.c.

10 {
14};
@ ERROR_KIND_INVALID
Definition result.c:12
@ ERROR_KIND_NOT_FOUND
Definition result.c:11
@ ERROR_KIND_PERMISSION_DENIED
Definition result.c:13

Function Documentation

◆ error_kind_debug()

void error_kind_debug ( enum error_kind const * self,
dc_debug_fmt ,
FILE * stream )
static
Examples
utils/result.c.

Definition at line 28 of file result.c.

28 {
29 fprintf(stream, "error_kind::%s", error_kind_to_str(*self));
30}
static char const * error_kind_to_str(enum error_kind self)
Definition result.c:16
static DC_PUBLIC FILE * stream(SELF *self)
Definition template.h:108

◆ error_kind_to_str()

char const * error_kind_to_str ( enum error_kind self)
static
Examples
utils/result.c.

Definition at line 16 of file result.c.

16 {
17 switch (self) {
19 return "NOT_FOUND";
21 return "INVALID";
23 return "PERMISSION_DENIED";
24 }
25 return "UNKNOWN";
26}

◆ example_data_debug()

void example_data_debug ( struct example_data const * self,
dc_debug_fmt fmt,
FILE * stream )
static

Definition at line 39 of file result.c.

39 {
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}
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

◆ example_data_delete()

void example_data_delete ( struct example_data * self)
static

Definition at line 37 of file result.c.

37{ free(self->description); }
char * description
Definition arena.c:13

◆ example_result_example()

void example_result_example ( DC_LOGGER * parent)
static
Examples
utils/result.c.

Definition at line 56 of file result.c.

56 {
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}
#define DC_DEBUG(DEBUG_FN, DEBUG_PTR)
Definition dump.h:92
#define DC_LOGGER
Definition file.h:168
#define DC_ASSERT(expr,...)
Definition panic.h:37
error_kind
Definition result.c:10
#define DC_SCOPED(type,...)
RAII in C. Call the destructor when the variable goes out of scope.
Definition scope.h:5
int value
Definition arena.c:14
#define DC_LOGGER_NEW(...)
Definition prelude.h:21
#define DC_LOG(...)
Definition prelude.h:20
@ DC_INFO
Definition trait.h:8

◆ main()

int main ( )

Definition at line 105 of file result.c.

105 {
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 NS(pre, post)
Definition namespace.h:14
static void example_result_example(DC_LOGGER *parent)
Definition result.c:56