solomon_gremlin/structure/
label.rs

1use crate::structure::T;
2
3pub enum LabelType {
4	Str(String),
5	Bool(bool),
6	T(T),
7}
8
9impl LabelType {
10	pub fn bytes(&self) -> Vec<u8> {
11		match self {
12			LabelType::Str(v) => v.as_bytes().to_vec(),
13			_ => unimplemented!(),
14		}
15	}
16
17	pub fn bytes_len(&self) -> usize {
18		self.bytes().len()
19	}
20}
21
22pub struct Labels(pub Vec<LabelType>);
23
24impl From<&str> for Labels {
25	fn from(param: &str) -> Labels {
26		Labels(vec![LabelType::Str(String::from(param))])
27	}
28}
29
30impl From<String> for Labels {
31	fn from(param: String) -> Labels {
32		Labels(vec![LabelType::Str(param)])
33	}
34}
35
36impl From<T> for Labels {
37	fn from(param: T) -> Labels {
38		Labels(vec![LabelType::T(param)])
39	}
40}
41
42impl From<()> for Labels {
43	fn from(_: ()) -> Labels {
44		Labels(vec![])
45	}
46}
47impl From<Vec<&str>> for Labels {
48	fn from(param: Vec<&str>) -> Labels {
49		Labels(param.into_iter().map(|val| LabelType::Str(String::from(val))).collect())
50	}
51}
52impl From<Vec<String>> for Labels {
53	fn from(param: Vec<String>) -> Labels {
54		Labels(param.into_iter().map(LabelType::Str).collect())
55	}
56}
57
58impl From<bool> for Labels {
59	fn from(param: bool) -> Labels {
60		Labels(vec![LabelType::Bool(param)])
61	}
62}
63
64impl From<(bool, Vec<&str>)> for Labels {
65	fn from(param: (bool, Vec<&str>)) -> Labels {
66		let mut out: Vec<LabelType> = vec![LabelType::Bool(param.0)];
67		out.append(&mut Into::<Labels>::into(param.1).0.drain(..).collect());
68		Labels(out)
69	}
70}
71
72impl From<(bool, T, Vec<&str>)> for Labels {
73	fn from(param: (bool, T, Vec<&str>)) -> Labels {
74		let mut out: Vec<LabelType> = vec![LabelType::Bool(param.0)];
75		out.append(&mut Into::<Labels>::into(param.1).0.drain(..).collect());
76		out.append(&mut Into::<Labels>::into(param.2).0.drain(..).collect());
77		Labels(out)
78	}
79}
80
81impl From<(T, Vec<&str>)> for Labels {
82	fn from(param: (T, Vec<&str>)) -> Labels {
83		let mut out: Vec<LabelType> = vec![LabelType::T(param.0)];
84		out.append(&mut Into::<Labels>::into(param.1).0.drain(..).collect());
85		Labels(out)
86	}
87}
88
89macro_rules! impl_into_labels_str {
90	($n:expr) => {
91		impl From<[&str; $n]> for Labels {
92			fn from(param: [&str; $n]) -> Labels {
93				Labels(param.iter().map(|s| LabelType::Str(String::from(*s))).collect())
94			}
95		}
96	};
97}
98
99impl_into_labels_str!(1);
100impl_into_labels_str!(2);
101impl_into_labels_str!(3);
102impl_into_labels_str!(4);
103impl_into_labels_str!(5);
104impl_into_labels_str!(6);
105impl_into_labels_str!(7);
106impl_into_labels_str!(8);
107impl_into_labels_str!(9);
108impl_into_labels_str!(10);
109
110macro_rules! impl_into_labels_string {
111	($n:expr) => {
112		impl From<[String; $n]> for Labels {
113			fn from(param: [String; $n]) -> Labels {
114				Labels(param.iter().map(|val| LabelType::Str(val.clone())).collect())
115			}
116		}
117	};
118}
119
120impl_into_labels_string!(1);
121impl_into_labels_string!(2);
122impl_into_labels_string!(3);
123impl_into_labels_string!(4);
124impl_into_labels_string!(5);
125impl_into_labels_string!(6);
126impl_into_labels_string!(7);
127impl_into_labels_string!(8);
128impl_into_labels_string!(9);
129impl_into_labels_string!(10);