Documentation
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
use num_traits::{cast::AsPrimitive, NumCast, PrimInt, WrappingNeg};
use serde::{
    de::{self, DeserializeSeed, SeqAccess, Visitor},
    serde_if_integer128,
};

use super::map_deserializer::MapDeserializer;
use crate::{Error, Result};

#[derive(Clone)]
pub struct Deserializer<'de> {
    pub(super) bytes: &'de [u8],
    pub(super) index: usize,
}

impl<'de> Deserializer<'de> {
    /// Create a new derializer.
    pub const fn from_bytes(bytes: &'de [u8]) -> Self {
        Self { bytes, index: 0 }
    }

    /// Returns the next byte and advances the internal buffer by one.
    /// Returns None if empty.
    pub(super) fn next_byte(&mut self) -> Result<u8> {
        if let Some(byte) = self.bytes.get(self.index) {
            self.advance();
            Ok(*byte)
        } else {
            Err(Error::Eof)
        }
    }

    /// Look at the next byte without advancing the buffer.
    /// Returns None if empty.
    pub(super) fn peek_byte(&mut self) -> Result<u8> {
        if let Some(byte) = self.bytes.get(self.index) {
            Ok(*byte)
        } else {
            Err(Error::Eof)
        }
    }

    /// Advances the internal buffer by one
    pub(super) fn advance(&mut self) {
        self.index += 1;
    }

    /// Ensures there aren't any trailing bytes
    /// # Errors
    /// TODO
    pub fn check_trailing_bytes(&mut self) -> Result<()> {
        if self.bytes.len() > self.index {
            Err(Error::TrailingBytes)
        } else {
            Ok(())
        }
    }

    /// Parses ascii numbers into as type N until a certain byte is found and discards it
    pub(super) fn next_ascii_number_until<N>(&mut self, negative: bool, until: u8) -> Result<N>
    where
        N: Copy + PrimInt + NumCast + WrappingNeg + 'static,
        u8: AsPrimitive<N>,
        i8: AsPrimitive<N>,
    {
        let mut significand = N::zero();

        loop {
            match self.next_byte()? {
                integer @ b'0'..=b'9' => {
                    // To convert an ascii number to an actuall number we can just subtract the ascii rappresentation of 0
                    let digit = (integer - b'0').as_();

                    let max = N::max_value();

                    // Checks if the number would overflow
                    if significand >= max / 10u8.as_()
                        && (significand > max / 10u8.as_() || digit > max % 10u8.as_())
                    {
                        return Err(Error::OutOfBound);
                    } else {
                        significand = significand * 10u8.as_() + digit;
                    }
                }
                token => {
                    break if token != until {
                        Err(Error::unexpected_token("", token, self.index))
                    } else if negative {
                        Ok(significand.wrapping_neg())
                    } else {
                        Ok(significand)
                    }
                }
            }
        }
    }

    /// Parses any integer ignoring the leading "i" bytes
    pub(super) fn parse_integer<N>(&mut self, negative: bool) -> Result<N>
    where
        N: Copy + PrimInt + NumCast + WrappingNeg + 'static,
        u8: AsPrimitive<N>,
        i8: AsPrimitive<N>,
    {
        // Check the first byte, if none then we hit eof too soon
        match self.peek_byte()? {
            b'0' => {
                if negative {
                    Err(Error::NegativeZero) // Negative zero is an invalid bencode number
                } else {
                    self.advance();
                    match self.next_byte()? {
                        b'e' => Ok(N::zero()),
                        b'0'..=b'9' => Err(Error::LeadingZero), // The only valid case for a leading zero is simply 0, any other number is invalid
                        token => Err(Error::unexpected_token("e", token, self.index)), // The only possible valid token at the end is "e"
                    }
                }
            }
            b'1'..=b'9' => self.next_ascii_number_until(negative, b'e'),
            token => Err(Error::unexpected_token(
                "number between 0-9",
                token,
                self.index,
            )),
        }
    }

    /// Parses a byte string
    pub(super) fn parse_byte_string(&mut self) -> Result<&'de [u8]> {
        let len = self.next_ascii_number_until::<usize>(false, b':')?;

        if len == 0 {
            return Ok(&[]);
        }

        if let Some(computed_index) = self.index.checked_add(len) {
            if self.bytes.len() >= (computed_index) {
                let bytes = &self.bytes[self.index..computed_index];
                self.index = computed_index;
                Ok(bytes)
            } else {
                Err(Error::EofWhileParsingByteString)
            }
        } else {
            Err(Error::OutOfBound)
        }
    }
}

impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
    type Error = Error;

    fn deserialize_any<V>(self, visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        match self.peek_byte()? {
            b'i' => self.deserialize_i64(visitor),
            b'0'..=b'9' => self.deserialize_bytes(visitor),
            b'l' => self.deserialize_seq(visitor),
            b'd' => self.deserialize_map(visitor),
            token => Err(Error::unexpected_token(
                "one of: i, 0-9, l, d",
                token,
                self.index,
            )),
        }
    }

    fn deserialize_bool<V>(self, _visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        Err(Error::Unsupported("bool"))
    }

    fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value>
    where
        V: de::Visitor<'de>,
    {
        if let b'i' = self.next_byte()? {
            match self.peek_byte()? {
                b'-' => {
                    self.advance();
                    visitor.visit_i8(self.parse_integer(true)?)
                }
                _ => visitor.visit_i8(self.parse_integer(false)?),
            }
        } else {
            Err(Error::InvalidType)
        }
    }

    fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value>
    where
        V: de::Visitor<'de>,
    {
        if let b'i' = self.next_byte()? {
            match self.peek_byte()? {
                b'-' => {
                    self.advance();
                    visitor.visit_i16(self.parse_integer(true)?)
                }
                _ => visitor.visit_i16(self.parse_integer(false)?),
            }
        } else {
            Err(Error::InvalidType)
        }
    }

    fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value>
    where
        V: de::Visitor<'de>,
    {
        if let b'i' = self.next_byte()? {
            match self.peek_byte()? {
                b'-' => {
                    self.advance();
                    visitor.visit_i32(self.parse_integer(true)?)
                }
                _ => visitor.visit_i32(self.parse_integer(false)?),
            }
        } else {
            Err(Error::InvalidType)
        }
    }

    fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value>
    where
        V: de::Visitor<'de>,
    {
        if let b'i' = self.next_byte()? {
            match self.peek_byte()? {
                b'-' => {
                    self.advance();
                    visitor.visit_i64(self.parse_integer(true)?)
                }
                _ => visitor.visit_i64(self.parse_integer(false)?),
            }
        } else {
            Err(Error::InvalidType)
        }
    }

    fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        if let b'i' = self.next_byte()? {
            visitor.visit_u8(self.parse_integer(false)?)
        } else {
            Err(Error::InvalidType)
        }
    }

    fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        if let b'i' = self.next_byte()? {
            visitor.visit_u16(self.parse_integer(false)?)
        } else {
            Err(Error::InvalidType)
        }
    }

    fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        if let b'i' = self.next_byte()? {
            visitor.visit_u32(self.parse_integer(false)?)
        } else {
            Err(Error::InvalidType)
        }
    }

    fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        if let b'i' = self.next_byte()? {
            visitor.visit_u64(self.parse_integer(false)?)
        } else {
            Err(Error::InvalidType)
        }
    }

    serde_if_integer128! {

        fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value>
        where
            V: Visitor<'de>,
        {
            if let b'i' = self.next_byte()? {
                visitor.visit_u128(self.parse_integer(false)?)
            } else {
                Err(Error::InvalidType)
            }
        }

        fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value>
        where
            V: de::Visitor<'de>,
        {
            if let b'i' = self.next_byte()? {
                match self.peek_byte()? {
                    b'-' => {
                        self.advance();
                        visitor.visit_i128(self.parse_integer(true)?)
                    }
                    _ => visitor.visit_i128(self.parse_integer(false)?),
                }
            } else {
                Err(Error::InvalidType)
            }
        }
    }

    fn deserialize_f32<V>(self, _visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        Err(Error::Unsupported("f32"))
    }

    fn deserialize_f64<V>(self, _visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        Err(Error::Unsupported("f64"))
    }

    fn deserialize_char<V>(self, _visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        Err(Error::Unsupported("char"))
    }

    fn deserialize_str<V>(self, visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        visitor.visit_borrowed_str(core::str::from_utf8(self.parse_byte_string()?)?)
    }

    fn deserialize_string<V>(self, visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        self.deserialize_str(visitor)
    }

    fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        visitor.visit_bytes(self.parse_byte_string()?)
    }

    fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        self.deserialize_bytes(visitor)
    }

    fn deserialize_option<V>(self, visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        visitor.visit_some(self) // TODO: Bencode doesn't really have a concept of missing value.
    }

    fn deserialize_unit<V>(self, _visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        Err(Error::Unsupported("()"))
    }

    fn deserialize_unit_struct<V>(self, _name: &'static str, _visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        Err(Error::Unsupported("()")) // TODO: Use the name to provide better errors
    }

    fn deserialize_newtype_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        visitor.visit_newtype_struct(self)
    }

    fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        match self.next_byte()? {
            b'l' => visitor.visit_seq(self),
            token => Err(Error::unexpected_token("l", token, self.index)),
        }
    }

    fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        self.deserialize_seq(visitor)
    }

    fn deserialize_tuple_struct<V>(
        self,
        _name: &'static str,
        _len: usize,
        visitor: V,
    ) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        self.deserialize_seq(visitor)
    }

    fn deserialize_map<V>(self, visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        match self.next_byte()? {
            // b'd' => visitor.visit_map(self),
            b'd' => visitor.visit_map(MapDeserializer::new(self)),
            b'l' => Err(Error::ExpectedDictionaryFoundList),
            b'i' => Err(Error::ExpectedDictionaryFoundInteger),
            b'0'..=b'9' => Err(Error::ExpectedDictionaryFoundByteString),
            token => Err(Error::unexpected_token("d", token, self.index)),
        }
    }

    fn deserialize_struct<V>(
        self,
        _name: &'static str,
        _fields: &'static [&'static str],
        visitor: V,
    ) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        self.deserialize_map(visitor)
    }

    fn deserialize_enum<V>(
        self,
        _name: &'static str,
        _variants: &'static [&'static str],
        _visitor: V,
    ) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        todo!() // TODO: Figure out what to do here.
    }

    fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        self.deserialize_bytes(visitor)
    }

    fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        self.deserialize_any(visitor)
    }
}

impl<'de> SeqAccess<'de> for Deserializer<'de> {
    type Error = Error;

    fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>>
    where
        T: DeserializeSeed<'de>,
    {
        if let b'e' = self.peek_byte()? {
            self.advance();
            Ok(None)
        } else {
            seed.deserialize(&mut *self).map(Some)
        }
    }
}

#[cfg(test)]
mod tests {

    use crate::from_bytes;

    #[test]
    fn zero_lenght_byte_string() {
        assert_eq!("", from_bytes::<&'static str>(b"0:").unwrap())
    }
}