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