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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use darling::FromMeta;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct VecStringWrapper {
pub v: Vec<String>,
pub c: Vec<char>, }
impl From<Vec<String>> for VecStringWrapper {
fn from(v: Vec<String>) -> Self {
VecStringWrapper {
c: v
.iter()
.map(|s| s.chars().next().unwrap())
.collect::<Vec<char>>(),
v,
}
}
}
impl From<VecStringWrapper> for Vec<String> {
fn from(val: VecStringWrapper) -> Self {
val.v
}
}
impl FromMeta for VecStringWrapper {
fn from_nested_meta(item: &syn::NestedMeta) -> darling::Result<Self> {
(match *item {
syn::NestedMeta::Lit(ref lit) => Self::from_value(lit),
syn::NestedMeta::Meta(ref mi) => Self::from_meta(mi),
})
.map_err(|e| e.with_span(item))
}
fn from_meta(item: &syn::Meta) -> darling::Result<Self> {
(match *item {
syn::Meta::Path(_) => Self::from_word(),
syn::Meta::List(ref value) => Self::from_list(
&value
.nested
.iter()
.cloned()
.collect::<Vec<syn::NestedMeta>>()[..],
),
syn::Meta::NameValue(ref value) => Self::from_value(&value.lit),
})
.map_err(|e| e.with_span(item))
}
fn from_none() -> Option<Self> {
None
}
fn from_word() -> darling::Result<Self> {
Err(darling::Error::unsupported_format("word"))
}
fn from_list(_items: &[syn::NestedMeta]) -> darling::Result<Self> {
Err(darling::Error::unsupported_format("list"))
}
fn from_value(value: &syn::Lit) -> darling::Result<Self> {
(match *value {
syn::Lit::Bool(ref b) => Self::from_bool(b.value),
syn::Lit::Str(ref s) => Self::from_string(&s.value()),
syn::Lit::Char(ref ch) => Self::from_char(ch.value()),
_ => Err(darling::Error::unexpected_lit_type(value)),
})
.map_err(|e| e.with_span(value))
}
fn from_char(_value: char) -> darling::Result<Self> {
Err(darling::Error::unexpected_type("char"))
}
fn from_string(value: &str) -> darling::Result<Self> {
Ok(VecStringWrapper {
v: value
.split(',')
.map(|s| s.to_string())
.collect::<Vec<String>>(),
c: value
.split(',')
.map(|s| s.chars().next().unwrap_or_default())
.collect::<Vec<char>>(),
})
}
fn from_bool(_value: bool) -> darling::Result<Self> {
Err(darling::Error::unexpected_type("bool"))
}
}