fbxcel_dom/v7400/object/property/loaders/
binstr.rs

1//! Property loaders for binary, and string.
2
3use crate::v7400::object::property::{loaders::check_attrs_len, LoadProperty, PropertyHandle};
4
5/// Binary property loader that loads owned data.
6///
7/// This does minimal checks about `data_type` and `label`.
8/// If you want to check property type precisely, you should make another
9/// loader type by purpose.
10///
11/// This loader rejects binary property even if the content is valid string.
12#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
13pub struct OwnedBinaryLoader;
14
15/// String property loader that loads owned data.
16///
17/// This does minimal checks about `data_type` and `label`.
18/// If you want to check property type precisely, you should make another
19/// loader type by purpose.
20#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
21pub struct OwnedStringLoader;
22
23/// Implements loaders for owned binary or string.
24macro_rules! impl_owned_loader {
25    ($ty_loader:ty, $ty_target:ty, $getter:ident, $target_name_str:expr) => {
26        impl $ty_loader {
27            /// Creates a new loader.
28            pub fn new() -> Self {
29                Self::default()
30            }
31        }
32
33        impl LoadProperty<'_> for $ty_loader {
34            type Value = $ty_target;
35            type Error = anyhow::Error;
36
37            fn expecting(&self) -> String {
38                $target_name_str.into()
39            }
40
41            fn load(self, node: &PropertyHandle<'_>) -> Result<Self::Value, Self::Error> {
42                let value_part = check_attrs_len(node, 1, $target_name_str)?;
43                value_part[0]
44                    .$getter()
45                    .map(Into::into)
46                    .map_err(|ty| prop_type_err!($target_name_str, ty, node))
47            }
48        }
49    };
50}
51
52impl_owned_loader! { OwnedBinaryLoader, Vec<u8>, get_binary_or_type, "binary" }
53impl_owned_loader! { OwnedStringLoader, String, get_string_or_type, "string" }
54
55/// Binary property loader that loads borrowed data.
56///
57/// This does minimal checks about `data_type` and `label`.
58/// If you want to check property type precisely, you should make another
59/// loader type by purpose.
60///
61/// This loader rejects binary property even if the content is valid string.
62#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
63pub struct BorrowedBinaryLoader;
64
65/// String property loader that loads borrowed data.
66///
67/// This does minimal checks about `data_type` and `label`.
68/// If you want to check property type precisely, you should make another
69/// loader type by purpose.
70#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
71pub struct BorrowedStringLoader;
72
73/// Implements loaders for borrowed binary or string.
74macro_rules! impl_borrowed_loader {
75    ($ty_loader:ty, $ty_target:ty, $getter:ident, $target_name_str:expr) => {
76        impl $ty_loader {
77            /// Creates a new loader.
78            pub fn new() -> Self {
79                Self::default()
80            }
81        }
82
83        impl<'a> LoadProperty<'a> for $ty_loader {
84            type Value = &'a $ty_target;
85            type Error = anyhow::Error;
86
87            fn expecting(&self) -> String {
88                $target_name_str.into()
89            }
90
91            fn load(self, node: &PropertyHandle<'a>) -> Result<Self::Value, Self::Error> {
92                let value_part = check_attrs_len(node, 1, $target_name_str)?;
93                value_part[0]
94                    .$getter()
95                    .map_err(|ty| prop_type_err!($target_name_str, ty, node))
96            }
97        }
98    };
99}
100
101impl_borrowed_loader! { BorrowedBinaryLoader, [u8], get_binary_or_type, "binary" }
102impl_borrowed_loader! { BorrowedStringLoader, str, get_string_or_type, "string" }