db_dump/
set.rs

1use serde::de::{Deserializer, Unexpected, Visitor};
2use std::fmt;
3
4struct SetVisitor<'a> {
5    expecting: &'a str,
6    optional: bool,
7}
8
9impl<'de, 'a> Visitor<'de> for SetVisitor<'a> {
10    type Value = Vec<String>;
11
12    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
13        formatter.write_str(self.expecting)
14    }
15
16    fn visit_str<E>(self, string: &str) -> Result<Self::Value, E>
17    where
18        E: serde::de::Error,
19    {
20        if string.starts_with('{') && string.ends_with('}') {
21            let csv = &string[1..string.len() - 1];
22            if csv.is_empty() {
23                Ok(Vec::new())
24            } else {
25                Ok(csv.split(',').map(str::to_owned).collect())
26            }
27        } else if self.optional && string.is_empty() {
28            Ok(Vec::new())
29        } else {
30            Err(serde::de::Error::invalid_value(
31                Unexpected::Str(string),
32                &self,
33            ))
34        }
35    }
36}
37
38pub(crate) fn de<'de, D>(deserializer: D, expecting: &str) -> Result<Vec<String>, D::Error>
39where
40    D: Deserializer<'de>,
41{
42    deserializer.deserialize_str(SetVisitor {
43        expecting,
44        optional: false,
45    })
46}
47
48pub(crate) fn optional<'de, D>(deserializer: D, expecting: &str) -> Result<Vec<String>, D::Error>
49where
50    D: Deserializer<'de>,
51{
52    deserializer.deserialize_str(SetVisitor {
53        expecting,
54        optional: true,
55    })
56}