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
//! Property loaders for binary, and string.

use crate::v7400::object::property::{loaders::check_attrs_len, LoadProperty, PropertyHandle};

/// Binary property loader that loads owned data.
///
/// This does minimal checks about `data_type` and `label`.
/// If you want to check property type precisely, you should make another
/// loader type by purpose.
///
/// This loader rejects binary property even if the content is valid string.
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct OwnedBinaryLoader;

/// String property loader that loads owned data.
///
/// This does minimal checks about `data_type` and `label`.
/// If you want to check property type precisely, you should make another
/// loader type by purpose.
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct OwnedStringLoader;

macro_rules! impl_owned_loader {
    ($ty_loader:ty, $ty_target:ty, $getter:ident, $target_name_str:expr) => {
        impl $ty_loader {
            /// Creates a new loader.
            pub fn new() -> Self {
                Self::default()
            }
        }

        impl LoadProperty<'_> for $ty_loader {
            type Value = $ty_target;
            type Error = anyhow::Error;

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

            fn load(self, node: &PropertyHandle<'_>) -> Result<Self::Value, Self::Error> {
                let value_part = check_attrs_len(node, 1, $target_name_str)?;
                value_part[0]
                    .$getter()
                    .map(Into::into)
                    .map_err(|ty| prop_type_err!($target_name_str, ty, node))
            }
        }
    };
}

impl_owned_loader! { OwnedBinaryLoader, Vec<u8>, get_binary_or_type, "binary" }
impl_owned_loader! { OwnedStringLoader, String, get_string_or_type, "string" }

/// Binary property loader that loads borrowed data.
///
/// This does minimal checks about `data_type` and `label`.
/// If you want to check property type precisely, you should make another
/// loader type by purpose.
///
/// This loader rejects binary property even if the content is valid string.
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct BorrowedBinaryLoader;

/// String property loader that loads borrowed data.
///
/// This does minimal checks about `data_type` and `label`.
/// If you want to check property type precisely, you should make another
/// loader type by purpose.
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct BorrowedStringLoader;

macro_rules! impl_borrowed_loader {
    ($ty_loader:ty, $ty_target:ty, $getter:ident, $target_name_str:expr) => {
        impl $ty_loader {
            /// Creates a new loader.
            pub fn new() -> Self {
                Self::default()
            }
        }

        impl<'a> LoadProperty<'a> for $ty_loader {
            type Value = &'a $ty_target;
            type Error = anyhow::Error;

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

            fn load(self, node: &PropertyHandle<'a>) -> Result<Self::Value, Self::Error> {
                let value_part = check_attrs_len(node, 1, $target_name_str)?;
                value_part[0]
                    .$getter()
                    .map_err(|ty| prop_type_err!($target_name_str, ty, node))
            }
        }
    };
}

impl_borrowed_loader! { BorrowedBinaryLoader, [u8], get_binary_or_type, "binary" }
impl_borrowed_loader! { BorrowedStringLoader, str, get_string_or_type, "string" }