added Result struct

This commit is contained in:
Timerix 2025-07-21 16:22:57 +03:00
parent 7e5b1ea9bb
commit 8999fda11c
2 changed files with 36 additions and 0 deletions

26
include/tlibc/errors.h Executable file
View File

@ -0,0 +1,26 @@
#pragma once
#include "std.h"
#include "string/cstr.h"
typedef struct Result_ {
cstr error;
union {
u64 v_u64;
i64 v_i64;
f32 v_f32;
f64 v_f64;
void* v_ptr;
};
} Result_;
#define Result(T) Result_
char* _genErrorMessage(cstr file, i32 line, cstr func, char* msg, bool free_msg);
#define _genErrorMessageHere(MSG, FREE_MSG) \
_genErrorMessage(__FILE__, __LINE__, __func__, MSG, FREE_MSG)
#define RESULT_ERROR(MSG, FREE_MSG) (Result_){ .error = _genErrorMessageHere(MSG, FREE_MSG) }
#define RESULT_VOID (Result_){ .error = NULL }
#define RESULT_VALUE(TYPE, V) (Result_){ .error = NULL, .v_##TYPE = V }

10
src/errors.c Executable file
View File

@ -0,0 +1,10 @@
#include "tlibc/errors.h"
#define ERRMSG_LENGTH 1024
char* _genErrorMessage(cstr file, i32 line, cstr func, char* msg, bool free_msg){
char* result = sprintf_malloc(1024, "[%s:%i] %s() throwed error: %s", file, line, func, msg);
if(free_msg)
free(msg);
return result;
}