result.h revision 1.1 1 1.1 christos /*
2 1.1 christos * Copyright (c) Meta Platforms, Inc. and affiliates.
3 1.1 christos * All rights reserved.
4 1.1 christos *
5 1.1 christos * This source code is licensed under both the BSD-style license (found in the
6 1.1 christos * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 1.1 christos * in the COPYING file in the root directory of this source tree).
8 1.1 christos * You may select, at your option, one of the above-listed licenses.
9 1.1 christos */
10 1.1 christos
11 1.1 christos #ifndef RESULT_H
12 1.1 christos #define RESULT_H
13 1.1 christos
14 1.1 christos #include <stddef.h>
15 1.1 christos
16 1.1 christos /**
17 1.1 christos * The error type enum.
18 1.1 christos */
19 1.1 christos typedef enum {
20 1.1 christos result_error_ok, /**< No error. */
21 1.1 christos result_error_skip, /**< This method was skipped. */
22 1.1 christos result_error_system_error, /**< Some internal error happened. */
23 1.1 christos result_error_compression_error, /**< Compression failed. */
24 1.1 christos result_error_decompression_error, /**< Decompression failed. */
25 1.1 christos result_error_round_trip_error, /**< Data failed to round trip. */
26 1.1 christos } result_error_t;
27 1.1 christos
28 1.1 christos /**
29 1.1 christos * The success type.
30 1.1 christos */
31 1.1 christos typedef struct {
32 1.1 christos size_t total_size; /**< The total compressed size. */
33 1.1 christos } result_data_t;
34 1.1 christos
35 1.1 christos /**
36 1.1 christos * The result type.
37 1.1 christos * Do not access the member variables directory, use the helper functions.
38 1.1 christos */
39 1.1 christos typedef struct {
40 1.1 christos result_error_t internal_error;
41 1.1 christos result_data_t internal_data;
42 1.1 christos } result_t;
43 1.1 christos
44 1.1 christos /**
45 1.1 christos * Create a result of the error type.
46 1.1 christos */
47 1.1 christos static result_t result_error(result_error_t error);
48 1.1 christos /**
49 1.1 christos * Create a result of the success type.
50 1.1 christos */
51 1.1 christos static result_t result_data(result_data_t data);
52 1.1 christos
53 1.1 christos /**
54 1.1 christos * Check if the result is an error or skip.
55 1.1 christos */
56 1.1 christos static int result_is_error(result_t result);
57 1.1 christos /**
58 1.1 christos * Check if the result error is skip.
59 1.1 christos */
60 1.1 christos static int result_is_skip(result_t result);
61 1.1 christos /**
62 1.1 christos * Get the result error or okay.
63 1.1 christos */
64 1.1 christos static result_error_t result_get_error(result_t result);
65 1.1 christos /**
66 1.1 christos * Get the result data. The result MUST be checked with result_is_error() first.
67 1.1 christos */
68 1.1 christos static result_data_t result_get_data(result_t result);
69 1.1 christos
70 1.1 christos static result_t result_error(result_error_t error) {
71 1.1 christos result_t result = {
72 1.1 christos .internal_error = error,
73 1.1 christos };
74 1.1 christos return result;
75 1.1 christos }
76 1.1 christos
77 1.1 christos static result_t result_data(result_data_t data) {
78 1.1 christos result_t result = {
79 1.1 christos .internal_error = result_error_ok,
80 1.1 christos .internal_data = data,
81 1.1 christos };
82 1.1 christos return result;
83 1.1 christos }
84 1.1 christos
85 1.1 christos static int result_is_error(result_t result) {
86 1.1 christos return result_get_error(result) != result_error_ok;
87 1.1 christos }
88 1.1 christos
89 1.1 christos static int result_is_skip(result_t result) {
90 1.1 christos return result_get_error(result) == result_error_skip;
91 1.1 christos }
92 1.1 christos
93 1.1 christos static result_error_t result_get_error(result_t result) {
94 1.1 christos return result.internal_error;
95 1.1 christos }
96 1.1 christos
97 1.1 christos char const* result_get_error_string(result_t result);
98 1.1 christos
99 1.1 christos static result_data_t result_get_data(result_t result) {
100 1.1 christos return result.internal_data;
101 1.1 christos }
102 1.1 christos
103 1.1 christos #endif
104