1use std::{cmp::Ordering, collections::HashMap};
9
10use serde::{Deserialize, Serialize};
11
12use super::ValidName;
13
14#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Default)]
15pub struct ValidMeta(pub HashMap<ValidName, Vec<ValidName>>);
16
17impl ValidMeta {
18 pub fn to_vec(&self) -> Vec<(String, Vec<String>)> {
19 self.0
20 .iter()
21 .map(|(k, v)| {
22 (
23 k.as_ref().to_string(),
24 v.iter().map(|v| v.as_ref().to_string()).collect(),
25 )
26 })
27 .collect::<Vec<_>>()
28 }
29}
30
31impl<T: AsRef<str>> TryFrom<HashMap<T, Vec<T>>> for ValidMeta {
32 type Error = anyhow::Error;
33
34 fn try_from(m: HashMap<T, Vec<T>>) -> Result<Self, Self::Error> {
35 Ok(Self(
36 m.into_iter()
37 .map(|(k, v)| -> Result<_, Self::Error> {
38 Ok((
39 ValidName::parse(k.as_ref().to_string())?,
40 v.iter()
41 .map(|v| -> Result<_, Self::Error> {
42 Ok(ValidName::parse(v.as_ref().to_string())?)
43 })
44 .collect::<Result<Vec<ValidName>, Self::Error>>()?,
45 ))
46 })
47 .collect::<Result<_, Self::Error>>()?,
48 ))
49 }
50}
51
52impl TryFrom<Vec<(String, Vec<String>)>> for ValidMeta {
53 type Error = anyhow::Error;
54
55 fn try_from(m: Vec<(String, Vec<String>)>) -> Result<Self, Self::Error> {
56 Ok(Self(
57 m.into_iter()
58 .map(|um| -> Result<_, Self::Error> {
59 Ok((
60 ValidName::parse(um.0)?,
61 um.1.into_iter()
62 .map(|v| -> Result<_, Self::Error> { Ok(ValidName::parse(v)?) })
63 .collect::<Result<Vec<ValidName>, Self::Error>>()?,
64 ))
65 })
66 .collect::<Result<_, Self::Error>>()?,
67 ))
68 }
69}
70
71impl TryFrom<Meta> for ValidMeta {
72 type Error = anyhow::Error;
73
74 fn try_from(m: Meta) -> Result<Self, Self::Error> {
75 Ok(Self(
76 m.0.into_iter()
77 .map(|(key, value)| -> Result<_, Self::Error> {
78 Ok((
79 ValidName::parse(key)?,
80 value
81 .into_iter()
82 .map(|v| -> Result<_, Self::Error> { Ok(ValidName::parse(v)?) })
83 .collect::<Result<Vec<ValidName>, Self::Error>>()?,
84 ))
85 })
86 .collect::<Result<_, Self::Error>>()?,
87 ))
88 }
89}
90
91#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Default, sqlx::FromRow)]
105pub struct Meta(pub HashMap<String, Vec<String>>);
106
107impl Meta {
108 pub fn new() -> Self {
110 Self(HashMap::new())
111 }
112
113 pub fn len(&self) -> usize {
115 self.0.len()
116 }
117
118 pub fn is_empty(&self) -> bool {
120 self.len() == 0
121 }
122
123 pub fn to_vec(&self) -> Vec<(String, Vec<String>)> {
125 self.0
126 .iter()
127 .map(|(k, v)| (k.clone(), v.clone()))
128 .collect::<Vec<_>>()
129 }
130
131 pub fn insert(&mut self, name: String, values: Vec<String>) {
133 self.0.insert(name, values);
134 }
135
136 pub fn get<T: AsRef<str>>(&self, key: T) -> Option<&Vec<String>> {
138 self.0.get(key.as_ref())
139 }
140}
141
142impl From<ValidMeta> for Meta {
143 fn from(m: ValidMeta) -> Self {
144 Self(
145 m.0.into_iter()
146 .map(|(k, v)| {
147 (
148 k.as_ref().to_string(),
149 v.into_iter().map(|v| v.as_ref().to_string()).collect(),
150 )
151 })
152 .collect(),
153 )
154 }
155}
156
157impl<T: AsRef<str>> TryFrom<HashMap<T, Vec<T>>> for Meta {
158 type Error = anyhow::Error;
159
160 fn try_from(m: HashMap<T, Vec<T>>) -> Result<Self, Self::Error> {
161 Ok(Self(
162 m.into_iter()
163 .map(|(k, v)| -> Result<_, Self::Error> {
164 Ok((
165 k.as_ref().to_string(),
166 v.into_iter()
167 .map(|v| -> Result<_, Self::Error> { Ok(v.as_ref().to_string()) })
168 .collect::<Result<Vec<String>, Self::Error>>()?,
169 ))
170 })
171 .collect::<Result<_, Self::Error>>()?,
172 ))
173 }
174}
175
176impl TryFrom<Vec<(String, Vec<String>)>> for Meta {
177 type Error = anyhow::Error;
178
179 fn try_from(m: Vec<(String, Vec<String>)>) -> Result<Self, Self::Error> {
180 Ok(Self(
181 m.into_iter()
182 .map(|um| -> Result<_, Self::Error> { Ok((um.0.clone(), um.1)) })
183 .collect::<Result<_, Self::Error>>()?,
184 ))
185 }
186}
187
188impl PartialOrd for Meta {
189 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
190 Some(self.cmp(other))
191 }
192}
193
194impl Ord for Meta {
195 fn cmp(&self, _other: &Self) -> Ordering {
196 Ordering::Equal
197 }
198}