array_object/convert/
from_string.rs

1use crate::adaptor::*;
2use crate::error::ArrayObjectError;
3use crate::misc::Product;
4use crate::storage::*;
5
6macro_rules! from_text {
7    ($($ty:ty),*) => {
8        $(
9            impl From<$ty> for ArrayObject {
10                fn from(val: $ty) -> Self {
11                    (&val).into()
12                }
13            }
14            impl From<Vec<$ty>> for ArrayObject {
15                fn from(val: Vec<$ty>) -> Self {
16                    (&val).into()
17                }
18            }
19            impl<const N: usize> From<[$ty; N]> for ArrayObject {
20                fn from(val: [$ty; N]) -> Self {
21                    (&val).into()
22                }
23            }
24            // Reference
25            impl From<&$ty> for ArrayObject {
26                fn from(val: &$ty) -> Self {
27                    let data: Vec<u8> = val.to_string().into_bytes().to_vec();
28                    Self {
29                        data,
30                        shape: vec![],
31                        datatype: DataType::String,
32                    }
33                }
34            }
35            impl From<&Vec<$ty>> for ArrayObject {
36                fn from(val: &Vec<$ty>) -> Self {
37                    let shape = vec![val.len() as u64];
38                    let val: Vec<_> = val.into_iter().map(|x| x.to_string()).collect();
39                    let data = val.into_iter().map(|x| x.as_bytes().to_vec()).collect::<Vec<_>>().join(&255u8);
40                    Self {
41                        data,
42                        shape,
43                        datatype: DataType::String,
44                    }
45                }
46            }
47            impl<const N: usize> From<&[$ty; N]> for ArrayObject {
48                fn from(val: &[$ty; N]) -> Self {
49                    val.to_vec().into()
50                }
51            }
52            // Double reference
53            impl From<&&$ty> for ArrayObject {
54                fn from(val: &&$ty) -> Self {
55                    (*val).into()
56                }
57            }
58            impl From<&&Vec<$ty>> for ArrayObject {
59                fn from(val: &&Vec<$ty>) -> Self {
60                    (*val).into()
61                }
62            }
63            impl<const N: usize> From<&&[$ty; N]> for ArrayObject {
64                fn from(val: &&[$ty; N]) -> Self {
65                    (*val).into()
66                }
67            }
68            // Box
69            impl From<Box<$ty>> for ArrayObject {
70                fn from(val: Box<$ty>) -> Self {
71                    val.as_ref().into()
72                }
73            }
74            impl From<Box<Vec<$ty>>> for ArrayObject {
75                fn from(val: Box<Vec<$ty>>) -> Self {
76                    val.as_ref().into()
77                }
78            }
79            impl<const N: usize> From<Box<[$ty; N]>> for ArrayObject {
80                fn from(val: Box<[$ty; N]>) -> Self {
81                    val.as_ref().into()
82                }
83            }
84            // Adaptor
85            impl TryFrom<VecShape<$ty>> for ArrayObject {
86                type Error = ArrayObjectError;
87                fn try_from(VecShape(val, shape): VecShape<$ty>) -> Result<Self, Self::Error> {
88                    if val.len() != shape.product() as usize {
89                        return Err(ArrayObjectError::NumberOfElementsMismatch(val.len(), shape.product() as usize));
90                    }
91                    if shape.len() > 15 {
92                        return Err(ArrayObjectError::TooLargeDimension(shape.len()));
93                    }
94                    let mut temp: ArrayObject = val.into();
95                    temp.shape = shape;
96                    Ok(temp)
97                }
98            }
99        )*
100    };
101}
102
103from_text!(String, &str);