#ifndef COMMON_RESULT_H
#define COMMON_RESULT_H

#include <stdbool.h>

#define RESULT(Value, Error, struct_name)                                      \
	typedef struct {                                                           \
		bool ok;                                                               \
		union {                                                                \
			Value value;                                                       \
			Error error;                                                       \
		};                                                                     \
	} struct_name;

#define RESULT_CTORS(Value, Error, struct_name, function_prefix)               \
	static inline struct_name function_prefix##_ok(Value value)                \
	{                                                                          \
		return (struct_name) {                                                 \
			.ok = true,                                                        \
			.value = value,                                                    \
		};                                                                     \
	}                                                                          \
                                                                               \
	static inline struct_name function_prefix##_error(Error error)             \
	{                                                                          \
		return (struct_name) {                                                 \
			.ok = false,                                                       \
			.error = error,                                                    \
		};                                                                     \
	}

#define RESULT_WITH_CTORS(Value, Error, struct_name, function_prefix)          \
	RESULT(Value, Error, struct_name)                                          \
	RESULT_CTORS(Value, Error, struct_name, function_prefix)

#endif