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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--

Binn-IR

Copyright (C) 2018-2023  Anonymous

There are several releases over multiple years,
they are listed as ranges, such as: "2018-2023".

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.

::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--
*/

//! # Decoder

use {
    alloc::string::String,
    std::io::Read,
    crate::{Blob, IoResult, List, Map, Object, Value},
};

/// # Decoder
///
/// ## Usage
///
/// ### Decoding any values
///
/// You can use [`decode()`][#decode()] and a `match` to filter values. This function will hand you the values after _finishing_ decoding
/// process.
///
/// ### Decoding desired values
///
/// You can use `decode_*()`. However, please note that: if an un-expected value is detected, the whole decoding operation might be _broken_.
/// It's because those functions just decode the header of a value, and stop if not matched. So at that point, data stream is already broken.
///
/// In contrast, with [`decode()`][#decode()], when you expect an [`Object`][Value::Object] but get a [`List`][Value::List], you can still
/// continue decoding next values.
///
/// [#decode()]: #method.decode
/// [Value::Object]: enum.Value.html#variant.Object
/// [Value::List]: enum.Value.html#variant.List
pub trait Decoder: Read + Sized {

    /// # Decodes a value
    fn decode(&mut self) -> IoResult<Option<Value>> {
        crate::decode(self)
    }

    /// # Decodes a null
    fn decode_null(&mut self) -> IoResult<Option<()>> {
        crate::decode_null(self)
    }

    /// # Decodes a boolean value
    fn decode_bool(&mut self) -> IoResult<Option<bool>> {
        crate::decode_bool(self)
    }

    /// # Decodes a `u8` value
    fn decode_u8(&mut self) -> IoResult<Option<u8>> {
        crate::decode_u8(self)
    }

    /// # Decodes an `i8` value
    fn decode_i8(&mut self) -> IoResult<Option<i8>> {
        crate::decode_i8(self)
    }

    /// # Decodes a `u16` value
    fn decode_u16(&mut self) -> IoResult<Option<u16>> {
        crate::decode_u16(self)
    }

    /// # Decodes an `i16` value
    fn decode_i16(&mut self) -> IoResult<Option<i16>> {
        crate::decode_i16(self)
    }

    /// # Decodes a `u32` value
    fn decode_u32(&mut self) -> IoResult<Option<u32>> {
        crate::decode_u32(self)
    }
    /// # Decodes an `i32` value
    fn decode_i32(&mut self) -> IoResult<Option<i32>> {
        crate::decode_i32(self)
    }

    /// # Decodes a `u64` value
    fn decode_u64(&mut self) -> IoResult<Option<u64>> {
        crate::decode_u64(self)
    }

    /// # Decodes an `i64` value
    fn decode_i64(&mut self) -> IoResult<Option<i64>> {
        crate::decode_i64(self)
    }

    /// # Decodes a [`Float`][Value::Float]
    ///
    /// [Value::Float]: enum.Value.html#variant.Float
    fn decode_float(&mut self) -> IoResult<Option<f32>> {
        crate::decode_float(self)
    }

    /// # Decodes a [`Double`][Value::Double]
    ///
    /// [Value::Double]: enum.Value.html#variant.Double
    fn decode_double(&mut self) -> IoResult<Option<f64>> {
        crate::decode_double(self)
    }

    /// # Decodes a text
    fn decode_text(&mut self) -> IoResult<Option<String>> {
        crate::decode_text(self)
    }

    /// # Decodes a [`DateTime`][Value::DateTime]
    ///
    /// [Value::DateTime]: enum.Value.html#variant.DateTime
    fn decode_date_time(&mut self) -> IoResult<Option<String>> {
        crate::decode_date_time(self)
    }

    /// # Decodes a [`Date`][Value::Date]
    ///
    /// [Value::Date]: enum.Value.html#variant.Date
    fn decode_date(&mut self) -> IoResult<Option<String>> {
        crate::decode_date(self)
    }

    /// # Decodes a [`Time`][Value::Time]
    ///
    /// [Value::Time]: enum.Value.html#variant.Time
    fn decode_time(&mut self) -> IoResult<Option<String>> {
        crate::decode_time(self)
    }

    /// # Decodes a [`DecimalStr`][Value::DecimalStr]
    ///
    /// [Value::DecimalStr]: enum.Value.html#variant.DecimalStr
    fn decode_decimal_str(&mut self) -> IoResult<Option<String>> {
        crate::decode_decimal_str(self)
    }

    /// # Decodes a [`Blob`][Value::Blob]
    ///
    /// [Value::Blob]: enum.Value.html#variant.Blob
    fn decode_blob(&mut self) -> IoResult<Option<Blob>> {
        crate::decode_blob(self)
    }

    /// # Decodes a [`List`][Value::List]
    ///
    /// [Value::List]: enum.Value.html#variant.List
    fn decode_list(&mut self) -> IoResult<Option<List>> {
        crate::decode_list(self)
    }

    /// # Decodes a [`Map`][Value::Map]
    ///
    /// [Value::Map]: enum.Value.html#variant.Map
    fn decode_map(&mut self) -> IoResult<Option<Map>> {
        crate::decode_map(self)
    }

    /// # Decodes an [`Object`][Value::Object]
    ///
    /// [Value::Object]: enum.Value.html#variant.Object
    fn decode_object(&mut self) -> IoResult<Option<Object>> {
        crate::decode_object(self)
    }

}

impl<T> Decoder for T where T: Read {}