array_object/convert/
from_string.rs

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
use crate::adaptor::*;
use crate::error::ArrayObjectError;
use crate::misc::Product;
use crate::storage::*;

macro_rules! from_text {
    ($($ty:ty),*) => {
        $(
            impl From<$ty> for ArrayObject {
                fn from(val: $ty) -> Self {
                    let data: Vec<u8> = val.to_string().into_bytes().to_vec();
                    Self {
                        data,
                        shape: vec![],
                        datatype: DataType::String,
                    }
                }
            }
            impl From<Vec<$ty>> for ArrayObject {
                fn from(val: Vec<$ty>) -> Self {
                    let shape = vec![val.len() as u64];
                    let val: Vec<_> = val.into_iter().map(|x| x.to_string()).collect();
                    let data = val.into_iter().map(|x| x.as_bytes().to_vec()).collect::<Vec<_>>().join(&255u8);
                    Self {
                        data,
                        shape,
                        datatype: DataType::String,
                    }
                }
            }
            impl TryFrom<VecShape<$ty>> for ArrayObject {
                type Error = ArrayObjectError;
                fn try_from(VecShape(val, shape): VecShape<$ty>) -> Result<Self, Self::Error> {
                    if val.len() != shape.product() as usize {
                        return Err(ArrayObjectError::NumberOfElementsMismatch(val.len(), shape.product() as usize));
                    }
                    if shape.len() > 15 {
                        return Err(ArrayObjectError::TooLargeDimension(shape.len()));
                    }
                    let mut temp: ArrayObject = val.into();
                    temp.shape = shape;
                    Ok(temp)
                }
            }
        )*
    };
}

from_text!(String, &str);