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 ()
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 ( )
static
Examples
utils/result.c.

Definition at line 56 of file result.c.

56 {
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}
static DC_PUBLIC dc_debug_fmt dc_debug_fmt_new()
Definition fmt.h:15
#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_DEBUG_TRACE
Definition debug.h:17

◆ main()

int main ( )

Definition at line 105 of file result.c.

105 {
107 return 0;
108}
static void example_result_example()
Definition result.c:56