pml/impls/
string.rs

1use crate::{Element, elem::ArrayElement as E, GetError};
2use std::string::ToString as TS;
3use Element::PmlArray as A;
4
5impl From<String> for Element {
6    fn from(s: String) -> Self {
7        Element::PmlString(s)
8    }
9}
10
11impl TryFrom<&Element> for String {
12    type Error = GetError;
13    fn try_from(elem: &Element) -> Result<Self, Self::Error> {
14        match elem {
15            Element::PmlString(s) => Ok(s.clone()),
16            Element::PmlBool(b) => Ok(b.to_string()),
17            Element::PmlI8(i) => Ok(i.to_string()),
18            Element::PmlI16(i) => Ok(i.to_string()),
19            Element::PmlI32(i) => Ok(i.to_string()),
20            Element::PmlI64(i) => Ok(i.to_string()),
21            Element::PmlI128(i) => Ok(i.to_string()),
22            Element::PmlU8(i) => Ok(i.to_string()),
23            Element::PmlU16(i) => Ok(i.to_string()),
24            Element::PmlU32(i) => Ok(i.to_string()),
25            Element::PmlU64(i) => Ok(i.to_string()),
26            Element::PmlU128(i) => Ok(i.to_string()),
27            Element::PmlF32(f) => Ok(f.to_string()),
28            Element::PmlF64(f) => Ok(f.to_string()),
29            Element::PmlStruct(_) |
30            Element::PmlArray(_) => Err(Self::Error::InvalidType)
31        }
32    }
33}
34
35impl TryFrom<&Element> for Vec<String> {
36    type Error = GetError;
37    fn try_from(value: &Element) -> Result<Self, Self::Error> {
38        match value {
39            A(E::PmlString(arr)) => Ok(arr.clone()),
40            A(E::PmlBool(arr)) => Ok(arr.iter().map(TS::to_string).collect()),
41            A(E::PmlU8(arr)) => Ok(arr.iter().map(TS::to_string).collect()),
42            A(E::PmlU16(arr)) => Ok(arr.iter().map(TS::to_string).collect()),
43            A(E::PmlU32(arr)) => Ok(arr.iter().map(TS::to_string).collect()),
44            A(E::PmlU64(arr)) => Ok(arr.iter().map(TS::to_string).collect()),
45            A(E::PmlU128(arr)) => Ok(arr.iter().map(TS::to_string).collect()),
46            A(E::PmlI8(arr)) => Ok(arr.iter().map(TS::to_string).collect()),
47            A(E::PmlI16(arr)) => Ok(arr.iter().map(TS::to_string).collect()),
48            A(E::PmlI32(arr)) => Ok(arr.iter().map(TS::to_string).collect()),
49            A(E::PmlI64(arr)) => Ok(arr.iter().map(TS::to_string).collect()),
50            A(E::PmlI128(arr)) => Ok(arr.iter().map(TS::to_string).collect()),
51            A(E::PmlF32(arr)) => Ok(arr.iter().map(TS::to_string).collect()),
52            A(E::PmlF64(arr)) => Ok(arr.iter().map(TS::to_string).collect()),
53            _ => Err(Self::Error::InvalidType)
54        }
55    }
56}