result.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright (c) 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. #ifndef RESULT_H
  11. #define RESULT_H
  12. #include <stddef.h>
  13. /**
  14. * The error type enum.
  15. */
  16. typedef enum {
  17. result_error_ok, /**< No error. */
  18. result_error_skip, /**< This method was skipped. */
  19. result_error_system_error, /**< Some internal error happened. */
  20. result_error_compression_error, /**< Compression failed. */
  21. result_error_decompression_error, /**< Decompression failed. */
  22. result_error_round_trip_error, /**< Data failed to round trip. */
  23. } result_error_t;
  24. /**
  25. * The success type.
  26. */
  27. typedef struct {
  28. size_t total_size; /**< The total compressed size. */
  29. } result_data_t;
  30. /**
  31. * The result type.
  32. * Do not access the member variables directory, use the helper functions.
  33. */
  34. typedef struct {
  35. result_error_t internal_error;
  36. result_data_t internal_data;
  37. } result_t;
  38. /**
  39. * Create a result of the error type.
  40. */
  41. static result_t result_error(result_error_t error);
  42. /**
  43. * Create a result of the success type.
  44. */
  45. static result_t result_data(result_data_t data);
  46. /**
  47. * Check if the result is an error or skip.
  48. */
  49. static int result_is_error(result_t result);
  50. /**
  51. * Check if the result error is skip.
  52. */
  53. static int result_is_skip(result_t result);
  54. /**
  55. * Get the result error or okay.
  56. */
  57. static result_error_t result_get_error(result_t result);
  58. /**
  59. * Get the result data. The result MUST be checked with result_is_error() first.
  60. */
  61. static result_data_t result_get_data(result_t result);
  62. static result_t result_error(result_error_t error) {
  63. result_t result = {
  64. .internal_error = error,
  65. };
  66. return result;
  67. }
  68. static result_t result_data(result_data_t data) {
  69. result_t result = {
  70. .internal_error = result_error_ok,
  71. .internal_data = data,
  72. };
  73. return result;
  74. }
  75. static int result_is_error(result_t result) {
  76. return result_get_error(result) != result_error_ok;
  77. }
  78. static int result_is_skip(result_t result) {
  79. return result_get_error(result) == result_error_skip;
  80. }
  81. static result_error_t result_get_error(result_t result) {
  82. return result.internal_error;
  83. }
  84. char const* result_get_error_string(result_t result);
  85. static result_data_t result_get_data(result_t result) {
  86. return result.internal_data;
  87. }
  88. #endif