1use super::{
2 error::{get_error_message, DecodeError},
3 DataView, Format, Read, ExtensionType,
4};
5use crate::{BigInt, BigNumber, JSON, Context};
6use byteorder::{BigEndian, ReadBytesExt};
7use core::hash::Hash;
8use std::{collections::BTreeMap, io::Read as StdioRead, str::FromStr};
9
10#[derive(Debug)]
11pub struct ReadDecoder {
12 pub(crate) context: Context,
13 pub(crate) view: DataView,
14}
15
16impl ReadDecoder {
17 pub fn new(buf: &[u8], context: Context) -> Self {
18 Self {
19 context: context.clone(),
20 view: DataView::new(buf, context).expect("Failed to create new data view"),
21 }
22 }
23
24 pub fn get_bytes(&mut self, n_bytes_to_read: u64) -> Result<Vec<u8>, DecodeError> {
25 let mut buf = vec![];
26 let mut chunk = self.take(n_bytes_to_read);
27 match chunk.read_to_end(&mut buf) {
28 Ok(_n) => Ok(buf),
29 Err(e) => Err(DecodeError::BytesReadError(e.to_string())),
30 }
31 }
32
33 pub fn read_i64(&mut self) -> Result<i64, DecodeError> {
34 let f = Format::get_format(self)?;
35 let prefix = f.to_u8();
36 if Format::is_positive_fixed_int(prefix) {
37 Ok(prefix as i64)
38 } else if Format::is_negative_fixed_int(prefix) {
39 Ok((prefix as i8) as i64)
40 } else {
41 match f {
42 Format::Int8 => Ok(ReadBytesExt::read_i8(self)? as i64),
43 Format::Int16 => Ok(ReadBytesExt::read_i16::<BigEndian>(self)? as i64),
44 Format::Int32 => Ok(ReadBytesExt::read_i32::<BigEndian>(self)? as i64),
45 Format::Int64 => Ok(ReadBytesExt::read_i64::<BigEndian>(self)?),
46 Format::Uint8 => Ok(ReadBytesExt::read_u8(self)? as i64),
47 Format::Uint16 => Ok(ReadBytesExt::read_u16::<BigEndian>(self)? as i64),
48 Format::Uint32 => Ok(ReadBytesExt::read_u32::<BigEndian>(self)? as i64),
49 Format::Uint64 => {
50 let v = ReadBytesExt::read_u64::<BigEndian>(self)?;
51
52 if v <= i64::MAX as u64 {
53 Ok(v as i64)
54 } else {
55 let formatted_err = format!("integer overflow: value = {}; bits = 64", v);
56 let err_msg = self.context().print_with_context(&formatted_err);
57 Err(DecodeError::IntRangeError(err_msg))
58 }
59 },
60 err_f => {
61 let formatted_err = format!(
62 "Property must be of type 'int'. {}",
63 get_error_message(err_f)
64 );
65 let err_msg = self.context().print_with_context(&formatted_err);
66 Err(DecodeError::WrongMsgPackFormat(err_msg))
67 }
68 }
69 }
70 }
71
72 pub fn read_u64(&mut self) -> Result<u64, DecodeError> {
73 let f = Format::get_format(self)?;
74 let prefix = f.to_u8();
75 if Format::is_positive_fixed_int(prefix) {
76 return Ok(prefix as u64);
77 } else if Format::is_negative_fixed_int(prefix) {
78 let formatted_err = format!(
79 "unsigned integer cannot be negative. {}",
80 get_error_message(f)
81 );
82 let err_msg = self.context().print_with_context(&formatted_err);
83
84 return Err(DecodeError::IntRangeError(err_msg))
85 }
86
87 match f {
88 Format::Uint8 => Ok(ReadBytesExt::read_u8(self)? as u64),
89 Format::Uint16 => Ok(ReadBytesExt::read_u16::<BigEndian>(self)? as u64),
90 Format::Uint32 => Ok(ReadBytesExt::read_u32::<BigEndian>(self)? as u64),
91 Format::Uint64 => Ok(ReadBytesExt::read_u64::<BigEndian>(self)?),
92 Format::Int8 => {
93 let int8 = ReadBytesExt::read_i8(self)?;
94
95 if int8 >= 0 {
96 return Ok(int8 as u64)
97 }
98
99 let formatted_err = format!(
100 "unsigned integer cannot be negative. {}",
101 get_error_message(f)
102 );
103 let err_msg = self.context().print_with_context(&formatted_err);
104
105 Err(DecodeError::IntRangeError(err_msg))
106 },
107 Format::Int16 => {
108 let int16 = ReadBytesExt::read_i16::<BigEndian>(self)?;
109
110 if int16 >= 0 {
111 return Ok(int16 as u64)
112 }
113
114 let formatted_err = format!(
115 "unsigned integer cannot be negative. {}",
116 get_error_message(f)
117 );
118 let err_msg = self.context().print_with_context(&formatted_err);
119
120 Err(DecodeError::IntRangeError(err_msg))
121 },
122 Format::Int32 => {
123 let int32 = ReadBytesExt::read_i32::<BigEndian>(self)?;
124
125 if int32 >= 0 {
126 return Ok(int32 as u64)
127 }
128
129 let formatted_err = format!(
130 "unsigned integer cannot be negative. {}",
131 get_error_message(f)
132 );
133 let err_msg = self.context().print_with_context(&formatted_err);
134
135 Err(DecodeError::IntRangeError(err_msg))
136 },
137 Format::Int64 => {
138 let int64 = ReadBytesExt::read_i64::<BigEndian>(self)?;
139
140 if int64 >= 0 {
141 return Ok(int64 as u64)
142 }
143
144 let formatted_err = format!(
145 "unsigned integer cannot be negative. {}",
146 get_error_message(f)
147 );
148 let err_msg = self.context().print_with_context(&formatted_err);
149
150 Err(DecodeError::IntRangeError(err_msg))
151 },
152
153 err_f => {
154 let formatted_err = format!(
155 "Property must be of type 'uint'. {}",
156 get_error_message(err_f)
157 );
158 let err_msg = self.context().print_with_context(&formatted_err);
159 Err(DecodeError::WrongMsgPackFormat(err_msg))
160 }
161 }
162
163 }
164}
165
166impl StdioRead for ReadDecoder {
167 fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
168 self.view.buffer.read(&mut *buf)
169 }
170}
171
172impl Read for ReadDecoder {
173 fn read_bool(&mut self) -> Result<bool, DecodeError> {
174 match Format::get_format(self)? {
175 Format::True => Ok(true),
176 Format::False => Ok(false),
177 err_f => {
178 let formatted_err = format!(
179 "Property must be of type 'bool'. {}",
180 get_error_message(err_f)
181 );
182 let err_msg = self.context().print_with_context(&formatted_err);
183 Err(DecodeError::WrongMsgPackFormat(err_msg))
184 }
185 }
186 }
187
188 fn read_i8(&mut self) -> Result<i8, DecodeError> {
189 let v = self.read_i64()?;
190 if v <= i8::MAX as i64 && v >= i8::MIN as i64 {
192 Ok(v as i8)
193 } else {
194 let formatted_err = format!("integer overflow: value = {}; bits = 8", v);
195 let err_msg = self.context().print_with_context(&formatted_err);
196 Err(DecodeError::IntRangeError(err_msg))
197 }
198 }
199
200 fn read_i16(&mut self) -> Result<i16, DecodeError> {
201 let v = self.read_i64()?;
202 if v <= i16::MAX as i64 && v >= i16::MIN as i64 {
204 Ok(v as i16)
205 } else {
206 let formatted_err = format!("integer overflow: value = {}; bits = 16", v);
207 let err_msg = self.context().print_with_context(&formatted_err);
208 Err(DecodeError::IntRangeError(err_msg))
209 }
210 }
211
212 fn read_i32(&mut self) -> Result<i32, DecodeError> {
213 let v = self.read_i64()?;
214 if v <= i32::MAX as i64 && v >= i32::MIN as i64 {
216 Ok(v as i32)
217 } else {
218 let formatted_err = format!("integer overflow: value = {}; bits = 32", v);
219 let err_msg = self.context().print_with_context(&formatted_err);
220 Err(DecodeError::IntRangeError(err_msg))
221 }
222 }
223
224 fn read_u8(&mut self) -> Result<u8, DecodeError> {
225 let v = self.read_u64()?;
226 if v <= u8::MAX as u64 && v >= u8::MIN as u64 {
228 Ok(v as u8)
229 } else {
230 let formatted_err = format!("unsigned integer overflow: value = {}; bits = 8", v);
231 let err_msg = self.context().print_with_context(&formatted_err);
232 Err(DecodeError::IntRangeError(err_msg))
233 }
234 }
235
236 fn read_u16(&mut self) -> Result<u16, DecodeError> {
237 let v = self.read_u64()?;
238 if v <= u16::MAX as u64 && v >= u16::MIN as u64 {
240 Ok(v as u16)
241 } else {
242 let formatted_err = format!("unsigned integer overflow: value = {}; bits = 16", v);
243 let err_msg = self.context().print_with_context(&formatted_err);
244 Err(DecodeError::IntRangeError(err_msg))
245 }
246 }
247
248 fn read_u32(&mut self) -> Result<u32, DecodeError> {
249 let v = self.read_u64()?;
250 if v <= u32::MAX as u64 && v >= u32::MIN as u64 {
252 Ok(v as u32)
253 } else {
254 let formatted_err = format!("unsigned integer overflow: value = {}; bits = 32", v);
255 let err_msg = self.context().print_with_context(&formatted_err);
256 Err(DecodeError::IntRangeError(err_msg))
257 }
258 }
259
260 fn read_f32(&mut self) -> Result<f32, DecodeError> {
261 match Format::get_format(self)? {
262 Format::Float32 => Ok(ReadBytesExt::read_f32::<BigEndian>(self)?),
263 err_f => {
264 let formatted_err = format!(
265 "Property must be of type 'float32'. {}",
266 get_error_message(err_f)
267 );
268 let err_msg = self.context().print_with_context(&formatted_err);
269 Err(DecodeError::WrongMsgPackFormat(err_msg))
270 }
271 }
272 }
273
274 fn read_f64(&mut self) -> Result<f64, DecodeError> {
275 match Format::get_format(self)? {
276 Format::Float64 => Ok(ReadBytesExt::read_f64::<BigEndian>(self)?),
277 Format::Float32 => Ok(ReadBytesExt::read_f32::<BigEndian>(self)? as f64),
278 err_f => {
279 let formatted_err = format!(
280 "Property must be of type 'float64'. {}",
281 get_error_message(err_f)
282 );
283 let err_msg = self.context().print_with_context(&formatted_err);
284 Err(DecodeError::WrongMsgPackFormat(err_msg))
285 }
286 }
287 }
288
289 fn read_string_length(&mut self) -> Result<u32, DecodeError> {
290 if self.is_next_nil()? {
291 return Ok(0)
292 }
293
294 match Format::get_format(self)? {
295 Format::FixStr(len) => Ok(len as u32),
296 Format::FixArray(len) => Ok(len as u32),
297 Format::Str8 => Ok(ReadBytesExt::read_u8(self)? as u32),
298 Format::Str16 => Ok(ReadBytesExt::read_u16::<BigEndian>(self)? as u32),
299 Format::Str32 => Ok(ReadBytesExt::read_u32::<BigEndian>(self)?),
300 Format::Nil => Ok(0),
301 err_f => {
302 let formatted_err = format!(
303 "Property must be of type 'string'. {}",
304 get_error_message(err_f)
305 );
306 let err_msg = self.context().print_with_context(&formatted_err);
307 Err(DecodeError::WrongMsgPackFormat(err_msg))
308 }
309 }
310 }
311
312 fn read_string(&mut self) -> Result<String, DecodeError> {
313 let str_len = self.read_string_length()?;
314 let bytes = self.get_bytes(str_len as u64)?;
315 match String::from_utf8(bytes) {
316 Ok(s) => Ok(s),
317 Err(e) => Err(DecodeError::StrReadError(e.to_string())),
318 }
319 }
320
321 fn read_bytes_length(&mut self) -> Result<u32, DecodeError> {
322 if self.is_next_nil()? {
323 return Ok(0)
324 }
325
326 match Format::get_format(self)? {
327 Format::FixArray(len) => Ok(len as u32),
328 Format::Bin8 => Ok(ReadBytesExt::read_u8(self)? as u32),
329 Format::Bin16 => Ok(ReadBytesExt::read_u16::<BigEndian>(self)? as u32),
330 Format::Bin32 => Ok(ReadBytesExt::read_u32::<BigEndian>(self)?),
331 Format::Nil => Ok(0),
332 err_f => {
333 let formatted_err = format!(
334 "Property must be of type 'bytes'. {}",
335 get_error_message(err_f)
336 );
337 let err_msg = self.context().print_with_context(&formatted_err);
338 Err(DecodeError::WrongMsgPackFormat(err_msg))
339 }
340 }
341 }
342
343 fn read_bytes(&mut self) -> Result<Vec<u8>, DecodeError> {
344 let bytes_len = self.read_bytes_length()?;
345 self.get_bytes(bytes_len as u64)
346 .map_err(|e| DecodeError::BytesReadError(e.to_string()))
347 }
348
349 fn read_bigint(&mut self) -> Result<BigInt, DecodeError> {
350 let bigint_str = self.read_string()?;
351 BigInt::from_str(&bigint_str).map_err(|e| DecodeError::ParseBigIntError(e.to_string()))
352 }
353
354 fn read_bignumber(&mut self) -> Result<BigNumber, DecodeError> {
355 let bignumber_str = self.read_string()?;
356 BigNumber::from_str(&bignumber_str).map_err(|e| DecodeError::ParseBigNumberError(e.to_string()))
357 }
358
359 fn read_json(&mut self) -> Result<JSON::Value, DecodeError> {
360 let json_str = self.read_string()?;
361 JSON::from_str(&json_str).map_err(|e| DecodeError::JSONReadError(e.to_string()))
362 }
363
364 fn read_array_length(&mut self) -> Result<u32, DecodeError> {
365 if self.is_next_nil()? {
366 return Ok(0)
367 }
368
369 match Format::get_format(self)? {
370 Format::FixArray(len) => Ok(len as u32),
371 Format::Array16 => Ok(ReadBytesExt::read_u16::<BigEndian>(self)? as u32),
372 Format::Array32 => Ok(ReadBytesExt::read_u32::<BigEndian>(self)?),
373 Format::Nil => Ok(0),
374 err_f => {
375 let formatted_err = format!(
376 "Property must be of type 'array'. {}",
377 get_error_message(err_f)
378 );
379 let err_msg = self.context().print_with_context(&formatted_err);
380 Err(DecodeError::WrongMsgPackFormat(err_msg))
381 }
382 }
383 }
384
385 fn read_array<T>(
386 &mut self,
387 mut item_reader: impl FnMut(&mut Self) -> Result<T, DecodeError>,
388 ) -> Result<Vec<T>, DecodeError> {
389 let arr_len = self.read_array_length()?;
390 let mut array: Vec<T> = vec![];
391 for i in 0..arr_len {
392 self.context.push("array[", &i.to_string(), "]");
393 let item = item_reader(self)?;
394 array.push(item);
395 self.context.pop();
396 }
397 Ok(array)
398 }
399
400 fn read_map_length(&mut self) -> Result<u32, DecodeError> {
401 if self.is_next_nil()? {
402 return Ok(0)
403 }
404
405 match Format::get_format(self)? {
406 Format::FixMap(len) => Ok(len as u32),
407 Format::Map16 => Ok(ReadBytesExt::read_u16::<BigEndian>(self)? as u32),
408 Format::Map32 => Ok(ReadBytesExt::read_u32::<BigEndian>(self)?),
409 Format::Nil => Ok(0),
410 err_f => {
411 let formatted_err = format!(
412 "Property must be of type 'map'. {}",
413 get_error_message(err_f)
414 );
415 let err_msg = self.context().print_with_context(&formatted_err);
416 Err(DecodeError::WrongMsgPackFormat(err_msg))
417 }
418 }
419 }
420
421 fn read_map<K, V>(
422 &mut self,
423 mut key_reader: impl FnMut(&mut Self) -> Result<K, DecodeError>,
424 mut val_reader: impl FnMut(&mut Self) -> Result<V, DecodeError>,
425 ) -> Result<BTreeMap<K, V>, DecodeError>
426 where
427 K: Eq + Hash + Ord,
428 {
429 let map_len = self.read_map_length()?;
430 let mut map: BTreeMap<K, V> = BTreeMap::new();
431 for i in 0..map_len {
432 self.context.push("map[", &i.to_string(), "]");
433 let key = key_reader(self)?;
434 let value = val_reader(self)?;
435 map.insert(key, value);
436 self.context.pop();
437 }
438 Ok(map)
439 }
440
441 fn read_ext_generic_map<K, V>(
442 &mut self,
443 mut key_reader: impl FnMut(&mut Self) -> Result<K, DecodeError>,
444 mut val_reader: impl FnMut(&mut Self) -> Result<V, DecodeError>,
445 ) -> Result<BTreeMap<K, V>, DecodeError>
446 where
447 K: Eq + Hash + Ord,
448 {
449 let position = self.view.buffer.position();
450 let format = Format::get_format(self)?;
451 self.view.buffer.set_position(position);
452
453 let _byte_length = match format {
454 Format::FixMap(_) => {
455 return self.read_map(key_reader, val_reader);
456 },
457 Format::Map16 => {
458 return self.read_map(key_reader, val_reader);
459 },
460 Format::FixExt1 => 1,
461 Format::FixExt2 => 2,
462 Format::FixExt4 => 4,
463 Format::FixExt8 => 8,
464 Format::FixExt16 => 16,
465 Format::Ext8 => ReadBytesExt::read_u8(self)? as u32,
466 Format::Ext16 => {
467 ReadBytesExt::read_u16::<BigEndian>(self)? as u32
468 },
469 Format::Ext32 => {
470 ReadBytesExt::read_u32::<BigEndian>(self)?
471 },
472 err_f => {
473 let formatted_err = format!(
474 "Property must be of type 'ext generic map'. {}",
475 get_error_message(err_f)
476 );
477 let err_msg = self.context().print_with_context(&formatted_err);
478 return Err(DecodeError::WrongMsgPackFormat(err_msg))
479 }
480 };
481
482 ReadBytesExt::read_u8(self)?;
484
485 let ext_type = ReadBytesExt::read_u8(self)?;
487
488 if ext_type != ExtensionType::GenericMap.to_u8() {
489 let formatted_err = format!(
490 "Extension must be of type 'ext generic map'. Found {}",
491 ext_type
492 );
493 let err_msg = self.context().print_with_context(&formatted_err);
494 return Err(DecodeError::WrongMsgPackFormat(err_msg))
495 }
496
497 self.read_map(key_reader, val_reader)
498 }
499
500 fn read_nullable_bool(&mut self) -> Result<Option<bool>, DecodeError> {
501 if self.is_next_nil()? {
502 Ok(None)
503 } else {
504 match self.read_bool() {
505 Ok(v) => Ok(Some(v)),
506 Err(e) => Err(DecodeError::BooleanReadError(e.to_string())),
507 }
508 }
509 }
510
511 fn read_nullable_i8(&mut self) -> Result<Option<i8>, DecodeError> {
512 if self.is_next_nil()? {
513 Ok(None)
514 } else {
515 match Read::read_i8(self) {
516 Ok(v) => Ok(Some(v)),
517 Err(e) => Err(DecodeError::IntReadError(e.to_string())),
518 }
519 }
520 }
521
522 fn read_nullable_i16(&mut self) -> Result<Option<i16>, DecodeError> {
523 if self.is_next_nil()? {
524 Ok(None)
525 } else {
526 match Read::read_i16(self) {
527 Ok(v) => Ok(Some(v)),
528 Err(e) => Err(DecodeError::IntReadError(e.to_string())),
529 }
530 }
531 }
532
533 fn read_nullable_i32(&mut self) -> Result<Option<i32>, DecodeError> {
534 if self.is_next_nil()? {
535 Ok(None)
536 } else {
537 match Read::read_i32(self) {
538 Ok(v) => Ok(Some(v)),
539 Err(e) => Err(DecodeError::IntReadError(e.to_string())),
540 }
541 }
542 }
543
544 fn read_nullable_u8(&mut self) -> Result<Option<u8>, DecodeError> {
545 if self.is_next_nil()? {
546 Ok(None)
547 } else {
548 match Read::read_u8(self) {
549 Ok(v) => Ok(Some(v)),
550 Err(e) => Err(DecodeError::UintReadError(e.to_string())),
551 }
552 }
553 }
554
555 fn read_nullable_u16(&mut self) -> Result<Option<u16>, DecodeError> {
556 if self.is_next_nil()? {
557 Ok(None)
558 } else {
559 match Read::read_u16(self) {
560 Ok(v) => Ok(Some(v)),
561 Err(e) => Err(DecodeError::UintReadError(e.to_string())),
562 }
563 }
564 }
565
566 fn read_nullable_u32(&mut self) -> Result<Option<u32>, DecodeError> {
567 if self.is_next_nil()? {
568 Ok(None)
569 } else {
570 match Read::read_u32(self) {
571 Ok(v) => Ok(Some(v)),
572 Err(e) => Err(DecodeError::UintReadError(e.to_string())),
573 }
574 }
575 }
576
577 fn read_nullable_f32(&mut self) -> Result<Option<f32>, DecodeError> {
578 if self.is_next_nil()? {
579 Ok(None)
580 } else {
581 match Read::read_f32(self) {
582 Ok(v) => Ok(Some(v)),
583 Err(e) => Err(DecodeError::FloatReadError(e.to_string())),
584 }
585 }
586 }
587
588 fn read_nullable_f64(&mut self) -> Result<Option<f64>, DecodeError> {
589 if self.is_next_nil()? {
590 Ok(None)
591 } else {
592 match Read::read_f64(self) {
593 Ok(v) => Ok(Some(v)),
594 Err(e) => Err(DecodeError::FloatReadError(e.to_string())),
595 }
596 }
597 }
598
599 fn read_nullable_string(&mut self) -> Result<Option<String>, DecodeError> {
600 if self.is_next_nil()? {
601 Ok(None)
602 } else {
603 match self.read_string() {
604 Ok(s) => Ok(Some(s)),
605 Err(e) => Err(DecodeError::StrReadError(e.to_string())),
606 }
607 }
608 }
609
610 fn read_nullable_bytes(&mut self) -> Result<Option<Vec<u8>>, DecodeError> {
611 if self.is_next_nil()? {
612 Ok(None)
613 } else {
614 match self.read_bytes() {
615 Ok(bytes) => Ok(Some(bytes)),
616 Err(e) => Err(DecodeError::BytesReadError(e.to_string())),
617 }
618 }
619 }
620
621 fn read_nullable_bigint(&mut self) -> Result<Option<BigInt>, DecodeError> {
622 if self.is_next_nil()? {
623 Ok(None)
624 } else {
625 match self.read_bigint() {
626 Ok(bigint) => Ok(Some(bigint)),
627 Err(e) => Err(DecodeError::BigIntReadError(e.to_string())),
628 }
629 }
630 }
631
632 fn read_nullable_bignumber(&mut self) -> Result<Option<BigNumber>, DecodeError> {
633 if self.is_next_nil()? {
634 Ok(None)
635 } else {
636 match self.read_bignumber() {
637 Ok(bignumber) => Ok(Some(bignumber)),
638 Err(e) => Err(DecodeError::BigNumberReadError(e.to_string())),
639 }
640 }
641 }
642
643 fn read_nullable_json(&mut self) -> Result<Option<JSON::Value>, DecodeError> {
644 if self.is_next_nil()? {
645 Ok(None)
646 } else {
647 match self.read_json() {
648 Ok(value) => Ok(Some(value)),
649 Err(e) => Err(DecodeError::JSONReadError(e.to_string())),
650 }
651 }
652 }
653
654 fn read_nullable_array<T>(
655 &mut self,
656 item_reader: impl FnMut(&mut Self) -> Result<T, DecodeError>,
657 ) -> Result<Option<Vec<T>>, DecodeError> {
658 if self.is_next_nil()? {
659 Ok(None)
660 } else {
661 match self.read_array(item_reader) {
662 Ok(array) => Ok(Some(array)),
663 Err(e) => Err(DecodeError::ArrayReadError(e.to_string())),
664 }
665 }
666 }
667
668 fn read_nullable_map<K, V>(
669 &mut self,
670 key_reader: impl FnMut(&mut Self) -> Result<K, DecodeError>,
671 val_reader: impl FnMut(&mut Self) -> Result<V, DecodeError>,
672 ) -> Result<Option<BTreeMap<K, V>>, DecodeError>
673 where
674 K: Eq + Hash + Ord,
675 {
676 if self.is_next_nil()? {
677 Ok(None)
678 } else {
679 match self.read_map(key_reader, val_reader) {
680 Ok(map) => Ok(Some(map)),
681 Err(e) => Err(DecodeError::MapReadError(e.to_string())),
682 }
683 }
684 }
685
686 fn read_nullable_ext_generic_map<K, V>(
687 &mut self,
688 key_reader: impl FnMut(&mut Self) -> Result<K, DecodeError>,
689 val_reader: impl FnMut(&mut Self) -> Result<V, DecodeError>,
690 ) -> Result<Option<BTreeMap<K, V>>, DecodeError>
691 where
692 K: Eq + Hash + Ord,
693 {
694 if self.is_next_nil()? {
695 Ok(None)
696 } else {
697 match self.read_ext_generic_map(key_reader, val_reader) {
698 Ok(map) => Ok(Some(map)),
699 Err(e) => Err(DecodeError::ExtGenericMapReadError(e.to_string())),
700 }
701 }
702 }
703
704 fn is_next_nil(&mut self) -> Result<bool, DecodeError> {
705 let position = self.view.buffer.position();
706 let format = Format::get_format(self)?;
707 if format == Format::Nil {
708 Ok(true)
709 } else {
710 self.view.buffer.set_position(position);
711 Ok(false)
712 }
713 }
714
715 fn is_next_string(&mut self) -> Result<bool, DecodeError> {
716 let position = self.view.buffer.position();
717 let format = Format::get_format(self)?;
718 self.view.buffer.set_position(position);
719
720 match format {
721 Format::FixStr(_) | Format::Str8 | Format::Str16 | Format::Str32 => Ok(true),
722 _ => Ok(false)
723 }
724 }
725
726 fn context(&mut self) -> &mut Context {
727 &mut self.context
728 }
729}