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
//! Single type loader.

use std::io;

use crate::pull_parser::{v7400::LoadAttribute, Result};

/// Loader for primitive types.
///
/// Supported types are: [`bool`], [`i16`] , [`i32`], [`i64`], [`f32`], [`f64`].
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct PrimitiveLoader<T>(std::marker::PhantomData<T>);

/// Generates `LoadAttribute` implementations for `PrimitiveLoader<T>`.
macro_rules! impl_load_attribute_for_primitives {
    ($ty:ty, $method_name:ident, $expecting_type:expr) => {
        impl LoadAttribute for PrimitiveLoader<$ty> {
            type Output = $ty;

            fn expecting(&self) -> String {
                $expecting_type.into()
            }

            fn $method_name(self, v: $ty) -> Result<Self::Output> {
                Ok(v)
            }
        }
    };
}

impl_load_attribute_for_primitives!(bool, load_bool, "single boolean");
impl_load_attribute_for_primitives!(i16, load_i16, "single i16");
impl_load_attribute_for_primitives!(i32, load_i32, "single i32");
impl_load_attribute_for_primitives!(i64, load_i64, "single i64");
impl_load_attribute_for_primitives!(f32, load_f32, "single f32");
impl_load_attribute_for_primitives!(f64, load_f64, "single f64");

/// Loader for array types.
///
/// Supported types are: `Vec<{bool, i32, i64, f32, f64}>`.
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ArrayLoader<T>(std::marker::PhantomData<T>);

/// Generates `LoadAttribute` implementations for `PrimitiveLoader<T>`.
macro_rules! impl_load_attribute_for_arrays {
    ($ty:ty, $method_name:ident, $expecting_type:expr) => {
        impl LoadAttribute for ArrayLoader<Vec<$ty>> {
            type Output = Vec<$ty>;

            fn expecting(&self) -> String {
                $expecting_type.into()
            }

            fn $method_name(
                self,
                iter: impl Iterator<Item = Result<$ty>>,
                _: usize,
            ) -> Result<Self::Output> {
                iter.collect::<Result<_>>()
            }
        }
    };
}

impl_load_attribute_for_arrays!(bool, load_seq_bool, "boolean array");
impl_load_attribute_for_arrays!(i32, load_seq_i32, "i32 array");
impl_load_attribute_for_arrays!(i64, load_seq_i64, "i64 array");
impl_load_attribute_for_arrays!(f32, load_seq_f32, "f32 array");
impl_load_attribute_for_arrays!(f64, load_seq_f64, "f64 array");

/// Loader for a binary.
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct BinaryLoader;

impl LoadAttribute for BinaryLoader {
    type Output = Vec<u8>;

    fn expecting(&self) -> String {
        "binary".into()
    }

    fn load_binary(self, mut reader: impl io::Read, len: u64) -> Result<Self::Output> {
        let mut buf = Vec::with_capacity(len as usize);
        reader.read_to_end(&mut buf)?;
        Ok(buf)
    }
}

/// Loader for a string.
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct StringLoader;

impl LoadAttribute for StringLoader {
    type Output = String;

    fn expecting(&self) -> String {
        "string".into()
    }

    fn load_string(self, mut reader: impl io::Read, len: u64) -> Result<Self::Output> {
        let mut buf = String::with_capacity(len as usize);
        reader.read_to_string(&mut buf)?;
        Ok(buf)
    }
}