fbxcel_dom/v7400/object/property/loaders/
binstr.rs1use crate::v7400::object::property::{loaders::check_attrs_len, LoadProperty, PropertyHandle};
4
5#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
13pub struct OwnedBinaryLoader;
14
15#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
21pub struct OwnedStringLoader;
22
23macro_rules! impl_owned_loader {
25 ($ty_loader:ty, $ty_target:ty, $getter:ident, $target_name_str:expr) => {
26 impl $ty_loader {
27 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#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
63pub struct BorrowedBinaryLoader;
64
65#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
71pub struct BorrowedStringLoader;
72
73macro_rules! impl_borrowed_loader {
75 ($ty_loader:ty, $ty_target:ty, $getter:ident, $target_name_str:expr) => {
76 impl $ty_loader {
77 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" }