This repository has been archived by the owner on Nov 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecoder.hpp
116 lines (92 loc) · 2.43 KB
/
decoder.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#ifndef LIB_RUBY_PARSER_DECODER_HPP
#define LIB_RUBY_PARSER_DECODER_HPP
#include <cstdbool>
#include <cstring>
#include "string.hpp"
#include "bytes.hpp"
namespace lib_ruby_parser
{
class InputError
{
public:
enum class Tag
{
UNSUPPORTED_ENCODING,
DECODING_ERROR
};
union Value
{
String unsupported_encoding;
String decoding_error;
Value();
Value(Value &&);
~Value();
Value &operator=(Value &&);
};
Tag tag;
Value as;
InputError(Tag tag, Value as);
InputError(InputError &&) = default;
~InputError();
InputError &operator=(InputError &&) = default;
static InputError UnsupportedEncoding(String unsupported_encoding);
static InputError DecodingError(String decoding_error);
};
struct DecoderResult
{
enum class Tag
{
OK,
ERR
};
union Value
{
ByteList ok;
InputError err;
Value();
Value(Value &&);
Value &operator=(Value &&);
~Value();
};
Tag tag;
Value as;
DecoderResult(Tag tag, Value as);
DecoderResult(DecoderResult &&) = default;
DecoderResult &operator=(DecoderResult &&) = default;
~DecoderResult();
static DecoderResult Ok(ByteList decoded);
static DecoderResult Err(InputError err);
};
extern "C"
{
struct DecoderResultBlob
{
uint8_t bytes[sizeof(DecoderResult)];
};
}
String string_from_string_blob(StringBlob blob);
ByteList byte_list_from_byte_list_blob(ByteListBlob blob);
DecoderResultBlob decoder_result_to_blob(DecoderResult decoder_result);
extern "C"
{
typedef DecoderResultBlob (*DecoderFunction)(void *, StringBlob, ByteListBlob);
}
class Decoder
{
public:
DecoderFunction f;
void *state;
explicit Decoder(DecoderFunction f, void *state);
};
class MaybeDecoder
{
public:
Decoder decoder;
bool is_some() const;
bool is_none() const;
explicit MaybeDecoder(Decoder decoder);
static MaybeDecoder Some(Decoder decoder);
static MaybeDecoder None();
};
} // namespace lib_ruby_parser
#endif // LIB_RUBY_PARSER_DECODER_HPP